在Spring MVC应用程序中修复Null EntityManger吗?

詹姆斯·麦克马洪:

在下面的代码中,我对注入的EnitityManager感到麻烦,它始终显示为null

public class GenericController extends AbstractController {

    @PersistenceContext(unitName = "GenericPU")
    private EntityManager em;

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //(em == null) is always trigged
        if (em == null) throw new NullPointerException("em is null");
        Collection<Generic> Generics = em.createNamedQuery("Generic.findAll").getResultList();

        ModelAndView mav = new ModelAndView("Generic");
        mav.addObject(Generics); 
        return mav;
    }
}

这是在dispatcher-servlet.xml中定义的bean定义

<bean id="Generic" class="com.application.web.GenericController" />

应基于persistence-context.xml文件中定义的基于tx:annotation的方式注入EnitityManager

<?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-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="removed" />
        <property name="username" value="removed" />
        <property name="password" value="removed" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="GenericPU" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
            </bean>
        </property>
    </bean>

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

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

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

</beans> 

持久性上下文是从applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
    <import resource="classpath:META-INF/persistence-context.xml"/>
</beans>

由于ORM实体作为JAR文件包含在项目中,因此完成了类路径导入。请注意,我相信持久性上下文已在加载,因为如果Spring无法解析其配置文件,它将不允许部署该应用程序。

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="CoolOrmJpaPU" transaction-type="RESOURCE_LOCAL">
    <class>com.application.orm.jpa.Generic</class>
    <!-- bunch more classes -->
  </persistence-unit>
</persistence>

还有我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

有谁可以帮我离开这里吗?

詹姆斯·麦克马洪:

今天我很幸运能够与顾问讨论这个问题,他能够帮助我解决整个问题。

所以我的问题是Spring MVC正在建立两个截然不同的上下文,一个是在applicationContext.xml中定义的应用程序上下文,另一个是在dispatcher-servlet.xml中定义的Web上下文。

一个上下文中的Bean无法与另一上下文中的Bean对话,因此,当我在applicationContext.xml中初始化持久性上下文时,我无法在由dispatcher-servlet.xml加载的Bean中访问它,即我的控制器。

当Netbeans自动生成我的Spring MVC的基础时,默认情况下会进行设置。在某些大型Web应用程序中,在与其余逻辑(持久性,业务代码等)不同的上下文中分离应用程序的Web部分将是有意义的。就我而言,当我尝试将实体管理器直接自动注入控制器中时,这对我不利。

为了解决这个问题,我移动了生产线

<import resource="classpath:META-INF/persistence-context.xml"/>

从applicationContext.xml到我的dispatcher-servlet.xml。然后,我的控制器从@PersistanceContext批注中正确地注入了EntityManagers。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring MVC应用程序过滤URL中的HTML-这是安全问题吗?

Spring MVC应用程序中可以有多个控制器吗?

Spring MVC应用程序中的性能

我可以在spring mvc应用程序中合并控制器吗?如果可以,它将如何影响性能?

在Spring Kafka中,我需要在应用程序中添加@EnableKafka批注吗?

在Spring MVC应用程序中打开静态文件

Spring MVC应用程序中的main()方法

Spring MVC应用程序测试中的ContextConfiguration

在Spring MVC应用程序中找不到静态资源

Spring MVC应用程序中未填充选择框

Spring MVC Web应用程序中的模板

Spring MVC应用程序中的CommandLineRunner功能

在Spring MVC应用程序中实现Swagger的“简单”方法

Spring MVC应用程序中的多个ScriptTemplateViewResolvers

在Spring MVC应用程序中检索会话数据

在 intellij idea 中运行 spring mvc 应用程序的问题

我可以将AWS User Pools用作Spring应用程序中的目录服务吗?

应该直接测试Spring Boot应用程序中的存储库吗?

在现代Spring Boot / SpringMVC应用程序中,我们仍然需要@Transactional吗?

在Spring Boot应用程序中未创建WEB-INF文件夹吗?

我可以在Spring Boot应用程序中从Thymeleaf表发出HTTP POST请求吗

我可以在Spring Boot应用程序中运行长任务吗?

在Spring Boot应用程序中配置mongodb属性maxWaitQueueSize吗?

可以在应用程序中实现SAML和基本Spring Security吗?

Spring框架可以在android应用程序中使用吗?

在Spring Boot应用程序中Autowired字段为null

我们可以使用Electron工具为Spring Boot Web MVC创建独立的应用程序吗?

如何在Apache Tomcat 6服务器中修复Java Spring MVC Web应用程序的运行时间?

在已经将spring-mvc用作依赖项的应用程序中设置spring-mvc