Java 修复Spring MVC应用程序中的空EntityManger?

Java 修复Spring MVC应用程序中的空EntityManger?,java,spring,jpa,spring-mvc,entitymanager,Java,Spring,Jpa,Spring Mvc,Entitymanager,在下面的代码中,我发现注入的EnitityManager有问题,它总是显示为null public class GenericController extends AbstractController { @PersistenceContext(unitName = "GenericPU") private EntityManager em; protected ModelAndView handleRequestInternal( HttpS

在下面的代码中,我发现注入的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;
    }
}
公共类GenericController扩展了AbstractController{
@PersistenceContext(unitName=“GenericPU”)
私人实体管理者;
受保护的模型和视图句柄请求内部(
HttpServletRequest请求,
HttpServletResponse)引发异常{
//(em==null)始终被触发
如果(em==null)抛出新的NullPointerException(“em为null”);
集合泛型=em.createNamedQuery(“Generic.findAll”).getResultList();
ModelAndView mav=新ModelAndView(“通用”);
mav.addObject(泛型);
返回mav;
}
}
下面是在dispatcher-servlet.xml中定义的bean定义


应基于persistence-context.xml文件中定义的tx:annotation-based注入EntityManager


持久性上下文是从applicationContext.xml加载的


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

这是我的persistence.xml


com.application.orm.jpa.Generic
还有我的web.xml


上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
2.
调度员
*.htm
30
redirect.jsp

有人能帮我吗?

我想你需要一个文件
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="GenericPU">
     <class>com.domain.MyClass</class>
  </persistence-unit>

</persistence>

com.domain.MyClass

我认为如果文件有不同的名称,它将不起作用,尤其是因为您没有在任何地方告诉EntityManager工厂新名称。

我曾经使用较旧的spring版本,当时您必须将setProperty()放入bean,并在spring bean定义内设置property标记,如:

<bean id="Generic" class="com.application.web.GenericController" />
    <property name="em" ref="entityManager" />
</bean>

也许您应该使用transactionManager或entityManagerFactory bean

PD:我个人更喜欢注入依赖项的旧方法。

你包括在内了吗

<context:annotation-config />


在SpringXML中。参考资料

今天我很幸运能够与一位顾问就这个问题进行讨论,他能够帮助我解决整个问题

因此,我的问题是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注释中的EntityManager。

您如何从application-context.xml引用persistence-context.xml?使用导入?马克,我已经为你添加了。抱歉,我确实想排除详细信息,但这里的配置太多,我不想包含所有内容。我有一个persistence.xml,我省略了它,因为我不认为它对这个问题至关重要。很抱歉,我将它添加到了问题中。Spring参考手册第13章中描述了web层XML配置的整个过程:呃,对不起,不仅仅是XML配置,而是总体配置-编辑注释按钮已经在哪里了?!那你是怎么解决的?据我所知,ApplicationContext充当dispatcherServlet上下文(以及所有其他上下文)的“根”或“父”上下文,因此应用程序上下文中定义的所有bean都应该对它可用。e、 g.控制器通常有其他@Service或@Component bean注入其中,这些bean可以在applicationContext中定义。所以你的答案并不能真正解释为什么它对你不起作用。@justSomeJavaGuy,我在答案中添加了更多细节。直接回答您,从向我解释的方式来看,应用程序上下文和web上下文(DispatcherServlet)存在于两个不同的内存区域中,无法相互通信。此分段特定于我的应用程序在web.xml文件中的配置方式,您可以将其设置为只存在一个Spring上下文。感谢您的更新-我认为这只是对图表中的“bean”一词的混淆。Spring“bean”应该在这两种上下文中都可用,但persistence.xml中定义的单元可能不符合托管Spring“bean”的条件,因此没有传播到子上下文(dispatcher servlet.xml)。我也有一点猜测,但这就是我理解它的方式。我没有,但我相信它是通过设置tx:annotation-driven隐式打开的。
<?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>
<?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="GenericPU">
     <class>com.domain.MyClass</class>
  </persistence-unit>

</persistence>
<bean id="Generic" class="com.application.web.GenericController" />
    <property name="em" ref="entityManager" />
</bean>
<context:annotation-config />
<import resource="classpath:META-INF/persistence-context.xml"/>