无法在外部 Jar 文件中注入实体管理器

发布于 2025-01-07 07:00:09 字数 2085 浏览 0 评论 0原文

我正在开发一个单独的java组件,它将打包为jar并添加到父项目中。将其作为组件的原因是因为明天我可能必须删除该代码并丢弃它。 我面临的问题是正在开发的独立组件(jar 文件)需要在有人调用其 API 时查询数据库。

我的父项目是使用 Spring + Hibernate + JPA 设置的。

我无法将 EntityManager 从父项目注入到在 spring 容器启动期间实例化的外部 Jar 中。

这在概念上正确吗?或者可以用这种方式做到这一点吗?

我希望 jar 文件仅搭载在父项目定义的 persistence.xml 上,并利用启动期间加载的 EntityManager。

编辑 实体管理器是通过主Web应用程序中的LocalContainerEntityManagerFactoryBean以这种方式注入的。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSourceWrapper" />
    <property name="persistenceUnitName" value="LineManagement" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="false" />
            <property name="databasePlatform"  value="${hibernate.dialect}" />
        </bean>
    </property>
    <property name="beanName" value="entityManager"></property>
</bean>

父Web应用程序中的abstractDAO是按以下方式定义的,PersistenceContext注释确实返回了EntityManager,一切正常。

public abstract class AbstractDAO<T extends LMEntity> {

   @PersistenceContext(unitName = "LineManagement")
   protected EntityManager entityManager;

   @Autowired
   private DataSource dataSource;

   @Autowired
   private DAOSupport daoSupport;

   public void initHibernateStatistics() {
      HibernateStatistics.enableHibernateStatistics(entityManager);
   }

下面是jar文件中的DAO我希望在其中注入 EntityManager,以便它可以使用已经提供的 DAO

这是 jar 文件中的 DAO

@Repository(value = "retryDAO")
public class RetryDAOImpl
  implements RetryDAO {

   @Autowired
   private EntityManager entityManager;

   @Transactional
   public void saveEvent(final IntegrationEvent event) {
      entityManager.merge(event);
   }

我尝试了 @Autowired 注释,它提到entityManager bean 不是 展示。

i am in the process of developing a seperate java component which will be packaged as a jar and added to the parent project. the reason for making this as a component is because tomorrow i might have to remove that code and discard it.
The problem that i am facing is the standalone component,(jar file) which is being developed needs to query the database when someone calls its API.

my parent project is setup using Spring + Hibernate + JPA.

I am not able to inject the EntityManager from the parent project into the external Jar which is instantiated during the startup of the spring container.

Is this conceptually correct? or is it possible to do it in this fashion?

i want the jar file to just piggyback on the persistence.xml which is defined by the parent project and make use of the EntityManager which was loaded during startup.

EDIT
The entityManager is injected through the LocalContainerEntityManagerFactoryBean in the main webapp in this fashion

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSourceWrapper" />
    <property name="persistenceUnitName" value="LineManagement" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="false" />
            <property name="databasePlatform"  value="${hibernate.dialect}" />
        </bean>
    </property>
    <property name="beanName" value="entityManager"></property>
</bean>

The abstractDAO in the parent webapp is defined in the following manner, and the PersistenceContext annotation does give back the EntityManager and everything works fine..

public abstract class AbstractDAO<T extends LMEntity> {

   @PersistenceContext(unitName = "LineManagement")
   protected EntityManager entityManager;

   @Autowired
   private DataSource dataSource;

   @Autowired
   private DAOSupport daoSupport;

   public void initHibernateStatistics() {
      HibernateStatistics.enableHibernateStatistics(entityManager);
   }

Below is the DAO in the jar file where i want the EntityManager to be injected so that it can use the one already provided

This is the DAO in the jar file

@Repository(value = "retryDAO")
public class RetryDAOImpl
  implements RetryDAO {

   @Autowired
   private EntityManager entityManager;

   @Transactional
   public void saveEvent(final IntegrationEvent event) {
      entityManager.merge(event);
   }

I tried the @Autowired annotation and it mentions that the entityManager bean isnt present.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2025-01-14 07:00:09

只要您满足以下条件,就可以实现:

  • 在您的父应用程序和您的组件之间共享相同的 Spring 上下文
  • ,或者您的应用程序上下文是组件 Spring 上下文的父级。

此外,您需要确保 Spring 实际上实例化您的组件,以便它可以注入任何必要的内容。如果您使用基于注释的配置,这可能涉及组件扫描(也就是说,您需要在 applicationContext.xml 中指定 代码>)。如果您使用传统的 XML 配置,,然后在 applicationContext.xml 中声明您的持久性组件就足够了。

绝对不行的是为您的父应用程序和 JAR 中的组件使用完全独立的上下文。

It is possible as long as you:

  • share the same Spring context between your parent application and your component
  • or your application context is the parent of your component Spring context.

Furthermore, you need to make sure that Spring does in fact instantiate your component so that it can inject whatever is necessary. If you are using annotation-based configuration, this might involve component scanning (that is, you would need to specify <context:component-scan /> in your applicationContext.xml). If you are using the traditional XML configuration, <context:annotation-config /> and then declaring your persistence components in the applicationContext.xml should be enough.

What would definitely not work is using completely independent contexts for your parent application and your component in the JAR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文