对我先前的问题采取了后续行动。我正在尝试使用JSR-330标准注释和jersey捆绑的HK2框架注入应用程序配置数据。
理想情况下,我想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()
是空的。知道有什么问题吗?另外,我可以绑定我的Injector实例而不是绑定类吗?这样,我可以构造将Map
数据作为参数传递的实例。
“我的问题是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
到定位器上下文对我来说似乎不合适。
然后我的看起来像 我的问题是为空。知道怎么了吗?另外,我可以绑定我的注入器的实例而不是绑定类吗?这样,我就可以构造将数据作为参数传递的实例。
问题内容: 主要问题-可能吗?我没有运气就尝试了.. 主app.js 提供者本身 而且我有这样的错误: 有任何想法吗? 谢谢! 问题答案: 您 不能 将服务注入提供者配置部分。 您 CAN 注入服务成初始化提供者的服务的部分。 细节: Angular框架有两个阶段的初始化过程: 阶段1:配置 在此阶段,将初始化所有提供程序,并执行所有部分。这些部分可能包含配置提供程序对象的代码,因此可以将它们与提
目前,我将辅助注射与命名参数一起使用,如下所示: 这很棒。但是我认为使用字符串作为参数的标识符有点难看。我想做的是以下内容: 所以本质上我想要自定义辅助注释。有办法做到这一点吗?
我发现了几个与此相关的(不是重复的)问题,但它们不能让我满意。 我无法理解在哪里以及为什么要使用? 我在一本书中读到了一个自定义注释的示例,但没有详细解释。 myMeth()内的输出与预期一致。 关于这个例子,我有几个问题。 1-如何在此程序中使用和?或
我正在基于Dropwizard v0.9.1构建一个web堆栈。堆栈中的所有日志都通过AppenderFactory接口的自定义实现发送到Loggly: 此类未在我的应用程序类中的环境中注册。相反,它似乎是Dropwizard基于@JsonTypeName注释自动连接的。尽管如此,和字段由出现在我的配置yaml文件中的值填充: 问题是,这些配置值不会出现在应用程序的配置类中,这意味着在构建其他资源
下面是如何配置应用程序的 问题是在应用程序启动期间,我得到以下错误 并且有很长的堆栈错误堆栈和描述 null 我刚试过用两个自定义方法param注入,那也不起作用