在 Weld 中从不同的 Jar 中注入 Bean

发布于 2024-12-07 06:40:37 字数 4568 浏览 3 评论 0原文

我有两个 Jars 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";
 }
}

但是,当我部署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)

有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-12-14 06:40:37

确保两个 jar 都是“bean 档案” - 即它们有 META-INF/beans.xml

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

柳若烟 2024-12-14 06:40:37

我遇到了同样的问题,我能够解决它,但我用耳朵来组合罐子。

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>

这样做使得容器中的 one.jar 可供 Two.jar 使用。

-库尔特

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

£噩梦荏苒 2024-12-14 06:40:37

一件事是,您必须创建 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文