

仅自用,如有侵权,立刻删!——感谢【尚硅谷】官方文档

Spring 是最受欢迎的企业级 Java 应用程序开发框架,数以百万的来自世界各地的开发人员使用
Spring 框架来创建性能好、易于测试、可重用的代码。
Spring 框架是一个开源的 Java 平台,它最初是由 Rod Johnson 编写的,并且于 2003 年 6 月首次在 Apache 2.0 许可下发布。
Spring 是轻量级的框架,其基础版本只有 2 MB 左右的大小。
Spring 框架的核心特性是可以用于开发任何 Java 应用程序,但是在 Java EE 平台上构建 web 应用程序是需要扩展的。 Spring 框架的目标是使 J2EE 开发变得更容易使用,通过启用基于 POJO编程模型来促进良好的编程实践。
Spring 基础框架,可以视为 Spring 基础设施,基本上任何其他 Spring 项目都是以 Spring Framework为基础的。
先简单说一下,IOC容器就是放置已经创建好的实例的一个地方。这样实例不需要程序员来手动创建,而是交给容器管理,更加高效。
下面这一部分更加详细阐述了,如果要深入了解IOC容器,需要阅读源码,这个以后安排上。这个笔记主要用于学习如何使用框架
IOC:Inversion of Control,翻译过来是反转控制。
①获取资源的传统方式
自己做饭:买菜、洗菜、择菜、改刀、炒菜,全过程参与,费时费力,必须清楚了解资源创建整个过程中的全部细节且熟练掌握。
在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取所需要的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式,增加了学习成本,同时降低了开发效率。
②反转控制方式获取资源
点外卖:下单、等、吃,省时省力,不必关心资源创建过程的所有细节。
反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接收资源的方式即可,极大的降低了学习成本,提高了开发的效率。这种行为也称为查找的被动形式。
③DI
DI:Dependency Injection,翻译过来是依赖注入。
DI 是 IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如:setter 方法)接受来自于容器的资源注入。相对于IOC而言,这种表述更直接。
所以结论是:IOC 就是一种反转控制的思想, 而 DI 是对 IOC 的一种具体实现。
Spring 的 IOC 容器就是 IOC 思想的一个落地的产品实现。IOC 容器中管理的组件也叫做 bean。在创建bean 之前,首先需要创建 IOC 容器。Spring 提供了 IOC 容器的两种实现方式:
①BeanFactory
这是 IOC 容器的基本实现,是 Spring 内部使用的接口。面向 Spring 本身,不提供给开发人员使用。
②ApplicationContext
BeanFactory 的子接口,提供了更多高级特性。面向 Spring 的使用者,几乎所有场合都使用
ApplicationContext 而不是底层的 BeanFactory。

对于xml文件的方式也要学会掌握,因为对于工程里面的一些jar包,你是无法在上面给他加注释的,只是使用xml文件方式。
maven依赖:
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>

创建Spring的配置文件:

配置文件中:
<bean id="studentOne" class="com.zylai.spring.pojo.Student">bean>
<bean id="studentTwo" class="com.zylai.spring.pojo.Student">bean>
测试:
@Test
public void testIOC(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
//获取bean
// 1.通过bean的id获取
Student studentOne = (Student) ioc.getBean("studentOne");
}
注意:要求ioc容器中有且仅有一个与之匹配的bean
若没有任何一个类型匹配的bean,抛出NoSuchBeanDefinitionException
若有多个类型匹配的bean,抛出NoUniqueBeanDefinitionException
@Test
public void testIOC(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
//获取bean
// 2.通过类的Class对象获取
Student studentOne = ioc.getBean(Student.class);
}
@Test
public void testIOC(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
//3.通过类型和id获取
Student studentOne = ioc.getBean("studentOne",Student.class);
System.out.println(studentOne);
}
以后用的最多的就是第二种方式,一个类型的bean只需要配置一次就可以了,如果真的需要多个实例,那么配置时加上scope属性选择多例就可以了
注意:在IOC容器中通过工厂模式和反射技术创建对象,所以需要对象的无参构造器
xml文件中不能写接口,很简单,接口没有实例对象,也没有无参构造器
如果组件类实现了接口,根据接口类型可以获取 bean 吗?
可以,前提是bean唯一
如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型可以获取 bean 吗?
不行,因为bean不唯一
下面这个会得到接口的实现类对象
@Test
public void testIOC(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
//通过接口获取实例
Person person = ioc.getBean(Person.class);
System.out.println(person);
}
根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:
对象 instanceof 指定的类型 的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到。
即通过bean的类型、bean所继承的类的类型、bean所实现的接口的类型都可以获取bean
分别是调用类的set方法和有参构造
<bean id="studentTwo" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1001"/>
<property name="sname" value="张三"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
bean>
<bean id="studentThree" class="com.zylai.spring.pojo.Student">
<constructor-arg value="1002"/>
<constructor-arg value="李四"/>
<constructor-arg value="女"/>
<constructor-arg value="23" name="age"/>
bean>
特殊字符和CDATA节的处理如下:
<bean id="studentFour" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1003"/>
<property name="sname">
<value>]]>value>
property>
<property name="gender">
<null/>
property>
bean>
三种方式:
ref:ref引用IOC容器中的某个bean的id
<bean id="studentFive" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1006"/>
<property name="sname" value="张六"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
<property name="clazz" ref="clazzOne"/>
bean>
级联:级联的方式,要求clazz属性赋值或者实例化。所以一般不用
<bean id="studentFive" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1006"/>
<property name="sname" value="张六"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
<property name="clazz.cid" value="1122"/>
<property name="clazz.cname" value="哈哈班"/>
bean>
内部bean:在属性中设置一个bean。
注意:不能通过ioc容器获取,相当于内部类,只能在当前student内使用
<bean id="studentFive" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1006"/>
<property name="sname" value="张六"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
<property name="clazz">
<bean id="clazzInner" class="com.zylai.spring.pojo.Clazz">
<property name="cid" value="1122"/>
<property name="cname" value="王班"/>
bean>
property>
bean>
在属性中使用array标签,标签中数组元素的值写在value中
<bean id="studentSix" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1006"/>
<property name="sname" value="张六"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
<property name="clazz">
<bean id="clazzInner" class="com.zylai.spring.pojo.Clazz">
<property name="cid" value="1122"/>
<property name="cname" value="王班"/>
bean>
property>
<property name="hobbies">
<array>
<value>唱value>
<value>跳value>
<value>rapvalue>
array>
property>
bean>
方式一:在属性中使用list标签
<bean id="clazzTwo" class="com.zylai.spring.pojo.Clazz">
<property name="cid" value="1111"/>
<property name="cname" value="王者班"/>
<property name="studentList">
<list>
<ref bean="studentOne"/>
<ref bean="studentTwo"/>
<ref bean="studentThree"/>
list>
property>
bean>
方式二:再写一个集合类型的bean
<bean id="clazzTwo" class="com.zylai.spring.pojo.Clazz">
<property name="cid" value="1111"/>
<property name="cname" value="王者班"/>
<property name="studentList" ref="studentList"/>
bean>
<util:list id="studentList">
<ref bean="studentFour"/>
<ref bean="studentFive"/>
<ref bean="studentSix"/>
util:list>
方式和list差不多
方式一:设置map标签
<bean id="studentSix" class="com.zylai.spring.pojo.Student">
<property name="sid" value="1006"/>
<property name="sname" value="张六"/>
<property name="age" value="23"/>
<property name="gender" value="男"/>
<property name="clazz">
<bean id="clazzInner" class="com.zylai.spring.pojo.Clazz">
<property name="cid" value="1122"/>
<property name="cname" value="王班"/>
bean>
property>
<property name="hobbies">
<array>
<value>唱value>
<value>跳value>
<value>rapvalue>
array>
property>
<property name="teacherMap">
<map>
<entry key="语文老师" value-ref="teacherOne"/>
<entry key="数学老师" value-ref="teacherTwo"/>
map>
property>
bean>
方式二:写一个集合类型的bean
<bean id="studentSix" class="com.zylai.spring.pojo.Student">
···略
<property name="teacherMap" ref="teacherMap"/>
bean>
<util:map id="teacherMap">
<entry key="语文老师" value-ref="teacherOne"/>
<entry key="数学老师" value-ref="teacherTwo"/>
util:map>
<bean id="studentSeven" class="com.zylai.spring.pojo.Student"
p:sid="1008" p:sname="小红" p:clazz-ref="clazzOne"
p:teacherMap-ref="teacherMap">
bean>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.31version>
dependency>
jdbc.propertiesjdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=root
initialSize=5
maxActive=9
注意:需要通过context标签引入外部文件,注意context标签在xml文件头对应的链接
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
beans>
在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义参加下表:
| 取值 | 含义 | 创建对象的时机 |
|---|---|---|
| singleton (默认) | 在IOC容器中,这个bean的对象始终为单实例 | IOC容器初始化时 |
| prototype | 这个bean在IOC容器中有多个实例 | 获取bean时 |
如果是在WebApplicationContext环境下还会有另外两个作用域(但不常用) :
| 取值 | 含义 |
|---|---|
| request | 在一个请求范围内有效 |
| session | 在一个会话范围内有效 |
<bean id="student" class="com.zylai.spring.pojo.Student" scope="prototype">
<property name="sid" value="1001"/>
<property name="sname" value="张三"/>
bean>
测试验证:
实例:
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
public User() {
System.out.println("生命周期:1、创建对象");
}
public User(Integer id, String username, String password, Integer age) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
System.out.println("生命周期:2、依赖注入");
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void initMethod() {
System.out.println("生命周期:3、初始化");
}
public void destroyMethod() {
System.out.println("生命周期:4、销毁");
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
后置处理:
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//此方法在bean的生命周期初始化之前执行
System.out.println("MyBeanPostProcessor-->后置处理postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//此方法在bean的生命周期初始化之后执行
System.out.println("MyBeanPostProcessor-->后置处理postProcessAfterInitialization");
return bean;
}
}
测试方法:
/**
* 1、实例化,调用无参构造
* 2、实例注入,调用set
* 3、后置处理的postProcessBeforeInitialization方法
* 4、初始化,需要通过bean的init-method属性指定初始化方法
* 使用bean
* 5、后置处理的postProcessAfterInitialization方法
* 6、IOC容器关闭时销毁,需要使用bean的destroy-method属性指定销毁方法
*
* bean的作用域是单例时,在创建IOC容器时,bean实例就会被创建
* 作用域是多例时,只有在获取bean实例时才会被创建
*
* bean的后置处理器会在生命周期的初始化前后添加额外的操作,
* 需要实现BeanPostProcessor接口,
* 且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,
* 而是针对IOC容器中所有bean都会执行
*/
@Test
public void test(){
//ClassPathXmlApplicationContext扩展了刷新和销毁的子方法
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = ioc.getBean(User.class);
System.out.println("获取bean并使用:"+user);
ioc.close();
}
结果:

FactoryBean是Spring提供的一种整合第三方框架的常用机制。
和普通的bean不同,配置一个FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是getObject()方法的返回值。
通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来,只把最简洁的使用界面展示给我们。
将来我们整合Mybatis时,Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。
一句话:IOC容器会创建工厂bean
getObject方法返回的实例类型,不会去创建工厂bean的实例。这样我们直接从ioc容器中获取工厂创建的实例对象
接口中的三个方法:
当把FactoryBean的实现类配置为bean时,会将当前类中的getObject方法返回的对象交给IOC容器管理
public class UserFactoryBean implements FactoryBean<User> {
@Override
public User getObject() throws Exception {
return new User();
}
@Override
public Class<?> getObjectType() {
return User.class;
}
}
public class FactoryTest {
@Test
public void test(){
// 在配置文件中只需要配置FactoryBean即可
// 当把FactoryBean的实现类配置为bean时,
// 真正交给IOC容器管理的对象,是FactoryBean工厂中getObject方法返回的对象
// 也就是说,省略了传统的工厂模式从工厂实例中获取产品的步骤,
// 而是直接把工厂的产品交给了ioc容器管理
// 另外,还可以设置是否为单例
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-factory.xml");
User user = ioc.getBean(User.class);
System.out.println(user);
}
}
根据指定的策略,在IOC容器中匹配某个bean,自动为为bean中的类类型的属性或者接口类型的属性赋值
可以通过bean标签的autowire属性设置自动装配的策略
自动装配的策略:
no,default:表示不装配,即bean中的属性不会自动匹配某个bean为某个属性赋值
byType:根据赋值的属性的类型,在IOC容器中匹配某个bean为属性赋值
异常情况:
byName:将要赋值的属性的属性名作为bean的id在IOC容器中匹配某个bean,为属性赋值
总结:一般使用byType。特殊情况下:当类型匹配的bean有多个时,此时可以使用byName实现自动装配
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.zylai.spring.controller.UserController"
autowire="byType">
bean>
<bean id="userService"
class="com.zylai.spring.service.impl.UserServiceImpl"
autowire="byType">
bean>
<bean id="userDao" class="com.zylai.spring.dao.impl.UserDaoImpl">bean>
beans>
这四个注解本质和功能上完全一样,后面三个相当于Component改了个名字,但是对于开发人员便于理解
注意:
在service层和dao层,注解应该标识在接口的实现类上
加了注解的类在IOC容器中的默认id为类名的小驼峰
<contxt:component-scan base-package="com.zylai.spring">
contxt:component-scan>
通过注解加扫描所配置的bean的id,默认值为类名的小驼峰,即类名的首字母为小写的结果,注意,接口应该是其实现类。可以通过标识组件注解的value属性设置bean的自定义的id
@Autowired默认通过byType方式自动注入,在IOC容器中通过类型匹配某个bean为属性赋值
若有多个类型匹配的bean,此时会自动转化为byName的方式来实现自动装配的效果
即将要赋值的属性的属性名作为bean的id匹配某个bean为属性赋值
若byType和byName的方式都无法实现自动装配,即IOC容器中有多个类型匹配的bean,且这些bean的id和要复制的属性的属性名都不一致,此时抛异常。
此时可以在要赋值的属性上,添加一个注解@Qualifier("value")通过该注解的value属性值,指定某个bean的id,然后将这个bean为属性赋值
注意:若IOC容器中没有任何一个类型匹配bean,此时抛出异常:NoSuchBeanDefinitionException
在@Autowired注解中有个required属性,默认值为true,要求必须完成自动装配可以将required设置为false,此时能装配则装配,无法装配则使用属性的默认值
controller:
@Controller
public class UserController {
@Autowired
private UserService userService;
public void saveUser(){
userService.saveUser();
}
}
service
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void saveUser() {
System.out.println("保存信息-->service");
userDao.saveUser();
}
}
dao
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void saveUser() {
System.out.println("保存成功-->dao");
}
}

二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。
让不属于目标方法核心逻辑的代码从目标方法中剥离出来——解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。

静态代理的思想就是代理对象和目标对象都实现同一个接口,然后在代理对象中调用目标对象的方法。
接口:
public interface Calculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
目标对象:
public class CalculatorImpl implements Calculator{
@Override
public int add(int i, int j) {
int result = i + j;
System.out.println("方法内部,result:"+result);
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
System.out.println("方法内部,result:"+result);
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
System.out.println("方法内部,result:"+result);
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
System.out.println("方法内部,result:"+result);
return result;
}
}
代理对象:
public class CalculatorStaticProxy implements Calculator {
// 将被代理的目标对象声明为成员变量
private Calculator target;
public CalculatorStaticProxy(Calculator target) {
this.target = target;
}
@Override
public int add(int i, int j) {
// 附加功能由代理类中的代理方法来实现
System.out.println("[日志] add 方法开始了,参数是:" + i + "," + j);
// 通过目标对象来实现核心业务逻辑
int addResult = target.add(i, j);
System.out.println("[日志] add 方法结束了,结果是:" + addResult);
return addResult;
}
}
可以看到,通过静态代理,达到了我们想要的目标。
缺点:代理都写死了,不具备灵活性。比如将来要给其他的类加上这个日志功能,那么还需要创建很多的代理类,产生大量重复的代码。
比如说,将日志功能都集中到一个代理类中,将来有任何的日志需求,都通过这一个代理类实现。这里就用到了动态代理的技术。
使用JDK动态代理:
public class ProxyFactory {
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
public Object getProxy(){
//使用JDK动态代理
/**
* 必须给出类加载器,根据类加载器来创建类
* ClassLoader loader:指定目标对象使用的类加载器
* Class>[] interfaces:获取目标对象实现的所有接口的class对象的数组
* InvocationHandler h :设置代理类中的抽象方法如何重写
*/
ClassLoader classLoader = target.getClass().getClassLoader();
Class<?>[] interfaces = target.getClass().getInterfaces();
InvocationHandler h = new InvocationHandler() {
//这里是设置代理类中的方法如何重写
//proxy:表示代理对象,method表示要执行的方法,args表示要执行的方法的参数列表
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object res = null;
try {
System.out.println("日志,方法:"+method.getName()+",参数:"+ Arrays.toString(args));
//调用目标对象的方法
res = method.invoke(target, args);
System.out.println("日志,方法:"+method.getName()+",结果:"+res);
return res;
} catch (Exception e) {
e.printStackTrace();
System.out.println("日志,方法:"+method.getName()+",异常:"+e);
} finally {
System.out.println("日志,方法:"+method.getName()+",方法执行完毕");
}
return res;
}
};
return Proxy.newProxyInstance(classLoader,interfaces,h);
}
}
ProxyFactory proxyFactory = new ProxyFactory(new CalculatorImpl());
Calculator proxy = (Calculator)proxyFactory.getProxy();
proxy.add(1,2);
AOP(Aspect Oriented Programming)面向切面编程,是一种设计思想,是面向对象编程的一种补充和完善。它以通过预编译方式和运行期动态代理方式实现在不修改源代码的情况下给程序动态统一添加额外功能的一种技术
简单来说:就是把非核心业务抽取出来,给切面类管理。再把抽取出来的放到相应的位置。
从目标对象中抽取出来的非核心业务,比如之前代理模式中的日志功能,针对于计算器功能来说,日志就是非核心业务。
这个概念不是语法层面天然存在的,而是根据附加功能的逻辑上的需要:有十个附加功能,就有十个横切关注点。

非核心的业务再目标对象中叫做横切关注点,将横切关注点抽取出来封装到切面类中,他就是这个类中的一个方法叫做通知。
每一个横切关注点要做的事情都封装成一个方法,这样的方法就叫做通知方法。

封装横切关注点的类,通知的方法都写在切面类中

被代理的目标对象,比如计算器的实现类
代理对象
一个纯逻辑的概念:抽取横切关注点的位置,比如方法执行之前,方法捕获异常的时候等等。
连接点的作用:我们不但要抽取出来,还要套回去。

定位连接点的方式。
抽和套,抽取出来横切关注点,封装到切面中,就是一个通知。然后通过切入点找到连接点,把通知套到连接点的位置。

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.3.1version>
dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.zylai.spring.aop.annotation"/>
<aop:aspectj-autoproxy/>
beans>
在切面中,需要通过指定的注解将方法标识为通知方法
切入点表达式:设置在表示通知的注解的value属性中
* "execution(public int com.zylai.spring.aop.annotation.CalculatorImpl.add(int,int))"
* "execution(* com.zylai.spring.aop.annotation.*.*(..))"
* 第一个*表示任意的访问修饰符和返回值类型
* 第二个*表示包下所有的类
* 第三个*表示类中任意的方法
* ..表示任意的参数列表
重用连接点表达式
* //声明一个公共的切入点表达式
* @Pointcut("execution(* com.zylai.spring.aop.annotation.CalculatorImpl.*(..))")
* public void pointCut(){}
* 使用方式: @After("pointCut()")
重用连接点表达式
* 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应的方法信息
* //获取连接点对应方法的签名信息
* Signature signature = joinPoint.getSignature();
* //获取连接点所对应的参数
* Object[] args = joinPoint.getArgs();
切面的优先级
* 可以通过@Order注解的value属性设置优先级,默认值为Integer.MAX
* value值越小优先级越高
总的:
* 1. 在切面中,需要通过指定的注解将方法标识为通知方法
*
* 2. 切入点表达式:设置在表示通知的注解的value属性中
* "execution(public int com.zylai.spring.aop.annotation.CalculatorImpl.add(int,int))"
* "execution(* com.zylai.spring.aop.annotation.*.*(..))"
* 第一个*表示任意的访问修饰符和返回值类型
* 第二个*表示包下所有的类
* 第三个*表示类中任意的方法
* ..表示任意的参数列表
*
* 3.重用连接点表达式
* //声明一个公共的切入点表达式
* @Pointcut("execution(* com.zylai.spring.aop.annotation.CalculatorImpl.*(..))")
* public void pointCut(){}
* 使用方式: @After("pointCut()")
*
*
* 4. 获取连接点信息
* 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应的方法信息
* //获取连接点对应方法的签名信息
* Signature signature = joinPoint.getSignature();
* //获取连接点所对应的参数
* Object[] args = joinPoint.getArgs();
*
* 5.切面的优先级
* 可以通过@Order注解的value属性设置优先级,默认值为Integer.MAX
* value值越小优先级越高
@Component
@Aspect//将当前组件表示为切面
public class LoggerAspect {
@Pointcut("execution(* com.zylai.spring.aop.annotation.CalculatorImpl.*(..))")
public void pointCut(){}
// @Before("execution(public int com.zylai.spring.aop.annotation.CalculatorImpl.add(int,int))")
//表示这个类下所有的方法,用*表示所有,参数列表用..表示所有的参数列表
// @Before("execution(* com.zylai.spring.aop.annotation.CalculatorImpl.*(..))")
@Before("pointCut()")
public void beforeAdviceMethod(JoinPoint joinPoint){
//获取连接点对应方法的签名信息(签名信息就是方法的声明信息)
Signature signature = joinPoint.getSignature();
//获取连接点所对应的参数
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect,前置通知,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
}
@After("pointCut()")
public void afterAdviceMethod(JoinPoint joinPoint){
//获取连接点对应方法的签名信息(签名信息就是方法的声明信息)
Signature signature = joinPoint.getSignature();
System.out.println("LoggerAspect,后置通知,方法:"+signature.getName()+",执行完毕");
}
//在返回通知中若要获取目标对象方法的返回值,只需要通过注解的returning属性值
//就可以将通知方法的某个参数指定为接收目标对象方法的返回值
@AfterReturning(value = "pointCut()",returning = "result")
public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
Signature signature = joinPoint.getSignature();
System.out.println("LoggerAspect,返回通知,方法:"+signature.getName()+",结果:"+result);
}
//在返回通知中若要获取目标对象方法的异常,只需要通过注解的throwing属性值
//就可以将通知方法的某个参数指定为接收目标对象方法出现的异常
@AfterThrowing(value = "pointCut()",throwing = "ex")
public void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable ex){
Signature signature = joinPoint.getSignature();
System.out.println("LoggerAspect,异常通知,方法:"+signature.getName()
+"异常:"+ex);
}
@Around("pointCut()")
//环绕通知的方法返回值一定要和目标方法的返回值一致
public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
Object result = null;
try {
System.out.println("环绕通知-->前置通知");
//表示目标对象方法的执行
result = joinPoint.proceed();
System.out.println("环绕通知-->返回通知");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("环绕通知-->异常通知");
}finally {
System.out.println("环绕通知-->后置通知");
}
return result;
}
}
在Spring中,提供了封装了jdbc的JdbcTemplate,在之后的事务模块中,我们使用JdbcTemplate执行SQL
maven
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>5.3.1version>
dependency>
配置文件:
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
//指定当前测试类在spring的测试环境中执行,此时就可以通过注入的方法直接获取IOC容器的bean
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring测试环境的配置文件
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testInsert(){
String sql = "insert into t_user values(null,?,?,?,?,?)";
int update = jdbcTemplate.update(sql, "root", "123", 23, "女", "123@163.com");
System.out.println(update);
}
@Test
public void testGetUserById(){
String sql = "select * from t_user where id = ?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);
System.out.println(user);
}
@Test
public void testGetAllUser(){
String sql = "select * from t_user";
List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
System.out.println(list);
}
@Test
public void testGetCount(){
String sql = "select count(*) from t_user";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(count);
}
}

既然事务控制的代码有规律可循,代码的结构基本是确定的,所以框架就可以将固定模式的代码抽取出来,进行相关的封装。
封装起来的好处:
编程式:自己写代码实现功能
声明式:通过配置让框架实现功能
用户购买图书,先查询图书的价格,再更新图书的库存和用户的余额 假设用户id为1的用户,购买id为1的图书 ,用户余额为50,而图书价格为80 购买图书之后,用户的余额为-30,数据库中余额字段设置了无符号,因此无法将-30插入到余额字段 ,此时执行sql语句会抛出SQLException
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.zylai.spring">
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
beans>
在需要进行事务操作的类或方法上加上注解即可
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
@Transactional
public void buyBook(Integer userId, Integer bookId) {
//查询图书的价格
Integer price = bookDao.getPriceByBookId(bookId);
//更新图书的库存
bookDao.updateStock(bookId);
//更新用户的余额
bookDao.updateBalance(userId,price);
}
}
结果:如果更新图书的库存顺利执行,而更新用户余额执行失败,那么将会回滚事务,图书库存将会恢复。
介绍
对一个查询操作来说,如果我们把它设置成只读,就能够明确告诉数据库,这个操作不涉及写操作。这样数据库就能够针对查询操作来进行优化
使用方式
@Transactional(readOnly = true)
public void buyBook(Integer userId, Integer bookId) {
//查询图书的价格
Integer price = bookDao.getPriceByBookId(bookId);
//更新图书的库存
bookDao.updateStock(bookId);
//更新用户的余额
bookDao.updateBalance(userId,price);
}
注意:
对增删改操作设置只读会抛出下面异常:
Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
介绍:
事务在执行过程中,有可能因为遇到某些问题,导致程序卡住,从而长时间占用数据库资源。而长时间占用资源,大概率是因为程序运行出现了问题(可能是Java程序或MySQL数据库或网络连接等等)。
此时这个很可能出问题的程序应该被回滚,撤销它已做的操作,事务结束,把资源让出来,让其他正常程序可以执行。
概括来说就是一句话:超时回滚,释放资源。
使用方式:
@Transactional(timeout = 3)
public void buyBook(Integer bookId, Integer userId) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//查询图书的价格
Integer price = bookDao.getPriceByBookId(bookId);
//更新图书的库存
bookDao.updateStock(bookId);
//更新用户的余额
bookDao.updateBalance(userId, price);
//System.out.println(1/0);
}
观察结果
执行过程中抛出异常:
org.springframework.transaction.TransactionTimedOutException: Transaction timed out:
deadline was Fri Jun 04 16:25:39 CST 2022
声明式事务默认对于运行时异常都进行回滚,一般使用的是在此基础上加上不因为哪个异常而回滚
可以通过@Transactional中相关属性设置回滚策略
@Transactional(noRollbackFor = ArithmeticException.class)
//@Transactional(noRollbackForClassName = "java.lang.ArithmeticException")
public void buyBook(Integer bookId, Integer userId) {
//查询图书的价格
Integer price = bookDao.getPriceByBookId(bookId);
//更新图书的库存
bookDao.updateStock(bookId);
//更新用户的余额
bookDao.updateBalance(userId, price);
System.out.println(1/0);
}
数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事务与其他事务隔离的程度称为隔离级别。SQL标准中规定了多种事务隔离级别,不同隔离级别对应不同的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱。
隔离级别一共有四种:
读未提交:READ UNCOMMITTED
允许Transaction01读取Transaction02未提交的修改。
读已提交:READ COMMITTED、
要求Transaction01只能读取Transaction02已提交的修改。
可重复读:REPEATABLE READ
确保Transaction01可以多次从一个字段中读取到相同的值,即Transaction01执行期间禁止其它事务对这个字段进行更新。
串行化:SERIALIZABLE
确保Transaction01可以多次从一个表中读取到相同的行,在Transaction01执行期间,禁止其它事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
各个隔离级别解决并发问题的能力见下表:
| 隔离级别 | 脏读 | 不可重复读 | 幻读 |
|---|---|---|---|
| READ UNCOMMITTED | 有 | 有 | 有 |
| READ COMMITTED | 无 | 有 | 有 |
| REPEATABLE READ | 无 | 无 | 有 |
| SERIALIZABLE | 无 | 无 | 无 |
各种数据库产品对事务隔离级别的支持程度:
| 隔离级别 | Oracle | MySQL |
|---|---|---|
| READ UNCOMMITTED | × | √ |
| READ COMMITTED | √(默认) | √ |
| REPEATABLE READ | × | √(默认) |
| SERIALIZABLE | √ | √ |
使用:
@Transactional(isolation = Isolation.DEFAULT)//使用数据库默认的隔离级别
@Transactional(isolation = Isolation.READ_UNCOMMITTED)//读未提交
@Transactional(isolation = Isolation.READ_COMMITTED)//读已提交
@Transactional(isolation = Isolation.REPEATABLE_READ)//可重复读
@Transactional(isolation = Isolation.SERIALIZABLE)//串行化
简单来说:
结账,买两本书。
如果以结账时间为事务,第二本买失败,第一本一会回滚。
如果用买书本身的操作,就是能买几本就是几本。
详细介绍:
当事务方法被另一个事务方法调用时,必须指定事务应该如何传播。例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行。