gpt4 book ai didi

java - 带有限定符 @SystemProperty 的 String 类型的依赖关系不满足

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:39 26 4
gpt4 key购买 nike

我正在尝试通过注入(inject)值来读取系统属性

我正在尝试使用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;
}
}

我注入(inject)的方式是

@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;
}
....
}

我使用 maven Cargo 部署我的 war 并将属性设置为

                 <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:

我错过了什么?

最佳答案

假设您正在使用 WildFly 8.x/CDI 1.1,您的 SystemPropertyProvider 似乎会被忽略,因为它缺少 bean 定义注释。或者您是否将其放入带有合适的 beans.xml 描述符的显式 bean 存档中?

顺便说一下,为了注入(inject)系统和其他环境属性,我建议查看 Apache DeltaSpike @ConfigProperty .

关于java - 带有限定符 @SystemProperty 的 String 类型的依赖关系不满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26832663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com