YogendraR
YogendraR

Reputation: 2386

Spring beans are initializing twice in Spring Web application

Project Directory We are working on Spring MVC project. We want to initialize all the beans at the time of deploying the EAR. Following is our web.xml file :

    <servlet>
    <servlet-name>Spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>Spring</servlet-name>
    <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Spring-servlet.xml</param-value>
    </context-param>

   <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Below is our Spring configuration file:

<context:component-scan base-package="com"></context:component-scan>
<context:annotation-config></context:annotation-config>
<mvc:annotation-driven></mvc:annotation-driven>

<!-- Switch on the Caching -->
<cache:annotation-driven />

<!-- Do the component scan path -->
<!-- <context:component-scan base-package="caching" /> -->
<bean id="ehcache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:configLocation="WEB-INF/ehcache.xml" p:shared="true" /> <!-- Why changed to true? https://stackoverflow.com/a/16370326 -->

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cacheManager-ref="ehcache" />

<!-- creating datasource -->
<bean id="dataSourceForFilters"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.mariadb.jdbc.Driver" />
    <property name="url" value="jdbc:mariadb://ip:3306/demo" />
    <property name="username" value="remote" />
    <property name="password" value="password" />
</bean>
<bean id="dataSourceForData" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/misoracle"></property>
</bean>

<!-- creating jdbctemplate and injecting datasource into it -->
<bean id="jdbcTemplateForFilters" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceForFilters"></property>
</bean>
<bean id="jdbcTemplateForData" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceForData"></property>
</bean>
<!-- end -->

<bean id="applicationContextProvider" class="com.providers.ApplicationContextProvider"></bean>

We are using component annotations(@Controller,@Service,@Repository) for defining beans.

While deploying the EAR into the server, Beans are initializing fine as per our requirement.

When we are hitting the application with URL pattern like 'IP:port/context-root/rest', Spring beans are initializing once again.

Why beans are initializing again, can someone please help ?

Upvotes: 1

Views: 1053

Answers (1)

KayV
KayV

Reputation: 13845

Your web.xml mapping should be like this:

<servlet>
    <servlet-name>Spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Spring-servlet.xml</param-value>
</context-param>

Upvotes: 1

Related Questions