Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring multiple bean instances being created
    text
    copied!<p>I have created a spring controller called RegistrationController using autowire @Controller. I have for my own curiosity sake created a default constructor as follows and added a logger statement:</p> <pre><code>public RegistrationController() { logger.info("Registration Controller (Constructor) called--&gt;"+this); } </code></pre> <p>What I found was when I start my tomcat server (v7) in spring source IDE (v2.9) in the log file I see the following:</p> <pre><code>INFO: Initializing Spring root WebApplicationContext 2012-08-15 15:12:28,808 [pool-2-thread-1] INFO com.controllers.registration.RegistrationController - Registration Controller (Constructor) called--&gt;com.controllers.registration.RegistrationController@78c0dc2 Aug 15, 2012 3:12:28 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring FrameworkServlet 'main' 2012-08-15 15:12:29,256 [pool-2-thread-1] INFO com.controllers.registration.RegistrationController - Registration Controller (Constructor) called--&gt;com.controllers.registration.RegistrationController@773ba8d6 </code></pre> <p>I understand that the RegistrationController by default should be singleton object and only one instance must be created. However, as you can see from the log file that two instances are created. Somehow I feel this is not correct. But I don't have the exact answer.</p> <p>Some of the important lines from my web.xml are as follows:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/classes/applicationContext*.xml&lt;/param-value&gt; &lt;/context-param&gt; . . &lt;servlet&gt; &lt;servlet-name&gt;main&lt;/servlet-name&gt; &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt; &lt;load-on-startup&gt;2&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;main&lt;/servlet-name&gt; &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;main&lt;/servlet-name&gt; &lt;url-pattern&gt;*.jspx&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;main&lt;/servlet-name&gt; &lt;url-pattern&gt;*.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; . . &lt;/web-app&gt; </code></pre> <p>As you must have guessed, I have a main-servlet.xml and applicationContext.xml as my spring configuration files.</p> <p>Can you please help me understand what is happening here?</p> <p>EDITED SINCE I CANNOT ANSWER MY QUESTION BEFORE 8 HRS HAS ELAPSED:</p> <p>Thanks to @Andna for the suggestion to look for the <code>&lt;context:component-scan /&gt;</code> in applicationContext.xml and main-servlet.xml. I had that element in both the file and hence every spring bean was scanned twice.</p> <p>However, removing the <code>&lt;context:component-scan /&gt;</code> from the applicationContext.xml caused my Spring security configuration to break. To go in more detail I had created a class that implemented <code>org.springframework.security.core.userdetails.UserDetailsService</code></p> <pre><code>@Service("userDetailsService") public class DuncanUserDetailsServiceImpl implements UserDetailsService { @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { } } </code></pre> <p>To support this class I had the following in my applicationContext-security.xml (shortened version) file:</p> <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt; &lt;beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"&gt; &lt;beans:property name="userDetailsService" ref="userDetailsService"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"&gt; &lt;beans:property name="providers"&gt; &lt;beans:list&gt; &lt;beans:ref local="daoAuthenticationProvider" /&gt; &lt;/beans:list&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;authentication-manager&gt; &lt;authentication-provider user-service-ref="userDetailsService"&gt; &lt;password-encoder ref= "passwordEncoder"&gt; &lt;salt-source user-property="userName"/&gt; &lt;/password-encoder&gt; &lt;/authentication-provider&gt; &lt;/authentication-manager&gt; &lt;beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" /&gt; &lt;/beans:beans&gt; </code></pre> <p>Therefore removing <code>&lt;context: component-scan /&gt;</code> from the applicationContext.xml caused the "userDetailsService" object instance to go missing and I got tonnes of errors in my log file.</p> <p>So what I did was I kept my component scan in the main-servlet.xml as it is:</p> <pre><code>&lt;context:component-scan base-package="com.some.org" &gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component-scan&gt; </code></pre> <p>However, I edited the component scan using the exclude filter in the applicationContext.xml as follows:</p> <pre><code>&lt;context:component-scan base-package="com.bankofamerica" &gt; &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;context:exclude-filter type="regex" expression="com.some.org.common.http.HTTPClient" /&gt; &lt;/context:component-scan&gt; </code></pre> <p>This helped me in achieving both the thing:</p> <ol> <li>Making sure that the singleton object was indeed singleton</li> <li>Making sure that the Spring security work as before.</li> </ol> <p>Thank you all for all the wonderful suggestions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload