如何让 spring 注入我的 EntityManager?

发布于 2024-08-28 02:18:49 字数 5355 浏览 6 评论 0原文

我正在遵循此处的指南,但是当 DAO 执行时,EntityManagernull

我已经尝试了在指南的评论、各种论坛和此处(包括 这个),无济于事。无论我做什么,EntityManager 仍然是null

以下是相关文件,其中包等已更改以保护无辜者。

spring-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        xmlns:p="http://www.springframework.org/schema/p">

     <context:component-scan base-package="com.group.server"/>
     <context:annotation-config/>
     <tx:annotation-driven/>

     <bean id="propertyPlaceholderConfigurer"
           class="com.group.DecryptingPropertyPlaceholderConfigurer"
           p:systemPropertiesModeName="SYSTEM_PROPERTIES_MODE_OVERRIDE">
         <property name="locations">
             <list>
                 <value>classpath*:spring-*.properties</value>
                 <value>classpath*:${application.environment}.properties</value>
             </list>
         </property>
     </bean>

     <bean id="orderDao" class="com.package.service.OrderDaoImpl"/>

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="persistenceUnitName" value="MyServer"/>
         <property name="loadTimeWeaver">
             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
         </property>
         <property name="dataSource" ref="dataSource"/>
         <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                 <property name="showSql" value="${com.group.server.vendoradapter.showsql}"/>
                 <property name="generateDdl" value="${com.group.server.vendoradapter.generateDdl}"/>
                 <property name="database" value="${com.group.server.vendoradapter.database}"/>
             </bean>
         </property>
     </bean>

     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory"/>
         <property name="dataSource" ref="dataSource"/>
     </bean>

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="${com.group.server.datasource.driverClassName}"/>
         <property name="url" value="${com.group.server.datasource.url}"/>
         <property name="username" value="${com.group.server.datasource.username}"/>
         <property name="password" value="${com.group.server.datasource.password}"/>
     </bean>

     <bean id="executorService" class="java.util.concurrent.Executors" factory-method="newCachedThreadPool"/>

 </beans>

persistence.xml

<persistence  xmlns="http://java.sun.com/xml/ns/persistence"  version="1.0">
    <persistence-unit name="MyServer" transaction-type="RESOURCE_LOCAL"/>
</persistence>

OrderDaoImpl

 package com.group.service;

 import com.group.model.Order;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;

 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import java.util.List;

 @Repository
 @Transactional
 public class OrderDaoImpl implements OrderDao {

     private EntityManager entityManager;

     @PersistenceContext
     public void setEntityManager(EntityManager entityManager) {
         this.entityManager = entityManager;
     }

     @Override
     public Order find(Integer id) {
         Order order = entityManager.find(Order.class, id);
         return order;
     }

     @Override
     public List<Order> findAll() {
         Query query = entityManager.createQuery("select o from Order o");
         return query.getResultList();
     }

     @Override
     public List<Order> findBySymbol(String symbol) {
         Query query = entityManager.createQuery("select o from Order o where o.symbol = :symbol");
         return query.setParameter("symbol", symbol).getResultList();
     }
 }

I'm following the guide here, but when the DAO executes, the EntityManager is null.

I've tried a number of fixes I found in the comments on the guide, on various forums, and here (including this), to no avail. No matter what I seem to do the EntityManager remains null.

Here are the relevant files, with packages etc changed to protect the innocent.

spring-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        xmlns:p="http://www.springframework.org/schema/p">

     <context:component-scan base-package="com.group.server"/>
     <context:annotation-config/>
     <tx:annotation-driven/>

     <bean id="propertyPlaceholderConfigurer"
           class="com.group.DecryptingPropertyPlaceholderConfigurer"
           p:systemPropertiesModeName="SYSTEM_PROPERTIES_MODE_OVERRIDE">
         <property name="locations">
             <list>
                 <value>classpath*:spring-*.properties</value>
                 <value>classpath*:${application.environment}.properties</value>
             </list>
         </property>
     </bean>

     <bean id="orderDao" class="com.package.service.OrderDaoImpl"/>

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="persistenceUnitName" value="MyServer"/>
         <property name="loadTimeWeaver">
             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
         </property>
         <property name="dataSource" ref="dataSource"/>
         <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                 <property name="showSql" value="${com.group.server.vendoradapter.showsql}"/>
                 <property name="generateDdl" value="${com.group.server.vendoradapter.generateDdl}"/>
                 <property name="database" value="${com.group.server.vendoradapter.database}"/>
             </bean>
         </property>
     </bean>

     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory"/>
         <property name="dataSource" ref="dataSource"/>
     </bean>

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="${com.group.server.datasource.driverClassName}"/>
         <property name="url" value="${com.group.server.datasource.url}"/>
         <property name="username" value="${com.group.server.datasource.username}"/>
         <property name="password" value="${com.group.server.datasource.password}"/>
     </bean>

     <bean id="executorService" class="java.util.concurrent.Executors" factory-method="newCachedThreadPool"/>

 </beans>

persistence.xml

<persistence  xmlns="http://java.sun.com/xml/ns/persistence"  version="1.0">
    <persistence-unit name="MyServer" transaction-type="RESOURCE_LOCAL"/>
</persistence>

OrderDaoImpl

 package com.group.service;

 import com.group.model.Order;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;

 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import java.util.List;

 @Repository
 @Transactional
 public class OrderDaoImpl implements OrderDao {

     private EntityManager entityManager;

     @PersistenceContext
     public void setEntityManager(EntityManager entityManager) {
         this.entityManager = entityManager;
     }

     @Override
     public Order find(Integer id) {
         Order order = entityManager.find(Order.class, id);
         return order;
     }

     @Override
     public List<Order> findAll() {
         Query query = entityManager.createQuery("select o from Order o");
         return query.getResultList();
     }

     @Override
     public List<Order> findBySymbol(String symbol) {
         Query query = entityManager.createQuery("select o from Order o where o.symbol = :symbol");
         return query.setParameter("symbol", symbol).getResultList();
     }
 }

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

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

发布评论

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

评论(3

你与昨日 2024-09-04 02:18:49

您是否尝试过将 unitName="MyServer" 添加到您的 @PersistenceContext 注释中?

Have you tried adding unitName="MyServer" to your @PersistenceContext annotation?

与之呼应 2024-09-04 02:18:49

使用org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor

Use the org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.

故事灯 2024-09-04 02:18:49

只是一个想法...您是否在 web.xml 中有此条目,

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/META-INF/spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

这将确保您的 spring 上下文已加载并且所有对象均由 spring 容器注入...如果您错过了这一点,请尝试...

just a thought...are you having this entry in the web.xml

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/META-INF/spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

this will make sure your spring context is loaded and all the objects are injected by spring container...just try if you have missed this...

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