Spring相关类加载控制:
-
Spring加载的bean设定扫描范围为com.it,排除掉Controller包内的bean
@ComponentScan(value="com.it",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
-
Spring加载的bean设定扫描范围为精准范围,例如service包、dao包等
@ComponentScan({"com.it.service","com.it.dao"})
-
不区分Spring和SpringMVC环境加载到同一个环境中
Controller加载控制与业务bean加载控制:
package com.it.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//解决post中文乱码
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
/**
public class ServletConfig extends AbstractDispatcherServletInitializer {
// 加载springMVC 容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
// 谁知哪些请求归属于springMVC处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}//'/'表示处理所以请求
// 加载spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
*/
}