简体   繁体   中英

Inject a bean into EJB, which has been implemented in a different module - WAR

I have the following structure:

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

The JAR contains several interfaces and qualifiers. These interfaces and qualifiers will be used in other modules - the WAR and EJB.

The WAR has the JAR as a dependency and a class implements a particular interface. That class is annotated with a qualifier. Both - the interface and qualifier come from the JAR.

The EJB has the JAR as a dependency and i want it to be able to inject the bean that the WAR has implemented.

But it doesn't work. Crashes with error:

Caused by: org.jboss.weld.exceptions.WeldException: WELD-001602: Cannot create qualifier instance model for @com.api.QualifierWithParams()

Again, @com.api.QualifierWithParams() comes from the JAR, which has been imported.

I presume that the runtime is unaware of the cdi beans between the modules?

Now the whole reason i'm doing this is because the EJB can be @Singleton which will be accessed by whatever number of instances of Servlets there are. Use it sort like a cache. I guess i can duplicate some code, but rather not. Is there a way?

EDIT - took me a while to strip down code, but here it is:

/*
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"));

        //...
    }
}

Stack trace:

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

Further tests showed that if i inject it as an Instance, it will work:

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

But then i have to manually add the qualifier:

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

The qualifier (QualifierWithParams) itself must be manually implemented:

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

But that doesn't work with InjectionPoint, so in the code in the OP this:

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

will be null...

So i guess i will look into alternative ways. I will probably do a @ApplicationScoped CDI bean in place of a @Singleton EJB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM