【问题标题】:Spring MVC: application context is running twiceSpring MVC:应用程序上下文运行两次
【发布时间】:2013-12-12 22:01:19
【问题描述】:

我正在使用 Spring MVC 3.2,并且我可以在日志中清楚地看到,当我运行 application 时,应用程序上下文正在运行两次。初始化、数据库连接、映射一切都翻了一番。我正在将 JRebel 与 NetBeans 7.4 一起用于使用 Tomcat 作为容器进行开发。

这里是 web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/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>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>WebResourceOptimizer</filter-name>
    <filter-class>
        ro.isdc.wro.http.WroFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>WebResourceOptimizer</filter-name>
    <url-pattern>/wro/*</url-pattern>
</filter-mapping>

我尝试将 applicationContext 从 /WEB-INF/spring/ 移动到 /WEB-INF/ 并删除 context-param 但它仍然加载两次。

这里是applicationContext.xml:

<context:component-scan base-package="com.somedomain.web" />

<mvc:annotation-driven />

<mvc:resources mapping="/static/**" location="/static/" />

<jpa:repositories base-package="com.somedomain.web" />

<import resource="../hibernate/hibernate-context.xml" />

<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages"></property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>

最后是 dispatcher-servlet.xml:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/html/" />
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="" />
    <property name="suffix" value=".html" />
    <property name="contentType" value="text/html; charset=UTF-8"></property>
    <property name="exposeSpringMacroHelpers" value="true" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en"></property>
</bean>

有谁知道是什么原因造成的以及如何阻止它?

编辑

这里是hibernate-context.xml:

<context:property-placeholder location="/WEB-INF/database/firebirddb.properties" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="${database.driver}"
    p:jdbcUrl="${database.url}"
    p:user="${database.user}"
    p:password="${database.password}"
    p:acquireIncrement="5"
    p:idleConnectionTestPeriod="60"
    p:maxPoolSize="100"
    p:maxStatements="50"
    p:minPoolSize="10" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="/WEB-INF/persistence/persistence.xml" />
    <property name="persistenceUnitName" value="hibernatePersistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

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

编辑 2

这是 Tomcat 日志...看起来应用程序被部署了两次

stu 27, 2013 1:56:17 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
stu 27, 2013 1:56:17 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
stu 27, 2013 1:56:30 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
stu 27, 2013 1:56:32 PM org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'dispatcher'
stu 27, 2013 1:56:32 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
stu 27, 2013 1:56:35 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
stu 27, 2013 1:56:35 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
stu 27, 2013 1:56:47 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'

【问题讨论】:

  • 你也可以发布你的hibernate-context.xml 吗?
  • 您确定不是因为错误的日志配置而简单地记录了两次?
  • 这都是你的配置吗?确保您没有扫描组件(因此@Configuration 类两次)。还要确保一个上下文 (xml) 没有导入另一个上下文。
  • 您的应用程序的基本包是什么? com.somedomain 还是 com.somedomain.webcom.somedomain.web 包含 @Controllers,对吧?

标签: java spring spring-mvc


【解决方案1】:

这可能是 Spring 配置问题。请尝试以下配置。

在您的 dispatcher-servlet.xml 中:

<context:component-scan base-package="com.somedomain.web" />

在您的 applicationContext.xml 中:

<context:component-scan base-package="com.somedomain">
    <!-- let springmvc context scan the web package -->
    <context:exclude-filter type="regex" expression="com/somedomain/web/.*" />
</context:component-scan>

【讨论】:

  • 我试过了,我得到了这个异常:java.lang.IllegalStateException: No transactional EntityManager available
  • @MirkoFilipovic 这似乎是一个 JPA/Hibernate 配置问题。这里是我常用的配置:link。请试一试并告诉我。
  • 我很确定这不是 JPA/Hibernate 配置问题。当我运行具有 EntityManager 自动装配的应用程序时,该异常是由索引数据库字段的类引发的。我禁用了该类并且上下文仍在加载两次。
猜你喜欢
  • 2013-04-03
  • 2014-08-18
  • 2012-10-08
  • 2011-04-23
  • 2012-10-04
  • 2023-03-16
  • 1970-01-01
  • 2017-03-02
  • 2012-06-15
相关资源
最近更新 更多