SpringMVC简介
SpringMVC入门案例
①:使用SpringMVC技术想导入SpringMVC坐标与Servlet坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</sccope>
</dependeccy>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependeccy>
②:创建SpringMVC控制器类(等同于Servlet功能)
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save...")
return "{'info':'springmvc'}"
}
}
③:初始化SpringMVC环境,设定SpringMVC加载对应的bean
@Configuration
@ComponentScan("com.mei.controller")
public class SpringMvcConfig{
}
④:初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理请求
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer{
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotataionConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx; //Tomcat容器加载spring配置
}
protected String[] getServletMapping() {
return new String[] {"/"}; //拦截所有请求
}
protected WebApplicationContext createRootApplicationContext(){
return null;
}
}
SpringMVC工作流程
-
启动服务器初始化过程
1.服务器启动,执行ServletCountainersInitConfig类,初始化web容器
2.执行createServletApplicationContext方法,创建了WebApplicationContext对象
3.加载SpringMvcConfig
4.执行@ComponentScan加载对应的bean
5.加载UserController,每个@RequestMapping的名称对应一个具体的方法
6.执行getServletMappings方法,定义所有请求都通过SpringMVC -
单次请求过程
1.发送请求localhost/save
2.web容器发现所有请求都经过SpringMVC,将请求交给SpringMVC处理
3.解析请求路径/save
4.有/save匹配执行对应方法save()
5.执行save()
6.检测到@ResponseBody直接将save()方法的返回值作为响应求体返回给请求方
Controller加载控制与业务bean加载控制
-
SpringMVC相关bean(表现层bean)
-
Spring控制的bean
业务bean(Service)
功能bean(DataSource) -
SpringMVC相关bean加载控制
SpringMVC加载的bean对应的包均在com.mei.controller包内 -
Spring相关bean加载控制
方式一:Spring加载的bean设定扫描范围为com.mei,排除掉controller包内的bean
方式二:Spring加载的bean设定扫描范围为精准范围,例如service包、dao包等
方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中 -
@ComponentScan 类注解
-
范例:
@Configuration
@ComponentScan(value = "com.mei",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.clas
)
)
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; //加载SpringMVC
}
protected WebApplicationContext creatRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class)
return ctx; //加载Spring
}
protected String[] getServletMappings() {
return new String[]{"/"}
}
}
- 简化开发
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer{
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected Class<?>[] getServletMappings() {
return new Class[]{"/"};
}
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
}