我有一个核心库,它有一个接口,我想在 Fuse ESB(Apache ServiceMix 和 Karaf)中将其公开为 OSGI 服务。目标是允许其他捆绑包使用它。该服务使用 JPA (OpenJPA) 和 Spring。下面是界面:
public interface PatientService {
public Patient find(Integer id);
}
和班级:
@Repository
public class PatientServiceJpaImpl implements PatientService {
@PersistenceContext(unitName="psu")
private EntityManager entityManager;
@Override
public Patient find(Integer id) {
return entityManager.find(Patient.class, id);
}
}
以下是缩写META-INF/spring/beans.xml
:
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<context:annotation-config />
<context:component-scan base-package="..." />
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="psu" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
和META-INF/persistence.xml
(也缩写):
<persistence xmlns="http://java.sun.com/xml/ns/persistence" ...>
<persistence-unit name="psu" transaction-type="RESOURCE_LOCAL">
<class>...</class>
</persistence>
在非 OSGi 环境中,一切正常。它使用 felix maven-bundle-plugin,所以为了创建 OSGi 服务,我添加了以下内容OSGI-INF/blueprint/osgi-context.xml
:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="patientService" class="com.test.service.PatientServiceJpaImpl" />
<service id="osgiPatientService" ref="patientService" interface="com.test.service.PatientService" />
</blueprint>
捆绑包部署成功并注册了服务。问题是当PatientService
从另一个包中引用时,实体管理器还没有被注入,因此NullPointerException
在find(Integer id)
方法中抛出了一个。以下是消费者的片段META-INF/spring/consumer-context.xml
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<bean id="patientServiceImpl" class="com.test.ws.PatientWebServiceImpl" >
<property name="patientService">
<osgi:reference interface="com.test.service.PatientService"/>
</property>
</bean>
...
</beans>
为了清楚起见,PatientService
被注入到消费者包中,但实体管理器没有被注入到提供者包中。此外,由于在启动原始服务时出现以下日志输出,因此持久性单元似乎不是问题:
125 psu TRACE [SpringOsgiExtenderThread-14] openjpa.Runtime - org.apache.openjpa.persistence.PersistenceProviderImpl@24a5031d creating container org.apache.openjpa.persistence.EntityManagerFactoryImpl@4d6f77b6 for PU psu.
为了了解发生了什么,我在PatientServiceJpaImpl
类的构造函数中记录了对象内存引用和堆栈跟踪。构造函数被调用了两次(创建了两个不同的对象):
第一个输出似乎源自 osgi 容器,从 开始,
org.apache.felix
或多或少以org.apache.aries.blueprint
.第二个输出似乎起源于 spring 框架,从 开始,
org.springframework.osgi
或多或少以org.springframework.beans.BeanUtils
.
当调用消费者服务时,它的引用是对蓝图实例化对象的引用,该对象没有注入的实体管理器。同样从日志中,持久化单元在对象的蓝图实例化之后被实例化PatientServiceJpaImpl
。
我已经搜索并修补了这个问题很长一段时间,但我已经没有想法了。具有讽刺意味的是,它实际上在某一时刻有效,但我做了很多改变才能让它发挥作用,以至于它是一个我无法成功退出的老鼠窝。
为什么蓝图托管对象中没有注入持久化上下文?任何想法将不胜感激。谢谢。