Spring导入运行两次hibernate持久性

时间:2012-12-11 10:19:16

标签: java spring hibernate servlets spring-mvc

我有2个弹簧配置:

spring-servlet.xml
spring-security.xml

需要将此行添加到安全性中:

<beans:import resource="spring-servlet.xml"/>

现在hibernate运行了两次,这是日志截图:

enter image description here

我的web.xml:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

1 个答案:

答案 0 :(得分:0)

删除<beans:import resource="spring-servlet.xml"/>

并设置

<param-value>
    /WEB-INF/spring-security.xml;/WEB-INF/spring-servlet.xml 
</param-value>

也许你两次定义sessionFactory bean。删除其中一个。

编辑:

好的,两个背景是正常的。一个 - 由ContextLoaderListener加载的应用程序上下文应包含sessionFactory,dao,services等的定义,通常名称为applicationContext.xml

DispatcherServlet应该只包含MVC的bean。您可以在参数:

中定义conext名称
<init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/spring-servlet.xml</param-value>
</init-param>