在什么情况下配置
Configuration in what context
我有一个包含所有业务 bean 的根上下文(它是根上下文)
<?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>
和servlet-context只有controller bean(它是root的child)
<?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>
我想添加一个配置 class 以便以编程方式创建一些 bean。我的问题是:这些 bean 将成为 root-context 或 servlet-context 的一部分?
我的配置:
@Configuration
public class AppConfig {
@Bean
public ArrayList<String> myBean() {
ArrayList<String> std=new ArrayList<String>();
std.add("ciao");
return std;
}
}
我问这个是因为我不想创建重复的 bean。为了不允许这个问题,如果你阅读我的上下文代码,根上下文已经为控制器之外的所有组件启用了组件扫描,并且 servlet-context 只为控制器启用了组件扫描......所以我的问题是 bean ArrayList myBean(声明在我的配置 class) 有作为上下文根还是 servlet-context?您确定 spring 不会创建重复的 bean 吗?拜托,你能解释一下这种行为吗?
"The root context have enabled component scan for all components except controller and servlet-context have enabled component scan only for Controller"
AppConfig 具有配置注释,因此将位于根上下文中。因此,其中的任何内容都将位于根上下文中。
我有一个包含所有业务 bean 的根上下文(它是根上下文)
<?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>
和servlet-context只有controller bean(它是root的child)
<?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>
我想添加一个配置 class 以便以编程方式创建一些 bean。我的问题是:这些 bean 将成为 root-context 或 servlet-context 的一部分?
我的配置:
@Configuration
public class AppConfig {
@Bean
public ArrayList<String> myBean() {
ArrayList<String> std=new ArrayList<String>();
std.add("ciao");
return std;
}
}
我问这个是因为我不想创建重复的 bean。为了不允许这个问题,如果你阅读我的上下文代码,根上下文已经为控制器之外的所有组件启用了组件扫描,并且 servlet-context 只为控制器启用了组件扫描......所以我的问题是 bean ArrayList myBean(声明在我的配置 class) 有作为上下文根还是 servlet-context?您确定 spring 不会创建重复的 bean 吗?拜托,你能解释一下这种行为吗?
"The root context have enabled component scan for all components except controller and servlet-context have enabled component scan only for Controller"
AppConfig 具有配置注释,因此将位于根上下文中。因此,其中的任何内容都将位于根上下文中。