2
votes

I'm using Spring MVC 3.2 and I can clearly see in the logs that application context is running twice when I run application. Initialization, database connection, mappings everything is doubled. I'm using JRebel with NetBeans 7.4 for development with Tomcat as container.

Here is the 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>

I tried moving applicationContext from /WEB-INF/spring/ into /WEB-INF/ and removing context-param but it still loads twice.

Here is the 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>

and finally 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>

Does anyone have any idea what is causing this and how to stop it?

EDIT

Here's is the 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>

EDIT 2

Here is the Tomcat log...looks like application is getting deployed twice

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
Can you post your hibernate-context.xml as well?chrylis -cautiouslyoptimistic-
Are you sure that you are not simply logging twice because of a wrong log configuration?LaurentG
Is this ALL your configuration? Make sure you aren't scanning for components (and thus @Configuration classes twice). Also make sure that one context (xml) isn't importing the other one.M. Deinum
What is the base package of your application? com.somedomain or com.somedomain.web? com.somedomain.web contains @Controllers, right?sp00m

1 Answers

0
votes

This could be a Spring configuration issue. Please try the following configuration.

In your dispatcher-servlet.xml:

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

In your 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>