SpringMVC相关bean(表现层bean)
Spring控制的bean
业务bean(Service)
功能bean(DateSource等)
SpringMVC相关bean加载控制
SpringMVC加载的bean对应的包均在com.itheima.controller包内
Spring相关bean加载控制
方式一:Spring加载的bean设定扫描范围为com.itheima,排除controller包内的bean
方式二:Spring加载的bean设定扫描范围为精准扫描,例如service包、dao包等
方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中
方式一:
名称:@ComponentScan
类型:类注解
范例:
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; @Configuration //设置spring配置类加载bean时的过滤规则,当前要求排除掉表现层对应的bean //excludeFilters属性:设置扫描加载bean时,排除的过滤规则 //type属性:设置排除规则,当前使用按照bean定义时的注解类型进行排除 //classes属性:设置排除的具体注解类,当前设置排除@Controller定义的bean @ComponentScan(value="com.itheima", excludeFilters = @ComponentScan.Filter( type = FilterType.ANNOTATION, classes = Controller.class ) ) public class SpringConfig { }
属性:
excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes)
includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes)
方式三:
bean的加载格式:
- public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
- protected WebApplicationContext createServletApplicationContext() {
- AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
- ctx.register(SpringMvcConfig.class);
- return ctx;
- }
- protected WebApplicationContext createRootApplicationContext() {
- AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
- ctx.register(SpringConfig.class);
- return ctx;
- }
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- }
简化开发:
- //web配置类简化开发,仅设置配置类类名即可
- public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- protected Class<?>[] getRootConfigClasses() {
- return new Class[]{SpringConfig.class};
- }
-
- protected Class<?>[] getServletConfigClasses() {
- return new Class[]{SpringMvcConfig.class};
- }
-
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- }