使用自定义的hk2 InjectionResolver注入应用程序配置

使用自定义的hk2 InjectionResolver注入应用程序配置,第1张

使用自定义的hk2 InjectionResolver注入应用程序配置

“我的问题是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源代码示例中有一个示例用法。

这是一个示例实现

必需的依赖项。检查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
到定位器上下文对我来说似乎不合适。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存