到目前为止,我已经做到了这一点:
@Inject
@ScoringModule("AGE")
private AgeModule ageModule;
@Inject
@ScoringModule("CUSTOMER_TYPE")
private CustomerTypeModule customerTypeModule;
@Inject @Any
private Instance<CustomerScoringModule> scoringModuleInstance;
CustomerScoringModule module = scoringModuleInstance.select(new ScoringModuleLiteral("AGE")).get();
然后我会得到这样一个例外:
org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type CustomerScoringModule with qualifiers @Any @ScoringModule
有没有什么“明显”的东西让我想到我可能做错了?我第一次尝试使用CDI,有可能(甚至可能)我在某处做了一些愚蠢的事情:-)
任何帮助或提示都将不胜感激,并提前表示感谢!
package de.otto.cccs.customerscoring.modules;
import de.otto.cccs.customerscoring.modules.vo.ScoringModuleResponseVO;
import de.otto.cccs.customerscoring.valueobjects.webservice.ScoringRequestVO;
public interface CustomerScoringModule {
public ScoringModuleResponseVO process(ScoringRequestVO inputVO);
}
两个模块实现中的一个(它们现在只是虚拟实现,唯一的区别是process()方法中的log.info()输出,这就是为什么im在这里只包括其中一个):
package de.otto.cccs.customerscoring.modules;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import de.otto.cccs.customerscoring.modules.qualifiers.ScoringModule;
import de.otto.cccs.customerscoring.modules.vo.ScoringModuleResponseVO;
import de.otto.cccs.customerscoring.valueobjects.webservice.ScoringRequestVO;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Stateless
@LocalBean
@ScoringModule("AGE")
public class AgeModule implements CustomerScoringModule {
public AgeModule() {
super();
}
@Override
public ScoringModuleResponseVO process(ScoringRequestVO inputVO) {
log.info("processed AGE_MODULE");
return new ScoringModuleResponseVO();
}
}
@ScoringModule限定符:
package de.otto.cccs.customerscoring.modules.qualifiers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ScoringModule {
String value();
}
和注释文字:
package de.otto.cccs.customerscoring.modules.qualifiers;
import javax.enterprise.util.AnnotationLiteral;
public class ScoringModuleLiteral extends AnnotationLiteral<ScoringModule> implements ScoringModule {
private static final long serialVersionUID = 1L;
private String value;
public ScoringModuleLiteral(String value) {
this.value = value;
}
@Override
public String value() {
return this.value;
}
}
最后是我的空豆子。xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>