How does ServiceLocator find @Service and @Contact automatically in HK2?(ServiceLocator 如何在 HK2 中自动找到 @Service 和 @Contact?)
问题描述
根据 HK2 @Service javadoc
对要自动添加到hk2 服务定位器.
Annotation placed on classes that are to be automatically added to an hk2 ServiceLocator.
我不知道如何让 ServiceLocator 自动找到带注释的类.
I don't know how to make ServiceLocator find annotated classes automatically.
测试服务
@Contract
public interface TestService {
}
TestServiceImpl
@Service
public class TestServiceImpl implements TestService {
}
主要
public static void main(String[] args) {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
TestService service = locator.getService(TestServiceImpl.class);
System.out.println(service); // null
}
结果始终为 null.我必须添加 Descriptor 以便 ServiceLocator 可以找到它.
The result is always null. I have to add Descriptor so the ServiceLocator can find it.
public static void main(String[] args) {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration config = dcs.createDynamicConfiguration();
config.bind(BuilderHelper.link(TestServiceImpl.class).to(TestService.class).in(Singleton.class).build());
config.commit();
TestService service = locator.getService(TestServiceImpl.class);
System.out.println(service); // TestServiceImpl instance
}
如何让 ServiceLocator 自动找到带注解的类?我是不是误会了什么?
How do I let ServiceLocator find the annotated classes automatically ? Did I misunderstand something ?
推荐答案
需要运行hk2-inhabitant-generator 覆盖您构建的类,以便自动检测服务.这里还有更多信息.
You need to run the hk2-inhabitant-generator over your built classes in order to get automatic detection of services. There is more information here as well.
该步骤在构建过程中的作用是创建一个名为 META-INF/hk2-locator/default 的文件,其中包含有关服务的信息.然后 createAndPopulateServiceLocator 调用读取这些文件并自动将这些服务描述符添加到返回的 ServiceLocator 中.
What that step does in the build process is to create a file named META-INF/hk2-locator/default with information about services. The createAndPopulateServiceLocator call then reads those files and automatically adds those service descriptors into the returned ServiceLocator.
这篇关于ServiceLocator 如何在 HK2 中自动找到 @Service 和 @Contact?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ServiceLocator 如何在 HK2 中自动找到 @Service 和 @Contact?
基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
