0

Here is bean initialization

<bean id="testBean" class="com.xyz.server.instancefactory.Test">
    <property name="v" value="3000"/>
</bean>

Here is the bean:

public class Test {

    private int v=0;

    public int getV() {
        return v;
    }

    public void setV(int v) {
        System.out.println("setting V value................."+v);
        this.v = v;
    }
}

When I run the app it shows setting V value.................3000 twice . Why? I don't see any reason of calling it twice.

Here is some log

Feb 03, 2014 12:20:40 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/MYAPP]
Feb 03, 2014 12:20:40 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /home/manish/.netbeans/7.3.1/apache-tomcat-7.0.34.0_base/conf/Catalina/localhost/MYAPP.xml
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
1.............setting V value.................3000
1.............setting V value.................3000
Feb 03, 2014 12:20:42 AM org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MYAPP]] after start() had already been called. The second call will be ignored.

Web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

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

mvc-dispatcher-servlet.xml

<context:component-scan base-package="com.myapp.server.controllers" />
    <mvc:annotation-driven />
        <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                    <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                    <value>.jsp</value>
            </property>
    </bean>
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
        <bean id="testBean" class="com.MYAPP.server.instancefactory.Test">
            <property name="v" value="3000"/>
        </bean>
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

1 Answers1

2

Your context is being loaded twice. First by the ContextLoaderListener because of this config

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

And second by the DispatcherServlet which, by default, tries to find a resource with the name of the servlet as declared here

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

and -servlet.xml appended to the end, ie. mvc-dispatcher-servlet.xml.

Your ContextLoaderListener should be loading your root context, while the DispatcherServlet should be loading the servlet context, which should include beans necessary for the controller and view configuration.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    @Manish You don't seem to currently need a `ContextLoaderListener`, so get rid of it and the `context-param`. – Sotirios Delimanolis Feb 02 '14 at 19:09
  • I commented `` and `` part of xml and its working. am i doing right? – Manish Kumar Feb 02 '14 at 19:14
  • @Manish It working before, too. It was just redundant. It depends what you want to do. If you want those beans to be declared in the `ApplicationContext` loaded by `DispatcherServlet`, then you are doing it correctly. – Sotirios Delimanolis Feb 02 '14 at 19:15
  • one more thing if i remove context-param tag but next time if i hv to include spring-security.xml file the how will include that cos i have removed context-param tag – Manish Kumar Feb 02 '14 at 19:17
  • 1
    @Manish [You can split up your context files.](http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s). Modularize them so that each is configuring one component/layer of the application. The `ContextLoaderListener` loads the root context, which may import security and persistence contexts. The `DispatcherServlet` loads the servlet context. – Sotirios Delimanolis Feb 02 '14 at 19:18
  • correct me if i am doing something wrong.In `web.xml` i am using `DispatcherServlet` and to use other configuration xml file i am importing those xml file inside `-servlet.xml` using ``. Am I doing correct to break the thing in multiple file? and in this way i will use all security,db xml config file. correct? – Manish Kumar Feb 03 '14 at 05:44