我有一个EAR应用程序(部署在Weblogic 12c上),它有一个“持久化”组件。“持久化”组件使用JPA (实现: EclipseLink)来持久化对象。
使用entityManager的bean被声明为@Stateless,实体管理器通过@PersistenceContext注释注入。
问题是,每次尝试访问entityManager时,我都有一个entityManager(意思是,他没有被正确注入)。
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyBean implements MyBeanLocal {
@PersistenceContext(unitName = "MyPersistenceUnit", type = PersistenceContextType.EXTENDED)
EntityManager entityManager;
public void insert(MyObject object) {
try {
entityManager.persist(object); //NullPointerException here
} catch (Exception e) {
e.printStackTrace();
}
}persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MyPersistenceUnit"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>MyDataSource</jta-data-source>
<class>com.myclasses.MyObject</class>>
</persistence-unit>
</persistence>我使用工厂来获取MyBean的实例
public class MyBeanFactory
{
public static MyBean create()
{
return new MyBean();
}
}我把它用在普通代码中:
MyBeanLocal bean = MyBeanFactory.create();MyBeanLocal是MyBean的接口,它是这样的
@Local(MyBeanLocal.class)
public interface MyBeanLocal {
public void insert(MyObject object);
}我试图通过MyBean注释注入@EJB实例,但它失败了(每当我试图使用据称被注入的MyBean实例时,NullPointerException就会失败)
当我的应用程序部署在Weblogic服务器上时,我既没有看到MyBean,也没有看到MyPersistenceUnit (我看到来自其他组件的MDB),尽管我可以看到部署> "MyDomain“>配置/设置>持久性下的MyPersistenceUnit。MyDataSource是好的,并连接。
这里有weblogic服务器的启动日志。
####<15 juil. 2015 09 h 52 CEST> <Notice> <Security> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946746252> <BEA-090082> <Security initializing using security realm myrealm.>
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771648> <BEA-000365> <Server state changed to STANDBY.>
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771650> <BEA-000365> <Server state changed to STARTING.>
####<15 juil. 2015 09 h 53 CEST> <Warning> <EJB> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946795598> <BEA-012035> <The Remote interface method: public abstract boolean com.myclasses.UtilityRemote.send(com.mytypes.ReqType,java.lang.String) in EJB UtilityBean contains a parameter of type com.mytypes.ReqType which is not serializable. Though the EJB IMMJMSUtilityBean has call-by-reference set to false, this parameter is not serializable and hence will be passed by reference. A parameter can be passed using call-by-value only if the parameter type is serializable.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <Log Management> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946808540> <BEA-170027> <The server has successfully established a connection with the Domain level Diagnostic Service.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809347> <BEA-000365> <Server state changed to ADMIN.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809503> <BEA-000365> <Server state changed to RESUMING.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809774> <BEA-002613> <Channel "Default[1]" is now listening on 127.0.0.1:7001 for protocols iiop, t3, ldap, snmp, http.>
####<15 juil. 2015 09 h 53 CEST> <Warning> <Server> <MyUsername> <AdminServer> <DynamicListenThread[Default]> <<WLS Kernel>> <> <> <1436946809774> <BEA-002611> <The hostname "MyUsername.MyProxy.com", maps to multiple IP addresses: 172.23.247.5, 0:0:0:0:0:0:0:1.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default[2]" is now listening on 0:0:0:0:0:0:0:1:7001 for protocols iiop, t3, ldap, snmp, http.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default" is now listening on 172.23.247.5:7001 for protocols iiop, t3, ldap, snmp, http.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809776> <BEA-000331> <Started the WebLogic Server Administration Server "AdminServer" for domain "test" running in development mode.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810276> <BEA-000360> <The server started in RUNNING mode.>
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810280> <BEA-000365> <Server state changed to RUNNING.> 我怀疑问题在于weblogic服务器没有正确处理这个bean,但是我不知道如何修复它。
如果有人能帮我,我会很感激的。如果需要的话,可以向我询问更多的代码/配置/上下文。
发布于 2015-07-15 12:05:50
这里的问题是如何获得EJB引用--如果您自己使用"new“构造实例,那么您将得到的只是一个POJO而不是EJB --这意味着您将无法访问任何EJB服务,比如依赖注入和事务管理。
要获得真正的EJB,您应该使用在初始上下文中查找bean
MyBeanRemote bean = (MyBeanRemote) new InitialContext().lookup("MyBean/remote");https://stackoverflow.com/questions/31343122
复制相似问题