简介
SpringMVC技术与Servlet技术功能等同,均属于web层开发技术
SpringMVC是一种基于java实现的MVC模型的轻量级Web框架
优点
使用简单,开发便捷(相比于Servlet)
灵活性强
入门案例
第一步、导入SpringMVC与Servlet坐标
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cacb</groupId><artifactId>SpringMVC_demo1</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><port>80</port><path>/</path></configuration></plugin></plugins></build></project>
第二步、创建SpringMVC控制类(等同于Servlet功能)
package com.cacb.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;//定义controller(使用@Controller定义Bean)
@Controller
public class UserController {//设置当前操作的返回路径@RequestMapping("/save")//设置当前路径的返回值类型@ResponseBodypublic String save(){System.out.println("user saving!");return "{'module':'SpringMVC'}";}
}
第三步、初始化SpringMVC环境(同SPring环境),设定SpringMVC加载对应的Bean
package com.cacb.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//创建spring配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.cacb.controller")
public class SpringMVCConfig {
}
第四步、初始化Servlet容器,加载SPringMVC环境,并设置SpringMVC技术处理的请求
package com.cacb.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;//定义一个servlet容器启动的配置类,在里面加载spring配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {// 加载springMVC容器配置@Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(SpringMVCConfig.class);return ctx;}//设置哪些请求归属springMVC处理@Overrideprotected String[] getServletMappings() {//所有请求归springmvc管理return new String[]{"/"};}//加载spring容器配置@Overrideprotected WebApplicationContext createRootApplicationContext() {return null;}
}
Bean加载控制
Controller加载控制与业务Bean加载控制
SpringMVC相关Bean
表现层Bean
Spring控制的Bean
业务Bean(Service)
功能Bean(DataSource等)
因功能不同,为了避免Spring错误的加载到SpringMVC管理的Bean
加载Spring控制的Bean的时候要排除掉SpringMVC控制的Bean
三种解决方式
方式一:Spring加载的bean设定扫描范围为com.cacb,排除掉controller包内的bean
方式二:Spring加载的bean设定扫描范围为几个准范围,例如service,dao包等
方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中