同时存在SpringMvcConfig以及SpringConfig俩个不同的配置属性
package com.itheima.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
//定义一个servlet容器启动的配置类,在里面加载spring的配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载springmvc容器配置
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
//加载spring配置
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
//设置那些请求归属springmvc处理
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
对以上操作进行一些简化
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
根据选项可以选择ComponentScan不扫描的对象
@ComponentScan(value = "com.itheima",
//不扫描的文件
excludeFilters = @ComponentScan.Filter(
//按注解的方式进行筛选
type = FilterType.ANNOTATION,
//当为Controller注解时,不进行扫描操作
classes = Controller.class
))
选择需要扫描的几个对象的操作
@ComponentScan({"com.itheima.service","com.itheima.dao"})