Injecting a bean from a different Jar in Weld

前端 未结 3 686
轮回少年
轮回少年 2021-01-04 00:43

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

Jar B has a single class:

@ApplicationScoped
public class MyMa         


        
相关标签:
3条回答
  • 2021-01-04 01:01

    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

    0 讨论(0)
  • 2021-01-04 01:09

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

    0 讨论(0)
  • 2021-01-04 01:19

    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

    0 讨论(0)
提交回复
热议问题