2

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?

alexbt
  • 16,415
  • 6
  • 78
  • 87
esoni
  • 1,362
  • 4
  • 24
  • 37
  • from my test it seems that configuration bean added is in root container .. can someone explain why ? For my purpose this is correct – esoni May 16 '17 at 15:15

1 Answers1

0

"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.

  • So also Configuration is scanned by context:component-scan .. in this way spring add all the beans contained in all configuration class scanned bycomponent-scan. Can you confirm ? – esoni May 17 '17 at 08:01
  • only last questo Olga :) : if i create two class @Configuration with some beans Spring in this case add its in root context or i can have only 1 configuration? – esoni May 18 '17 at 06:45
  • you can have as many configurations classes as you want: http://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/modularizing-configurations.html – Olga Khylkouskaya May 18 '17 at 16:15