是否可以在Glassfish v3上使用CDI将EJB 3.1 bean注入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
所以看起来@Inject成为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
Unpacked EAR结构:
│ 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
答案 0 :(得分:6)
我对CDI不是很熟悉,但我想如果你自己创建POJO对象,就不会发生注入。你试过了吗?
@Inject
private MyPOJO p;
而不是:
MyPOJO p = new MyPOJO();
答案 1 :(得分:2)
我能够使用@EJB或@Inject在非EJB类(如Servlet和DAO)中注入EJB(和其他资源)。通常你会使用@EJB注入一个ejb和@Inject来注入一个非ejb资源。
另外,不要忘记“beans.xml”文件,这需要激活CDI:
http://download.oracle.com/javaee/6/tutorial/doc/gjbnz.html
我认为这可能是你的问题。实际上缺少这个文件会使CDI失效,但是你仍然只是将注入的东西注入到你的EJB中,因为这甚至可以在Java EE的前CDI版本中实现。