Injecting a bean from a different Jar in Weld

我与影子孤独终老i 提交于 2019-11-30 17:15:30

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!