将bean注入EJB,它已在不同的模块中实现 - WAR

时间:2017-06-10 22:35:27

标签: java java-ee ejb cdi

我有以下结构:

EAR
|
| - EJB
| - WAR (servlet)
| - JAR (api)

JAR包含多个接口和限定符。这些接口和限定符将用于其他模块 - WAR和EJB。

WAR将JAR作为依赖项,并且类实现特定接口。该类使用限定符进行注释。两者 - 接口和限定符都来自JAR。

EJB将JAR作为依赖项,我希望它能够注入WAR已实现的bean。

但它不起作用。崩溃时出错:

  

引起:org.jboss.weld.exceptions.WeldException:WELD-001602:   无法为其创建限定符实例模型   @ com.api.QualifierWithParams()

同样,@ com.api.QualifierWithParams()来自已导入的JAR。

我认为运行时不知道模块之间的cdi bean吗?

现在,我这样做的全部原因是因为EJB可以是@Singleton,它可以被任何数量的Servlet实例访问。使用它像缓存一样排序。我想我可以复制一些代码,但不是。有办法吗?

编辑 - 花了一些时间来删除代码,但现在是:

/*
EJB:

In maven this will have in its pom.xml's dependencies a dependency element for the JAR below.

*/

@Local
public interface EJBInterface {
    public String someBusinessMethod (String s);
}

@Singleton(name="EBJImplementation")
@javax.ejb.Startup
@Local(EJBInterface.class)
public class EBJImplementation implements EJBInterface{

    @javax.inject.Inject @com.api.QualifierWithParams(type=SomeType.PRIMARY, someQualifierParameter=15) InjectableBeanInterface b;

    @Override
    public String someBusinessMethod (String s){
        return s + " with param value of: " + Integer.parseInt(b.getParam());
    }

    /*
    @PostConstruct
    private void setUp(){

    }
    */
}

////////////////////////////////////////////

JAR:

package com.api;
public interface InjectableBeanInterface {

    public int getParam();
}

package com.api;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface QualifierWithParams {
    SomeType type() default SomeType.PRIMARY; //just an enum to help  reduce annotation explosion

    @Nonbinding int someQualifierParameter() default 0;
}


////////////////////////////////////////////

/*
WAR:

Like the EJB, it will include JAR file as a dependency

*/

public class BeanImpl implements com.api.InjectableBeanInterface{

    private int someQualifierParameter;

    public BeanImpl (int p){
        someQualifierParameter = p;
    }

    @Override
    public int getParam(){return someQualifierParameter == null ? -1 : someQualifierParameter;}
}


public class InjectableBeanFactory {

    @Produces
    @com.api.QualifierWithParams(type=SomeType.PRIMARY)
    public com.api.InjectableBeanInterface productionFact(InjectionPoint ip){
        //get the parameter from the qualifier and create new instance
        Annotated a = ip.getAnnotated();
        com.api.QualifierWithParams qualifierParams = a.getAnnotation(com.api.QualifierWithParams.class);

        int tempParam = qualifierParams.someQualifierParameter();
        return new BeanImpl(tempParam);
    }
}

//Servlet. Configuration is done in the web.xml . This is the entry point of the application.
public class Main extends HttpServlet{

    private @EJB EJBInterface b;

    @Override
    protected void doGet(HttpServletRequest rq, HttpServletResponse rs) throws ServletException, IOException {

        Logger.getLogger("main-logger").log(Level.INFO, b.someBusinessMethod("called from servlet"));

        //...
    }
}

堆栈追踪:

Failed to start service jboss.deployment.unit."test.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."test".WeldStartService: Failed to start service
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.weld.exceptions.WeldException: WELD-001602: Cannot create qualifier instance model for @com.api.QualifierWithParams(type=PRIMARY, someQualifierParameter=15)
        at com.api.QualifierWithParams.type(QualifierWithParams.java:0)
  StackTrace:
        at org.jboss.weld.resolution.QualifierInstance.createValues(QualifierInstance.java:139)
        at org.jboss.weld.resolution.QualifierInstance.of(QualifierInstance.java:96)
        at org.jboss.weld.resolution.ResolvableBuilder.addQualifier(ResolvableBuilder.java:147)
        at org.jboss.weld.resolution.ResolvableBuilder.addQualifiers(ResolvableBuilder.java:197)
        at org.jboss.weld.resolution.ResolvableBuilder.<init>(ResolvableBuilder.java:83)
        at org.jboss.weld.manager.BeanManagerImpl.getBeans(BeanManagerImpl.java:567)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:357)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
        at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
        at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
        at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
        at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
        at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:748)
        at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.jboss.weld.resolution.QualifierInstance.createValues(QualifierInstance.java:137)
        ... 19 more

1 个答案:

答案 0 :(得分:0)

进一步的测试显示,如果我将其作为实例注入,它将起作用:

@Inject @Any javax.enterprise.inject.Instance<InjectableBeanInterface> d;

但是我必须手动添加限定符:

d.select(new QualifierWithParamsIMPL(SomeType.PRIMARY, 15)).get();

必须手动实现限定符(QualifierWithParams)本身:

public class QualifierWithParamsIMPL extends AnnotationLiteral<QualifierWithParams> implements QualifierWithParams {

    private final SomeType type;
    private final int someQualifierParameter;

    public CMSdbConnectionIMPL(SomeType type, int someQualifierParameter){
        this.type = type;
        this.someQualifierParameter = someQualifierParameter;
    }

    @Override
    public String type() {return type;}
    @Override
    public String someQualifierParameter() {return someQualifierParameter;}
}

但这不适用于InjectionPoint,因此在OP中的代码中:

com.api.QualifierWithParams qualifierParams = a.getAnnotation(com.api.QualifierWithParams.class);

将为null ...

所以我想我会研究其他方法。我可能会用@ApplicationScoped CDI bean代替@Singleton EJB。