Spring5笔记

一、Spring概述

1.1、特点

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)。
1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring 可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC(Inversion of Control ):控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop(Aspect-Oriented Programming):面向切面,不修改源代码进行功能增强
4、优点
(1)方便解耦,简化开发
(2)Aop 编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低 API 开发难度

1.2、四个基本包

在这里插入图片描述在这里插入图片描述

1.3、创建一个demo

1、导包

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.16</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.16</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>5.3.16</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.16</version>
    </dependency>

    <!--日志-->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2、创建pojo

package com.qdd.pojo;

public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、配置bean

<?xml version="1.0" encoding="UTF-8"?>
<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.qdd.pojo.User" id="user"/>
</beans>

4、测试

public class Mytest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = context.getBean("user",User.class);
        user.setName("赵贺齐");
        user.setAge(22);
        System.out.println(user);
    }
}

二、IOC 容器

2.1、IOC概念和原理

(1)概念:

控制反转,把对象创建和对象的调用过程交给spring进行管理。

目的:降低耦合度。

底层原理:xml,反射,工厂模式

Spring提供IOC容器两种实现方式(两个接口)

BeanFactory:Spring内部使用的接口,不提倡开发人员使用。特点:加载配置文件时不会创建对象,获取对象时才会创建对象。

**ApplicationContext:**BeanFactory的子接口,提供了更多更强大的功能,一般由开发人员使用。特点:加载配置文件时会把配置文件里的对象进行创建。

ApplicationContext两个常用实现类:

FileSystemXmlApplicationContext:绝对路径,从盘符开始算起
ClassPathXmlApplicationContext:相对路径,从src开始算起

什么是Bean管理?Bean管理是指两个操作:Spring创建对象 和 Spring注入属性

Bean管理有两种操作方式:基于xml配置文件方式实现 和 基于注解方式实现

2.2、IOC操作Bean管理(基于xml)

xml实现Bean管理:

2.2.1、基于xml方式创建对象:

在Spring配置文件中使用bean标签来创建对象
bean标签有很多属性,常用属性:
id:唯一标识
class:类路径
创建对象时,默认执行无参构造函数

2.2.2、基于xml方式注入属性

1、 三种方式

第一种方法:使用set方法进行注入:

首先先为类的属性提供set方法:

public class User {

    private String userName;
    private String userAge;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserAge() {
        return userAge;
    }
}

然后在xml配置文件中通过property标签进行属性注入

    <!--配置User对象-->
    <bean id="user" class="com.oymn.spring5.User">
        <property name="userName" value="haha"></property>
        <property name="userAge" value="18"></property>
    </bean>

测试

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
    User user = applicationContext.getBean("user", User.class);
    System.out.println(user.getUserName() + "     " + user.getUserAge());

第二种方法:使用有参构造函数进行注入

首先提供有参构造方法

public class User {

    private String userName;
    private String userAge;

    public User(String userName, String userAge){
        this.userName = userName;
        this.userAge = userAge;
    }
}

然后再xml配置文件中通过constructor-arg标签进行属性注入

    <!--配置User对象-->
    <bean id="user" class="com.oymn.spring5.User">
        <constructor-arg name="userName" value="haha"></constructor-arg>
        <constructor-arg name="userAge" value="18"></constructor-arg>
    </bean>

第三种方法:p名称空间注入(了解即可)

首先在xml配置文件中添加p名称空间,并且在bean标签中进行操作
在这里插入图片描述
然后提供set方法

public class User {

    private String userName;
    private String userAge;

    public User() {
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
}

第二步 进行属性注入,在 bean 标签里面进行操作

<bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" 
p:bauthor="无名氏"></bean>
2、xml注入其他属性

(1)、null值

    <!--配置User对象-->
    <bean id="user" class="com.oymn.spring5.User">
        <property name="userName"> <null/> </property>
    </bean>

(2)属性值包含特殊符号

<!--属性值包含特殊符号
 1 把<>进行转义 &lt; &gt;
 2 把带特殊符号内容写到 CDATA
-->
<property name="address">
 <value><![CDATA[<<南京>>]]></value>
</property> 

(3)、注入属性-外部 bean
1、创建两个类 service 类和 dao 类
2、在 service 调用 dao 里面的方法
3、在 spring 配置文件中进行配置

public class UserService {
 //创建 UserDao 类型属性,生成 set 方法
 private UserDao userDao;
 public void setUserDao(UserDao userDao) {
 this.userDao = userDao;
 }
 public void add() {
 System.out.println("service add...............");
 userDao.update();
 } }
<!--1 service 和 dao 对象创建-->
<bean id="userService" class="com.atguigu.spring5.service.UserService">
<!--注入 userDao 对象
name 属性:类里面属性名称
ref 属性:创建 userDao 对象 bean 标签 id 值
-->
	<property name="userDao" ref="userDaoImpl"></property>
</bean> 
<bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>

(4)、注入属性-外部 bean
1、创建两个类 service 类和 dao 类
2、在 service 调用 dao 里面的方法
3、在 spring 配置文件中进行配置

public class UserService {
 //创建 UserDao 类型属性,生成 set 方法
 private UserDao userDao;
 public void setUserDao(UserDao userDao) {
 this.userDao = userDao;
 }
 public void add() {
 System.out.println("service add...............");
 userDao.update();
 } }
<!--1 service 和 dao 对象创建--> 
<bean id="userService" class="com.atguigu.spring5.service.UserService">
<!--注入 userDao 对象
name 属性:类里面属性名称
ref 属性:创建 userDao 对象 bean 标签 id 值
-->
	<property name="userDao" ref="userDaoImpl"></property>
	</bean>
<bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean> 

(5)、注入属性-内部 bean
1、一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
2、在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

//部门类
public class Dept {
 private String dname;
 public void setDname(String dname) {
 this.dname = dname;
 } }
//员工类
public class Emp {
 private String ename;
 private String gender;
 //员工属于某一个部门,使用对象形式表示
 private Dept dept;
 public void setDept(Dept dept) {
 this.dept = dept;
 }
 public void setEname(String ename) {
 this.ename = ename;
 }
 public void setGender(String gender) {
 this.gender = gender;
 } }

3、在 spring 配置文件中进行配置

<!--内部 bean--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
	<property name="ename" value="lucy"></property>
	<property name="gender" value=""></property>
<!--设置对象类型属性-->
	<property name="dept">
	<bean id="dept" class="com.atguigu.spring5.bean.Dept">
		<property name="dname" value="安保部"></property>
	</bean>
	</property>
</bean>

(6)、注入属性-集合
1、创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

public class Stu {
 //1 数组类型属性
 private String[] courses;
 //2 list 集合类型属性
 private List<String> list;
 //3 map 集合类型属性
 private Map<String,String> maps;
 //4 set 集合类型属性
 private Set<String> sets;
 public void setSets(Set<String> sets) {
 this.sets = sets;
 }
 public void setCourses(String[] courses) {
 this.courses = courses;
 }
 public void setList(List<String> list) {
 this.list = list;
 }
 public void setMaps(Map<String, String> maps) {
 this.maps = maps;
 } }

2、在 spring 配置文件进行配置

<!--1 集合类型属性注入-->
 <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
	 <!--数组类型属性注入-->
 	<property name="courses">
 		<array>
 			<value>java 课程</value>
			 <value>数据库课程</value>
 		</array>
 	</property>
 	<!--list 类型属性注入-->
	 <property name="list">
 		<list>
 			<value>张三</value>
 			<value>小三</value>
 		</list>
 	</property>
	 <!--map 类型属性注入-->
 	<property name="maps">
		 <map>
 			<entry key="JAVA" value="java"></entry>
 			<entry key="PHP" value="php"></entry>
 		</map>
 	</property>
 	<!--set 类型属性注入-->
	 <property name="sets">
 		<set>
			 <value>MySQL</value>
			 <value>Redis</value>
		 </set>
	 </property>
</bean>

(7)在集合里面设置对象类型值

<!--创建多个 course 对象--> 
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
 	<property name="cname" value="Spring5 框架"></property>
</bean> <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
 	<property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象--> <property name="courseList">
 <list>
 	<ref bean="course1"></ref>
 	<ref bean="course2"></ref>
 </list>
</property>

(8)把集合注入部分提取出来
1、在 spring 配置文件中引入名称空间 util

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util.xsd">

2、使用 util 标签完成 list 集合注入提取

<!--1 提取 list 集合类型属性注入--> 
<util:list id="bookList">
	<value>易筋经</value>
	<value>九阴真经</value>
	<value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用--> 
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
	<property name="list" ref="bookList"></property>
</bean>

(9)FactoryBean
1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

2、普通 bean:在配置文件中定义 bean 类型就是返回类型

3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean

public class MyBean implements FactoryBean<Course> {
 //定义返回 bean
 @Override
 public Course getObject() throws Exception {
 Course course = new Course();
 course.setCname("abc");
 return course;
 }
 @Override
 public Class<?> getObjectType() {
 return null;
 }
 @Override
 public boolean isSingleton() {
 return false;
 } }

第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

<bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
</bean>

第三部 测试

@Test
public void test3() {
 ApplicationContext context =
 new ClassPathXmlApplicationContext("bean3.xml");
 Course course = context.getBean("myBean", Course.class);
 System.out.println(course);
}

2.2.3 、xml 自动装配

1、什么是自动装配

(1)根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入

2、演示自动装配过程

(1)根据属性名称自动注入

<!--实现自动装配
 bean 标签属性 autowire,配置自动装配
 autowire 属性常用两个值:
 byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
 byType 根据属性类型注入
-->
<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
 	<!--<property name="dept" ref="dept"></property>-->
</bean> 
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean> 

(2)根据属性类型自动注入

<!--实现自动装配
 bean 标签属性 autowire,配置自动装配
 autowire 属性常用两个值:
 byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
 byType 根据属性类型注入
-->
<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
 	<!--<property name="dept" ref="dept"></property>-->
</bean> 
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>

2.2.4、引入外部属性文件配置数据库连接池

(1)创建外部属性文件,properties 格式文件,写数据库信息
在这里插入图片描述
(2)把外部 properties 属性文件引入到 spring 配置文件中
引入 context 名称空间

<?xml version="1.0" encoding="UTF-8"?>
<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
       https://www.springframework.org/schema/context/spring-context.xsd">
    
<!--    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">-->
<!--        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>-->
<!--        <property name="url" value="jdbc:mysql://localhost:3306/qq"/>-->
<!--        <property name="username" value="root"/>-->
<!--        <property name="password" value="123456"/>-->
<!--    </bean>-->

    <context:property-placeholder location="classpath:jdbc.properties"/>
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
            <property name="driverClassName" value="${prop.driverClassName}"/>
            <property name="url" value="${prop.url}"/>
            <property name="username" value="${prop.username}"/>
            <property name="password" value="${prop.password}"/>
        </bean>
</beans>

2.3、IOC 操作 Bean 管理(基于注解方式)

2.3.1、什么是注解

(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置

2.3.2、Spring 针对 Bean 管理中创建对象提供注解

(1)@Component
(2)@Service
(3)@Controller
(4)@Repository

  • 上面四个注解功能是一样的,都可以用来创建 bean 实例

2.3.3、基于注解方式实现对象创建

第一步 引入依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.16</version>
        </dependency>

第二步 开启组件扫描

<!--开启组件扫描
 1 如果扫描多个包,多个包使用逗号隔开
 2 扫描包上层目录
--><context:component-scan base-package="com.atguigu"></context:component-scan>

第三步 创建类,在类上面添加创建对象注解

//在注解里面 value 属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
 public void add() {
 System.out.println("service add.......");
 } }

第四步 开启组件扫描细节配置

<!--示例 1
 use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
 context:include-filter ,设置扫描哪些内容
--><context:component-scan base-package="com.atguigu" use-defaultfilters="false">
 <context:include-filter type="annotation" 
 
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--示例 2
 下面配置扫描包所有内容
 context:exclude-filter: 设置哪些内容不进行扫描
--><context:component-scan base-package="com.atguigu">
 <context:exclude-filter type="annotation" 
 
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

2.3.4、基于注解方式实现属性注入

(1)@Autowired:根据属性类型进行自动装配
(2)@Qualifier:根据名称进行注入
这个@Qualifier 注解的使用,和上面@Autowired 一起使用
@Autowired //根据类型进行注入
@Qualifier(value = “userDaoImpl1”) //根据名称进行注入
private UserDao userDao;
(3)@Resource:可以根据类型注入,可以根据名称注入
//@Resource //根据类型进行注入
@Resource(name = “userDaoImpl1”) //根据名称进行注入
private UserDao userDao; (4)@Value:注入普通类型属性
@Value(value = “abc”)
private String name;

2.3.5、完全注解开发

(1)创建配置类,替代 xml 配置文件
@Configuration //作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = {“com.atguigu”})
public class SpringConfig {
}
(2)编写测试类
@Test
public void testService2() {
//加载配置类
ApplicationContext context
= new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean(“userService”,
UserService.class);
System.out.println(userService);
userService.add();
}

2.4、Bean作用域

1、在 Spring 里面,默认情况下,bean 是单实例对象
在这里插入图片描述
2、如何设置单实例还是多实例
(1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例
(2)scope 属性值
第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象
(3)singleton 和 prototype 区别

singletonprototype
单例多例
加载 spring 配置文件时候就会创建单实例对象调用getBean 方法时候创建多实例对象

2.5、 bean生命周期

(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
(4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
(6)bean 可以使用了(对象获取到了)
(7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

创建实体类

package com.qdd.pojo;

public class User {
    private String name;

    public User() {
        System.out.println("第一步,构造方法");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("第二步,调用了setname");
    }

    public void initMethod(){
        System.out.println("第四部,初始化方法");
    }

    public void destroyMethod(){
        System.out.println("第七步,销毁方法");
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

创建后置处理器,实现BeanpostProcess

package com.qdd.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第三部,初始化之前");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第五步,初始化之后");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

配置两个类的bean

<?xml version="1.0" encoding="UTF-8"?>
<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.qdd.pojo.User" id="user" scope="singleton"
          init-method="initMethod" destroy-method="destroyMethod" >
        <property name="name" value="赵贺齐"></property>
    </bean>

    <bean class="com.qdd.pojo.MyBeanPost" id="beanPost"></bean>
</beans>

最后,测试

import com.qdd.pojo.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    @Test
    public void test1() throws ClassNotFoundException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user1 = context.getBean("user",User.class);
        System.out.println("第六部,可以用了");
        context.close();
    }
}

控制台输出
在这里插入图片描述

三、Aop

3.1、 底层原理

底层通过动态代理来实现:

第一种:有接口的情况,使用JDK动态代理:创建接口实现类的代理对象。
第二种:无接口的情况,使用CGLIB动态代理:创建当前类子类的代理对象。
JDK动态代理举例:
通过 java.lang.reflect.Proxy类 的 newProxyInstance方法 创建代理类。

newProxyInstance方法:

  1. 参数一:类加载器
  2. 参数二:所增强方法所在的类,这个类实现的接口,支持多个接口
  3. 参数三:实现InvocationHandle接口,重写invoke方法来添加新的功能
    代码举例:
public interface UserDao {
    public int add(int a,int b);
    public String update(String id);
}
public class UserDaoImpl implements UserDao {
    @Override
    public int add(int a, int b) {
        return a + b;
    }
    @Override
    public String update(String id) {
        return id;
    }
}
import com.qdd.dao.UserDao;
import com.qdd.dao.impl.UserDaoImpl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy {
    public static void main(String[] args) {
        Class[] interfaces = {UserDao.class};
        //创建接口实现代理对象
//        Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                return null;
//            }
//        });

        UserDaoImpl userDao = new UserDaoImpl();
        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
        String result = dao.update("1");
        System.out.println("result=" + result);

    }
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
    //把要创建代理的对象传过来
    private Object obj;

    public UserDaoProxy(Object obj) {
        this.obj = obj;
    }

    //增强的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法执行之前
        System.out.println("方法执行之前,方法名:" + method.getName() + "\n传递的参数:" + args.toString());

        //执行被增强的方法
        Object res = method.invoke(obj, args);

        //方法执行之后
        System.out.println("方法执行之后" + obj);

        return res;
    }
}

结果:
​​在这里插入图片描述

3.2、 基于AspectJ实现AOP操作

3.2.1、AOP相关术语

  • 连接点:类中可以被增强的方法,称为连接点。
  • 切入点:实际被增强的方法,称为切入点。
  • 切面:指把通知应用到切入点这一个动作。
  • 通知:增强的那一部分逻辑代码。通知有多种类型:
  1. 前置通知:增强部分代码在原代码前面。
  2. 后置通知:增强部分代码在原代码后面。
  3. 环绕通知:增强部分代码既有在原代码前面,也有在原代码后面。
  4. 异常通知:原代码发生异常后才会执行。
  5. 最终通知:类似与finally那一部分

3.2.2、切入点表达式

语法:execution([权限修饰符] [返回类型] [类全路径] [方法名称] [参数列表])

  • 举例1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强
execution(* com.auguigu.dao.BookDao.add(..))
  • 举例2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强
execution(* com.atguigu.dao.BookDao.*(..))
  • 举例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强
execution(* com.atguigu.dao.*.* (..))

3.2.3、基于AspectJ实现AOP有两种方式

  1. 基于注解方法
import org.springframework.stereotype.Component;

@Component
public class User {
    public void add(){
        System.out.println("add()......");
    }
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class UserProxy {
    @Before("execution(* com.qdd.User.add(..))")
    public void before(){
        System.out.println("前置通知。。。。。");
    }
    @AfterReturning("execution(* com.qdd.User.add(..))")
    public void afterReturning(){
        System.out.println("后置通知。。。。。");
    }
    @After("execution(* com.qdd.User.add(..))")
    public void after(){
        System.out.println("最终通知。。。。。");
    }
    @Around("execution(* com.qdd.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前。。。。。");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后。。。。。");
    }
    @AfterThrowing("execution(* com.qdd.User.add(..))")
    public void afterThrowing(){
        System.out.println("异常通知。。。。。");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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.qdd"/>

    <aop:aspectj-autoproxy/>
</beans>
public class MyTest {
    @Test
    public void test1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }
}

结果:
在这里插入图片描述
注意:spring版本不一样,执行顺序可能不一样
2. 基于xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
   
   <bean class="com.qdd.User" id="user"/>
   <bean class="com.qdd.UserProxy" id="userProxy"/>
   
   <aop:config>
       <aop:pointcut id="p" expression="execution(* com.qdd.User.add(..))"/>
       <aop:aspect ref="userProxy">
           <aop:before method="before" pointcut-ref="p"/>
           <aop:after-returning method="afterReturning" pointcut-ref="p"/>
           <aop:after method="after" pointcut-ref="p"/>
           <aop:after-throwing method="afterThrowing" pointcut-ref="p"/>
           <aop:around method="around" pointcut-ref="p"/>
       </aop:aspect>
   </aop:config>
</beans>

四、JdbcTemplate

空降、空降

五、事务管理

事务是数据库操作最基本单位,要么都成功,要么都失败。

典型场景:转账

5.1、事务四个特性ACID:

  1. 原子性(atomicity)

事务的原子性是指一个事务中的所有操作是不可分割的,必须是一个逻辑单元,只能是全部执行成功或者全部执行失败。

  1. 一致性(consistency)

一旦所有事务动作完成,事务就要被提交。数据和资源处于一种* 满足业务规则 * 的一致性状态中

  1. 隔离性(isolation)

可能多个事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏

  1. 持久性(durability)

事务一旦完成,无论系统发生什么错误,结果都不会受到影响。通常情况下,事务的结果被写到持久化存储器中

5.2、声明式事务管理的参数

5.2.1、isolation(事务隔离级别)

有三个读问题:脏读,不可重复读,虚读(幻读)。
设置隔离级别,解决读问题:

脏读不可重复读虚读
READ UNCOMMITED(读未提交)
READ COMMITED(读已提交)
REPEATABLE READ(可重复读)
SERIALIZABLE(串行化)

5.2.2、propagation(传播行为)

行为介绍
propagation_requierd如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,这是最常见的选择。
propagation_supports支持当前事务,如果没有当前事务,就以非事务方法执行。
propagation_mandatory使用当前事务,如果没有当前事务,就抛出异常。
propagation_required_new新建事务,如果当前存在事务,把当前事务挂起。
propagation_not_supported以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
propagation_never以非事务方式执行操作,如果当前事务存在则抛出异常。
propagation_nested如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与propagation_required类似的操作

Spring 默认的事务传播行为是 PROPAGATION_REQUIRED,它适合于绝大多数的情况。

5.2.3、timeout(超时时间)

事务需要在一定时间内进行提交,超过时间后回滚。
默认值是-1,设置时间以秒为单位。

5.2.3、readOnly(是否只读)

默认值为false,表示可以查询,也可以增删改。
设置为true,只能查询。

5.2.3、rollbackFor(回滚)

设置出现哪些异常进行事务回滚。

5.2.3、noRollbackFor(不回滚)

设置出现哪些异常不进行事务回滚。

示例:

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class AccountService {

六、Spring5 新特性

6.1、 自带了日志封装

Spring5移除了Log4jConfigListener,官方建议使用Log4j2

6.2、 @Nullable注解

@Nullable注解可以用在方法上,属性上,参数上,表示方法返回值可以为空,属性可以为空,参数可以为空。

@Nullable     //表示方法返回值可以为空
public int getId();

@Nullable     //表示参数可以为空
public void setId(@Nullable int Id);

@Nullable     //表示属性可以为空
public int id;

6.3、Webflux

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crud派大星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值