Spring MVC Beans Loaded Twice Problem
05-06-2015In Spring MVC if you have two separate xml files such as applicationContext.xml and mvc-dispatcher-servlet.xml, then you must set use-default-filters attribute value to false to disable automatic detection opf classes annotated with @Component, @Repository, @Service or @Controller because these classes will be detected by means of <context:component-scan>
element.
applicationContext.xml file
<context:component-scan base-package="com.codesenior.telif.web"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<context:exclude-filter>
element exclude Controller classes because these classes are used in Spring MVC. However, applicationContext.xml file should contain core Spring components and mvc-dispatcher-servlet.xml file should contain Spring MVC configurations.
mvc-dispatcher-servlet.xml file
<context:component-scan base-package="com.codesenior.telif.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
After this configuration Spring will not detect same classes twice.