Spring MVC:应用程序上下文运行两次

时间:2013-11-27 10:10:05

标签: java spring spring-mvc

我正在使用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>

我尝试从/ WEB-INF / spring / into / WEB-INF /移动applicationContext并删除co​​ntext-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'

1 个答案:

答案 0 :(得分:0)

这可能是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>