Hibernate 卡拉夫+;JPA:如何使用blueprint.xml获取具有注入属性的实例?

Hibernate 卡拉夫+;JPA:如何使用blueprint.xml获取具有注入属性的实例?,hibernate,jpa,osgi,apache-karaf,blueprint,Hibernate,Jpa,Osgi,Apache Karaf,Blueprint,hibernate官方文档中的示例 <bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl"> <jpa:context unitname="data-point" property="entityManager"/> <tx:transaction method="*" value="Required"/> </bean> <s

hibernate官方文档中的示例

 <bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl">
    <jpa:context unitname="data-point" property="entityManager"/>
    <tx:transaction method="*" value="Required"/>
  </bean>
  <service ref="dpService" interface="org.hibernate.osgitest.DataPointService"/>
但现在我明白了

错误:Bundle org.apache.aries.jpa.support[204]EventDispatcher:调度期间出错。(java.lang.ClassCastException:org.apache.aries.transaction.internal.AriesTransactionManagerImpl不能强制转换为javax.transaction.TransactionManager) java.lang.ClassCastException:org.apache.aries.transaction.internal.AriesTransactionManagerImpl不能强制转换为javax.transaction.TransactionManager


通过OSGi服务,您不会“获取”Impl,您“仅”通过其API检索服务提供者,在您的情况下,该API就是DataPointService

如果这是你想要的,那就直截了当地说:

<reference id="pointService" interface="org.hibernate.osgitest.DataPointService"/>

这将为您提供该服务的参考。如果希望直接访问entitymanager,请对entitymanager接口执行相同的操作

要在启动时执行某些操作,您需要定义在bean完全初始化后要调用的init方法:

<bean id="myConsumingBean" init-method="init">
    <property name="dataPointService" ref="pointService">
</bean>

可能还想看看

更新:
您看到的错误可能是由系统可用的两个不同接口造成的。检查哪些捆绑包实际提供此接口,或者此接口是否位于您自己的捆绑包中

如果您依赖maven项目中提供的javax.persistence.EntityManager和scope compile,则可能会出现后一种情况。范围编译(如果未设置,则为默认值)通常会将这些依赖项嵌入到您自己的包中。确保将其设置为提供的范围,这将解决您的问题

更新2:
请不要为此使用捆绑激活器,不能将blueprint和捆绑激活器混合使用。 您应该为此使用std.Java类。基本上blueprint的行为类似于spring,因此blueprint.xml有点像bundle激活器。
因为如果将两者混合使用,则会导致无法正确创建类

通过OSGi服务,您不会“获取”Impl,您“仅”通过其API检索服务提供商,在您的情况下,该API就是DataPointService

如果这是你想要的,那就直截了当地说:

<reference id="pointService" interface="org.hibernate.osgitest.DataPointService"/>

这将为您提供该服务的参考。如果希望直接访问entitymanager,请对entitymanager接口执行相同的操作

要在启动时执行某些操作,您需要定义在bean完全初始化后要调用的init方法:

<bean id="myConsumingBean" init-method="init">
    <property name="dataPointService" ref="pointService">
</bean>

可能还想看看

更新:
您看到的错误可能是由系统可用的两个不同接口造成的。检查哪些捆绑包实际提供此接口,或者此接口是否位于您自己的捆绑包中

如果您依赖maven项目中提供的javax.persistence.EntityManager和scope compile,则可能会出现后一种情况。范围编译(如果未设置,则为默认值)通常会将这些依赖项嵌入到您自己的包中。确保将其设置为提供的范围,这将解决您的问题

更新2:
请不要为此使用捆绑激活器,不能将blueprint和捆绑激活器混合使用。 您应该为此使用std.Java类。基本上blueprint的行为类似于spring,因此blueprint.xml有点像bundle激活器。
因为如果将两者混合使用,则会导致无法正确创建类

我这样做了,但是在Activator onStart()中,我仍然得到null
ServiceReference dataPointServiceReference=bundleContext.getServiceReference(“pointService”);ServiceReference entityManagerServiceReference=bundleContext.getServiceReference(“myEntityManager”)erf。。。您不能将blueprint和经典OSGi激活剂混合使用。。。也许您应该为您的问题添加更多细节?我需要在bundle start上使用Hibernate访问数据库,然后您需要向您的服务消费bean添加init函数,该函数将在注入依赖项后调用。我感谢您的帮助。我通过卸载双子座的蓝图功能并只保留白羊座的蓝图来摆脱这个异常。现在正在调用init方法,但我的entityManager为null我这样做了
,但在Activator onStart()中,我仍然得到null
ServiceReference dataPointServiceReference=bundleContext.getServiceReference(“pointService”);ServiceReference entityManagerServiceReference=bundleContext.getServiceReference(“myEntityManager”)erf。。。您不能将blueprint和经典OSGi激活剂混合使用。。。也许您应该为您的问题添加更多细节?我需要在bundle start上使用Hibernate访问数据库,然后您需要向您的服务消费bean添加init函数,该函数将在注入依赖项后调用。我感谢您的帮助。我通过卸载双子座的蓝图功能并只保留白羊座的蓝图来摆脱这个异常。现在正在调用init方法,但我的entityManager为null