1:@Component注解
代替Spring-application.xml核心配置文件中的<bean>标签中的类
2:@Controller、@Service、@Repository是@Component的衍生注解
@Contorller:用于表现层bean定义
@Service:用于业务层bean定义
@Repository:用于数据层bean定义
3:配置注解开发
@Configuration:表示这个类代替了application.xml配置
@ComponentScan({"",""}):表示<context:component-scan>扫描的依赖包
4:Bean的实例化
@Scope("singleton"):表示创建的bean是单例模式的
@Scpoe("prototype"): 表示创建的bean不是单例的
@PostConstruct: 构造方法后
@PreDestroy: 销毁前
5:@Bean:配置管理第三方Bean
6:AOP的使用
- 概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式
- 作用:在不惊动原始设计的基础上为方法进行功能
增强
- 核心概念
- 代理(Proxy):SpringAOP的核心本质是采用
代理模式
实现的 - 连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行
- 切入点(Pointcut):匹配连接点的式子,也是具有共性功能的方法描述
- 通知(Advice):若干个方法的共性功能,在切入点处执行,最终体现为一个方法
- 切面(Aspect):描述通知与切入点的对应关系
- 目标对象(Target):被代理的原始对象成为目标对象
@Component
@Aspect
定义的一个通知类
@Ponitcut("execution(返回值类型 包.方法(参数))")
定义的通知空方法
@Around("类名.通知空方法()")
定义加强的通知方法
public Object runSpeed(ProceedingJoinPoint pjp){
Objection ret = pjp.proceed();
return ret;
}
7:SpringMvc的坐标导入
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
8.SpringMvc的配置
//定义Controller,使用@Controller定义Bean
@Controller
public class UserController {
//设置当前访问路径,使用@RequestMapping
@RequestMapping("/save")
//设置当前对象的返回值类型
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'SpringMVC'}";
}
}
//创建SpringMVC的配置文件,加载controller对应的bean
@Configuration
//
@ComponentScan("com.blog.controller")
public class SpringMvcConfig {
}
//定义一个servlet容器的配置类,在里面加载Spring的配置,继承AbstractDispatcherServletInitializer并重写其方法
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
//加载SpringMvc容器配置
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringMvcConfig.class);
return context;
}
//设置哪些请求归SpringMvc处理
protected String[] getServletMappings() {
//所有请求都交由SpringMVC处理
return new String[]{"/"};
}
//加载Spring容器配置
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
9.REST风格规范
REST中规定GET
/POST
/PUT
/DELETE
针对的是查询/新增/修改/删除,但如果我们非要使用GET
请求做删除,这点在程序上运行是可以实现的
10.SSM注解开发整合操作分析
- 创建工程
- 创建一个Maven的web工程
- pom.xml添加SSM需要的依赖jar包
- 编写Web项目的入口配置类,实现
AbstractAnnotationConfigDispatcherServletInitializer
重写以下方法
getRootConfigClasses()
:返回Spring的配置类 —> 需要SpringConfig
配置类getServletConfigClasses()
:返回SpringMVC的配置类 —> 需要SpringMvcConfig
配置类getServletMappings()
: 设置SpringMVC请求拦截路径规则getServletFilters()
:设置过滤器,解决POST请求中文乱码问题
- SSM整合(重点是各个配置的编写)
SpringConfig
- 标识该类为配置类,使用
@Configuration
- 扫描
Service
所在的包,使用@ComponentScan
- 在
Service
层要管理事务,使用@EnableTransactionManagement
- 读取外部的
properties
配置文件,使用@PropertySource
- 整合
Mybatis
需要引入Mybatis相关配置类,使用@Import
- 第三方数据源配置类
JdbcConfig
- 构建DataSource数据源,DruidDataSouroce,需要注入数据库连接四要素,使用
@Bean
、@Value
- 构建平台事务管理器,DataSourceTransactionManager,使用
@Bean
- Mybatis配置类
MybatisConfig
- 构建
SqlSessionFactoryBean
并设置别名扫描与数据源,使用@Bean
- 构建
MapperScannerConfigurer
并设置DAO层的包扫描
SpringMvcConfig
- 标识该类为配置类,使用
@Configuratio
- 扫描
Controller
所在的包,使用@ComponentScan
- 开启SpringMVC注解支持,使用
@EnableWebMvc
- 功能模块(与具体的业务模块有关)
- 创建数据库表
- 根据数据库表创建对应的模型类
- 通过Dao层完成数据库表的增删改查(接口+自动代理)
- 编写
Service
层(Service接口+实现类)
@Service
@Transactional
- 整合Junit对业务层进行单元测试
@RunWith
@ContextConfiguration
@Test
- 编写
Controller
层
- 接收请求
@RequestMapping
、@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
- 接收数据 简单、POJO、嵌套POJO、集合、数组、JSON数据类型
@RequestParam
@PathVariable
@RequestBody
- 转发业务层
@Autowired
- 响应结果
@ResponseBody