2011-11-17 55 views
2

是否可以使用Glassfish v3上的CDI將EJB 3.1 bean注入到POJO中?CDI將EJB注入到Glassfish v3上的POJO

我的類(在EJB模塊):

@Singleton 
@LocalBean 
@Startup 
@Named 
public class NewSingletonBean { 

    @PostConstruct 
    public void init(){ 
     System.out.println("NewSingletonBean INIT"); 
    } 

} 

_

@Singleton 
@LocalBean 
@Startup 
@DependsOn(value="NewSingletonBean") 
public class NewSingletonBean2 { 

    @Inject NewSingletonBean newSingletonBean; 

    @PostConstruct 
    public void init(){ 
     System.out.println("NewSingletonBean2 INIT"); 
     System.out.println("EJB injected into EJB: " + (newSingletonBean != null)); 
     MyPOJO p = new MyPOJO(); 
     p.sth(); 
    } 

} 

_

public class MyPOJO { 
     @Inject NewSingletonBean newSingletonBean; 

     public void sth(){ 
      System.out.println("EJB injected into POJO: " + (newSingletonBean != null)); 
     } 
} 

服務器輸出:

Launching GlassFish on Felix platform 
INFO: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry 
INFO: Grizzly Framework 1.9.31 started in: 31ms - bound to [0.0.0.0:4848] 
INFO: Grizzly Framework 1.9.31 started in: 109ms - bound to [0.0.0.0:8080] 
INFO: Grizzly Framework 1.9.31 started in: 62ms - bound to [0.0.0.0:8181] 
INFO: Grizzly Framework 1.9.31 started in: 141ms - bound to [0.0.0.0:3700] 
INFO: Grizzly Framework 1.9.31 started in: 0ms - bound to [0.0.0.0:7676] 
INFO: GlassFish Server Open Source Edition 3.1 (43) startup time : Felix (2 812ms), startup services(1 172ms), total(3 984ms) 
INFO: JMXStartupService: Started JMXConnector, JMXService URL = service:jmx:rmi://9.167.213.195:8686/jndi/rmi://9.167.213.195:8686/jmxrmi 
INFO: Hibernate Validator 4.1.0.Final 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: Grizzly Framework 1.9.31 started in: 16ms - bound to [0.0.0.0:8080] 
INFO: Grizzly Framework 1.9.31 started in: 16ms - bound to [0.0.0.0:8181] 
INFO: SEC1002: Security Manager is OFF. 
INFO: SEC1010: Entering Security Startup Service 
INFO: SEC1143: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper. 
INFO: SEC1115: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created. 
INFO: SEC1115: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created. 
INFO: SEC1115: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created. 
INFO: SEC1011: Security Service(s) Started Successfully 
INFO: WEB0169: Created HTTP listener [http-listener-1] on host/port [0.0.0.0:8080] 
INFO: WEB0169: Created HTTP listener [http-listener-2] on host/port [0.0.0.0:8181] 
INFO: WEB0169: Created HTTP listener [admin-listener] on host/port [0.0.0.0:4848] 
INFO: WEB0171: Created virtual server [server] 
INFO: WEB0171: Created virtual server [__asadmin] 
INFO: WEB0172: Virtual server [server] loaded default web module [] 
INFO: Portable JNDI names for EJB NewSingletonBean2 : [java:global/CDITest/CDITest-ejb/NewSingletonBean2!tries.NewSingletonBean2, java:global/CDITest/CDITest-ejb/NewSingletonBean2] 
INFO: Portable JNDI names for EJB NewSingletonBean : [java:global/CDITest/CDITest-ejb/NewSingletonBean!tries.NewSingletonBean, java:global/CDITest/CDITest-ejb/NewSingletonBean] 
INFO: WELD-000900 ${parsedVersion (osgiVersion}) 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: NewSingletonBean INIT 
INFO: NewSingletonBean2 INIT 
INFO: EJB injected into EJB: true 
INFO: EJB injected into POJO: false 
INFO: WEB0671: Loading application [CDITest#CDITest-war.war] at [CDITest-war] 
INFO: CDITest was successfully deployed in 3 531 milliseconds. 

注線:

INFO: EJB injected into EJB: true 
INFO: EJB injected into POJO: false 

所以它看起來像@注入到EJB工作正常,但到POJO沒有。我究竟做錯了什麼? 我試過GlassFish 3.1和3.0.1(使用NetBeans 7.0.1)。

編輯: 我正在使用NetBeans 7.0.1。 dist目錄結構:

│ CDITest.ear 
│ 
└───gfdeploy 
    └───CDITest 
     ├───CDITest-ejb_jar 
     │ │ .netbeans_automatic_build 
     │ │ .netbeans_update_resources 
     │ │ 
     │ ├───META-INF 
     │ │  beans.xml 
     │ │  MANIFEST.MF 
     │ │ 
     │ └───tries 
     │   MyPOJO.class 
     │   NewSingletonBean.class 
     │   NewSingletonBean2.class 
     │ 
     ├───CDITest-war_war 
     │ │ index.jsp 
     │ │ 
     │ ├───META-INF 
     │ │  MANIFEST.MF 
     │ │ 
     │ └───WEB-INF 
     │  └───classes 
     │    .netbeans_automatic_build 
     │    .netbeans_update_resources 
     │ 
     └───META-INF 
       MANIFEST.MF 

解包耳結構:

│ CDITest-ejb.jar 
│ CDITest-war.war 
│ 
└───META-INF 
     MANIFEST.MF 

未包裝的EJB模塊JAR結構:

├───META-INF 
│  beans.xml 
│  MANIFEST.MF 
│ 
└───tries 
     MyPOJO.class 
     NewSingletonBean.class 
     NewSingletonBean2.class 

回答

5

我對CDI不是很熟悉,但我想如果你自己創建POJO對象,不會發生注入。你嘗試:

@Inject 
private MyPOJO p; 

相反的:

MyPOJO p = new MyPOJO(); 

+0

你說得對。它與'@Inject private MyPOJO p;'那麼它是正確的行爲? – zacheusz

+2

@zacheusz如果你創建了你自己的類的實例,容器不負責它的生命週期 - 它不是它的責任,因此不會發生注入。 –

+0

@PiotrNowicki非常感謝:) – zacheusz

2

嗯,我能夠注入的EJB(和其他資源)的非EJB類(如Servlets和DAO)使用@EJB或@Inject。通常你會使用@EJB來注入一個ejb和@Inject來注入一個非ejb資源。

另外,不要忘了「的beans.xml」的文件,這是需要激活CDI:

http://download.oracle.com/javaee/6/tutorial/doc/gjbnz.html

我認爲這可能是你的問題。事實上,丟失這個文件會使CDI無效,但是您仍然將東西注入到您的EJB中,這是因爲即使在Java EE的前CDI版本中,這也是可能的。

+0

謝謝。我在EJB模塊jar歸檔的META-INF目錄中有beans.xml。如果我在POJO中使用@EJB:'public class MyPOJO {@EJB NewSingletonBean newSingletonBean;'它不起作用。 – zacheusz

+0

@Shivan我也嘗試過beans.xml,但仍然爲空。 – user2771655