javax.servlet
javax.servlet-api
4.0.1
provided
org.springframework
spring-webmvc
5.3.20
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("springmvc save......");
return "{'msg':'save'}";
}
}
@Configuration
@ComponentScan("com.my.controller")
public class SpringMvcConfig {
}
public class SpringMvcContextInitConfig extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext acwac=new AnnotationConfigWebApplicationContext();
acwac.register(SpringMvcConfig.class);
return acwac;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
一次性工作
多次工作
启动服务器初始化过程
单次请求过程
SpringMVC 相关的 bean(表现层)
Spring 控制的 bean
SpringMVC 相关的 bean 加载控制
Spring 相关的 bean 加载控制
方式一:Spring 加载的 bean 设定扫描范围为 com.my ,排除controller 包内的 bean
@Configuration
@ComponentScan(value = "com.my",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
public class SpringConfig {
}
方式二:Spring 加载的 bean 设定扫描范围为精准范围,com.my.service 等
方式三:不区分 Spring 与 SpringMVC 的环境,加载到同一个环境中