“我的问题是 application.getProperties() 是空的。知道有什么问题吗?
不,这对我来说实际上非常有效。
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();
}
}
C:\>curl http://localhost:8080/test/rest/config
Hello World Property
不过就个人而言,在这种情况下,我会创建自己的注释,以免覆盖@Named
注释的任何现有功能。
另一个很酷的选择
HK2有一个配置扩展,你可以Properties
从一个文件中加载一个对象.properties
,并让这些属性自动注入@Configured
注释。我找不到任何关于此的文档,但在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();
}
}
C:\>curl http://localhost:8080/test/rest/config
Hello Squirrel Property!
免责声明:由于此功能没有很好的文档记录,我不确定我是否在这里有一个好的实现。这只是通过反复试验。例如这个
ServiceLocatorUtilities.addClasses(locator, ConfigResource.class);
我觉得应该没有必要。这似乎是多余的,因为我已经在扫描包了。因此,将 显式添加ConfigResource
到定位器上下文对我来说似乎不合适。