我试图通过注入值来阅读System Property
。
我正在尝试使用http://juraj.blahunka.eu/2014/05/17/inject-jboss-system-properties/作为参考。
我的代码看起来像
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
public @interface SystemProperty {
/*
Full name of System Property
*/
@Nonbinding
String value();
}
和
public class SystemPropertyProvider {
@Produces
@SystemProperty("")
String getSystemProperty(final InjectionPoint injectionPoint) {
final SystemProperty annotation = injectionPoint.getAnnotated().getAnnotation(SystemProperty.class);
final String name = annotation.value();
final String found = System.getProperty(name);
if (null == found) {
throw new RuntimeException("System property " + name + "not found");
}
System.out.println("serverPrivateKeyValue:" + found);
return found;
}
}
和我注入的方式是
@Stateless
public class UniqueIdGenerator {
private static final String COLON = ":";
private String serverPrivateKey;
@SuppressWarnings("UnusedDeclaration")
public UniqueIdGenerator() {
}
@Inject
public UniqueIdGenerator(@SystemProperty("com.kb.serverPrivateKey") @Nonnull final String serverPrivateKey) {
this.serverPrivateKey = serverPrivateKey;
}
....
}
我使用war
部署maven cargo
并将属性设置为
<container>
<containerId>wildfly8x</containerId>
<dependencies combine.children="append">
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<systemProperties>
<com.kb.serverPrivateKey>test</com.kb.serverPrivateKey>
</systemProperties>
</container>
当我部署应用程序时,我将错误视为
[INFO] [talledLocalContainer] 11:57:49,676 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."application.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."application.war".WeldStartService: Failed to start service
[INFO] [talledLocalContainer] at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
[INFO] [talledLocalContainer] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type String with qualifiers @SystemProperty
[INFO] [talledLocalContainer] at injection point [BackedAnnotatedField] @Inject @SystemProperty private com.karmabeta.services.filter.AuthTokenValidatorFilter.serverPrivateKey
[INFO] [talledLocalContainer] at com.karmabeta.services.filter.AuthTokenValidatorFilter.serverPrivateKey(AuthTokenValidatorFilter.java:0)
[INFO] [talledLocalContainer] WELD-001475: The following beans match by type, but none have matching qualifiers:
[INFO] [talledLocalContainer] - Producer Method [String] with qualifiers [@BatchProperty @Any] declared as [[UnbackedAnnotatedMethod] @Produces @BatchProperty public org.jberet.creation.BatchBeanProducer.getString(InjectionPoint)]
[INFO] [talledLocalContainer]
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:368)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:289)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:135)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:166)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:514)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
[INFO] [talledLocalContainer] at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
[INFO] [talledLocalContainer] at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
[INFO] [talledLocalContainer] at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
[INFO] [talledLocalContainer] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] ... 3 more
[INFO] [talledLocalContainer]
[INFO] [talledLocalContainer] 11:57:49,686 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "application.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"application.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"application.war\".WeldStartService: Failed to start service
[INFO] [talledLocalContainer] Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type String with qualifiers @SystemProperty
[INFO] [talledLocalContainer] at injection point [BackedAnnotatedField] @Inject @SystemProperty private com.karmabeta.services.filter.AuthTokenValidatorFilter.serverPrivateKey
[INFO] [talledLocalContainer] at com.karmabeta.services.filter.AuthTokenValidatorFilter.serverPrivateKey(AuthTokenValidatorFilter.java:0)
[INFO] [talledLocalContainer] WELD-001475: The following beans match by type, but none have matching qualifiers:
[INFO] [talledLocalContainer] - Producer Method [String] with qualifiers [@BatchProperty @Any] declared as [[UnbackedAnnotatedMethod] @Produces @BatchProperty public org.jberet.creation.BatchBeanProducer.getString(InjectionPoint)]
[INFO] [talledLocalContainer] "}}
[INFO] [talledLocalContainer] 11:57:49,723 INFO [org.jboss.as.server] (ServerService Thread Pool -- 29) JBAS018559: Deployed "cargocpc.war" (runtime-name : "cargocpc.war")
[INFO] [talledLocalContainer] 11:
我错过了什么?
答案 0 :(得分:1)
假设您正在使用WildFly 8.x / CDI 1.1,似乎忽略了您的SystemPropertyProvider
,因为它缺少定义注释的bean。或者您是否将其放入具有合适的beans.xml
描述符的显式bean存档中?
顺便说一句,对于注入系统和其他环境属性,我建议你看一下Apache DeltaSpike @ConfigProperty
。
答案 1 :(得分:0)
通过日志中的以下消息:
[INFO] [talledLocalContainer] 11:57:49,676 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."application.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."application.war".WeldStartService: Failed to start service
EJB类中不会发生此错误,此消息是由类application.war
部署com.karmabeta.services.filter.AuthTokenValidatorFilter
模块生成的。
根据documentacion消息:
WELD-001408: Unsatisfied dependencies for type String with qualifiers @SystemProperty
要修复不满意的依赖关系,请执行以下操作:
- 创建一个实现bean类型的bean,并具有注入点的所有限定符类型
- 确保您已经拥有的bean位于具有注入点的模块的类路径中,或
- 使用beans.xml显式启用实现bean类型并具有相应限定符类型的
@Alternative
bean。- 使用
@Alternative
注释启用实现bean类型且具有相应限定符类型的@Priority
bean。
我认为第二项是解决方案的一部分,你可能在ejb模块中有生产者,这使得它从war模块中看不到。
请注意,@Producer
始终只能从顶级部署单元(例如war或ear / lib)中获取,而不是从子部署中获取。
如果是这种情况,您可以将生产者和其他组件CDI放在ear / lib或直接war / classes中的jar文件中。
我希望这有帮助。