Reputation: 10502
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>
Upvotes: 0
Views: 261
Reputation: 280175
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.
Upvotes: 2