对my previous question的跟进。我正在尝试使用JSR-330标准注释和jersey捆绑的HK2框架注入(inject)应用程序配置数据。
理想情况下,我想为InjectionResolver
批注创建一个自定义的Named
,它将在Map
或Properties
对象中查找所需的值,这些对象将从其他地方读取的数据中填充。在我的第一次尝试中,我创建了一个Application
实例,例如
public class MyApplication extends ResourceConfig {
...
packages(MY_PACKAGES);
property(MY_CONFIG_PROPERTY, someValue);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(ConfigurationInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<Named>>(){})
.in(Singleton.class)
}
});
}
然后我的
InjectionResolver
看起来像public class ConfigurationInjectionResolver implements InjectionResolver<Named> {
@Context Application application;
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) {
// lookup data in application.getProperties();
}
}
我的问题是
application.getProperties()
为空。知道有什么问题吗?另外,我可以绑定(bind)我的Injector实例而不是绑定(bind)类吗?这样,我可以构造将Map
数据作为参数传递的实例。 最佳答案
否。这实际上对我来说很好。
public class ConfigurationInjectionResolver implements InjectionResolver<Named> {
@Context
Application application;
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
Named annotation = injectee.getParent().getAnnotation(Named.class);
Map<String, Object> props = application.getProperties();
String name = annotation.value();
System.out.println(props.get(name));
return props.get(name);
}
@Override
public boolean isConstructorParameterIndicator() { return false; }
@Override
public boolean isMethodParameterIndicator() { return false; }
}
@ApplicationPath("/rest")
public class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
packages("jersey.startup.test");
property("hello.config", "Hello World Property");
register(new AbstractBinder() {
@Override
protected void configure() {
bind(ConfigurationInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<Named>>() {
}).in(Singleton.class);
}
});
}
}
资源资源
@Path("/config")
public class ConfigResource {
@Named("hello.config")
String hello;
@GET
public Response getHello() {
return Response.ok(hello).build();
}
}
不过,就个人而言,在这种情况下,我将创建自己的注释,以免覆盖
@Named
注释的任何现有功能。另一个很酷的选择
HK2具有配置扩展名,您可以在其中从例如
Properties
文件中加载.properties
对象,并使用@Configured
批注自动注入(inject)这些属性。我没有找到任何文档,但是在HK2 source code examples中有一个示例用法。这是一个示例实现
必需的依赖项。检查Jersey版本,并查看它所依赖的HK2版本。在我的情况下,Jersey 2.13使用HK2 2.3.0-b10,因此应为
${hk2.version}
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-configuration-hub</artifactId>
<version>${hk2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-configuration-integration</artifactId>
<version>${hk2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-property-file</artifactId>
<version>${hk2.version}</version>
</dependency>
应用配置
@ApplicationPath("/rest")
public class JerseyApplication extends ResourceConfig {
@Inject
public JerseyApplication(ServiceLocator locator) {
packages("jersey.startup.test");
ServiceLocatorUtilities.addClasses(locator, ConfigResource.class);
try {
loadConfigurationProperties(locator);
} catch (IOException ex) {
Logger.getLogger(JerseyApplication.class.getName())
.log(Level.SEVERE, null, ex);
}
}
private void loadConfigurationProperties(ServiceLocator locator)
throws IOException {
ConfigurationUtilities.enableConfigurationSystem(locator);
PropertyFileUtilities.enablePropertyFileService(locator);
PropertyFileService propertyFileService
= locator.getService(PropertyFileService.class);
Properties props = new Properties();
URL url = getClass().getResource("/configuration.properties");
props.load(url.openStream());
PropertyFileHandle propertyFileHandle
= propertyFileService.createPropertyHandleOfAnyType();
propertyFileHandle.readProperties(props);
}
}
configuration.properties
AppConfiguration.App.hello=Hello Squirrel Property!
资源资源
@Path("/config")
@ConfiguredBy("AppConfiguration")
public class ConfigResource {
@Configured
String hello;
@GET
public Response getHello() {
return Response.ok(hello).build();
}
}
免责声明:由于此功能的文档记录不充分,因此我不确定此处是否有良好的实现。这只是反复试验。例如这个
ServiceLocatorUtilities.addClasses(locator, ConfigResource.class);
我觉得没有必要。似乎是多余的,因为我已经在进行程序包扫描了。因此,将
ConfigResource
显式添加到定位器上下文对我来说似乎不合适。关于java - 使用自定义的hk2 InjectionResolver注入(inject)应用程序配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26386006/