Issue
I have a root context that have all business bean (it is the root context)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:annotation-config />
<context:component-scan base-package="it.mypack">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
and servlet-context that have only controller bean (it is a child of root)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="it.mypack">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
I want to add a Configuration class in order to create some bean programmatically. My question is: these bean will be part of root-context or servlet-context?
My configuration:
@Configuration
public class AppConfig {
@Bean
public ArrayList<String> myBean() {
ArrayList<String> std=new ArrayList<String>();
std.add("ciao");
return std;
}
}
I ask this because I don't want to create duplicate beans. To not allow this issue if you read my context code the root context have enabled component scan for all components except controller and servlet-context have enabled component scan only for Controller... so my question is the bean ArrayList myBean (declared in my config class) have as context root or servlet-context? Are you sure that spring not create duplicate bean? Please, can you explain the behaviour?
Solution
"The root context have enabled component scan for all components except controller and servlet-context have enabled component scan only for Controller"
AppConfig has Configuration annotation so will be in the root context. As so anything inside that will be in the root context.
Answered By - Olga Khylkouskaya