从Weld中的另一个罐子中注入一个bean [英] Injecting a bean from a different Jar in Weld

查看:113
本文介绍了从Weld中的另一个罐子中注入一个bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

罐子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:

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){
  ...
 }
}

带有@Produces方法的配置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";
 }
}

但是,当我使用两个Jars部署EAR时得到此异常:

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)

任何想法?

谢谢

推荐答案

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

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

这篇关于从Weld中的另一个罐子中注入一个bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆