【问题标题】:Unsatisfied dependencies for type String with qualifiers @SystemProperty带有限定符 @SystemProperty 的 String 类型的依赖关系不满足
【发布时间】:2014-11-09 20:01:55
【问题描述】:

我试图通过注入值来读取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;
    }
    ....
}  

我使用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:

我错过了什么?

【问题讨论】:

  • 这些类在单独的 jar 中吗?你有一个 beans.xml 在他们两个?

标签: java maven jboss wildfly


【解决方案1】:

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

顺便说一下,对于注入系统和其他环境属性,我建议看看 Apache DeltaSpike @ConfigProperty

【讨论】:

    【解决方案2】:

    通过日志中的以下消息:

    [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 类中没有出现该错误,此消息是由类com.karmabeta.services.filter.AuthTokenValidatorFilter 部署application.war 模块生成的。

    根据文档消息:

    WELD-001408: Unsatisfied dependencies for type String with qualifiers @SystemProperty
    

    要修复不满足的依赖关系,可以:

    • 创建一个实现 bean 类型并具有注入点的所有限定符类型的 bean,
    • 确保您已经拥有的 bean 位于具有注入点的模块的类路径中,或者
    • 使用 beans.xml 显式启用实现 bean 类型并具有适当限定符类型的 @Alternative bean。
    • 使用 @Priority 注释启用实现 bean 类型并具有适当限定符类型的 @Alternative bean。

    我认为第二项是解决方案的一部分,您可能在 ejb 模块中有生产者,这使得它在 war 模块中不可见。

    请注意,@Producer 始终只能从顶级部署单元(例如战争或 ear/lib)中获取,而不是从子部署中获取。

    如果是这种情况,您可以将您的生产者和其他组件 CDI 放在 ear/lib 或直接 war/classes 中的 jar 文件中。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多