从Weld中的另一个罐子中注入一个bean

时间:2011-09-30 11:58:33

标签: jar cdi ear jboss-weld

我有两个罐子A和B,其中A取决于B。

Jar B只有一个类:

@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(更准确地说,是一个EJB jar)有一个bean:

@ApplicationScoped
public class AnotherManagedBean {

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

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

带有@Produces方法的配置bean:

@ApplicationScoped
public class ConfigurationBean {

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

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

但是,当我使用两个Jars部署EAR时,我遇到了这个异常:

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)

有什么想法吗?

由于

3 个答案:

答案 0 :(得分:27)

确保两个罐子都是“豆档案” - 即他们有META-INF/beans.xml

答案 1 :(得分:2)

我遇到了这个完全相同的问题,我能够把它搞清楚但是我用耳朵来组合罐子。

耳朵布局

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>

这样做使得one.jar可用于容器中的two.jar。

-kurt

答案 2 :(得分:0)

有一件事,您必须创建Qulifier注释以准确指定应注入的注释。

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

然后..

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

注射..

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

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