我使用的是 Spring MVC 3.2,当我运行应用程序时,我可以在日志中清楚地看到应用程序上下文运行了两次。初始化、数据库连接、映射一切都加倍了。我将 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'
最佳答案
这可能是 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 - Spring MVC : application context is running twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20239465/