Injecting a bean from a different Jar in Weld
Asked Answered
B

3

21

I have two Jars A and B where A depends on B.

Jar B has a single class:

@ApplicationScoped
public class MyManagedBean {

 private String user;

 public MyManagedBean(){
  //Constructor necesary for CDI
 }

 @Inject
 public MyManagedBean(@Named("user") String user){
  this.user = user;
 }

 ...
}

Jar A (more precisely, an EJB jar) has a bean:

@ApplicationScoped
public class AnotherManagedBean {

 public AnotherManagedBean(){
  //Constructor necesary for CDI
 }

 @Inject
 public AnotherManagedBean(MyManagedBean bean){
  ...
 }
}

And a configuration bean with a @Produces method:

@ApplicationScoped
public class ConfigurationBean {

 public ConfigurationBean(){
  //Constructor necesary for CDI
 }

 @Produces
 @Named("user")
 public String getUser(){
  return "myUser";
 }
}

However, when I deploy an EAR with both Jars I'm getting this exception:

SEVERE: Exception while loading the app : WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [@Named] at injection point [[parameter 1] of [constructor] @Inject public com.example.MyManagedBean(String)]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [@Named] at injection point [[parameter 1] of [constructor] @Inject public com.example.MyManagedBean(String)]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:270)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129)
    at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351)
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336)
    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:396)
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:190)
    at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:306)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:462)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:382)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1064)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1244)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1232)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:459)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:209)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:238)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)

Any idea?

Thanks

Brachylogy answered 30/9, 2011 at 11:58 Comment(2)
I believe this is correct answer to your problem https://mcmap.net/q/660046/-cdi-using-interceptors-across-different-modules-bean-archivesOccur
i have the exact same problem let me know if you find a solution.Siliculose
N
32

Make sure both jars are "bean archives" - i.e. they have META-INF/beans.xml

Nedneda answered 30/9, 2011 at 19:35 Comment(1)
Yes, both jars have META-INF/beans.xmlBrachylogy
P
2

I was having this exact same problem and I was able to get it figured out but I use an ear to combine the jars.

Ear Layout

project.ear
|-- META-INF 
|      |-- MANIFEST.MF
|      |-- application.xml*
|-- one.jar (bean archive)
|      |-- META-INF
|      |      |-- beans.xml
|      |-- <code>
|-- two.jar (ejb)
  • application.xml

<application>
  <display-name>test-application</display-name>
  <module>
    <ejb>two.jar</ejb>
  </module>
  <module>
    <java>one.jar</java>
  </module>
</application>

Doing that it made one.jar available to two.jar in the container.

-kurt

Pytlik answered 16/11, 2011 at 18:58 Comment(0)
M
0

One thing, you have to create Qulifier annotation for specify exactly which should be injected.

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface UserConfiguration { }

and then..

 @Produces
 @UserConfiguration
 @Named("user")
 public String getUser(){
  return "myUser";
 }

for injection..

@Inject
public MyManagedBean(@UserConfiguration String user){
    this.user = user;
}

see also http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#d0e1355

Mohenjodaro answered 4/10, 2011 at 4:36 Comment(1)
Without a Qualifier annotation, it should have the @Default qualifier... Isn't it?Brachylogy

© 2022 - 2024 — McMap. All rights reserved.