【发布时间】:2025-12-07 14:50:02
【问题描述】:
我有一个核心库,它有一个接口,我想在 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>
bundle 部署成功,服务注册成功。问题是当PatientService被另一个bundle引用时,实体管理器还没有被注入,因此在find(Integer id)方法中抛出了NullPointerException。以下是消费者META-INF/spring/consumer-context.xml的sn-p:
<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对象的蓝图实例化之后实例化的。
我已经对这个问题进行了很长时间的搜索和修改,但我已经没有想法了。具有讽刺意味的是,它实际上在某个时候有效,但我做了很多改变才能让它发挥作用,以至于它是一个我无法成功退出的老鼠窝。
为什么没有在蓝图托管对象中注入持久化上下文?任何想法将不胜感激。谢谢。
【问题讨论】:
标签: jpa osgi apache-karaf blueprint-osgi