1

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 ?

YogendraR
  • 2,144
  • 12
  • 17
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Mickael Jul 27 '17 at 08:20
  • Share your spring-servlet.xml. And also share your project directory and error logs. – KayV Jul 27 '17 at 08:38
  • @KayV edit done – YogendraR Jul 27 '17 at 09:58
  • It is loaded twice because you tell it to do so. Both the `ContextLoaderListener` and `Dispatcherservlet` load the `spring-servlet.xml` and thus everything is loaded twice. – M. Deinum Jul 27 '17 at 10:44

1 Answers1

1

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>
KayV
  • 12,987
  • 11
  • 98
  • 148