Spring5
一、IOC容器
1.IOC思想
IOC:Inversion of Control,反转控制
- ①获取资源的传统方式
自己做饭:买菜、洗菜、择菜、改刀、烹煎炒等等,需要我们全过程参与,费时费力,必须清楚了解资源创建过程中的全部细节并且要熟练掌握。
在应用程序中的组件需要获取资源的时候,传统的方式是组件主动的从容器中获取所需的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式,增加了学习成本,同时降低了开发效率。
- ②反转控制获取资源
点外卖:下单、等、吃、省时省力,不必关心资源创建过程的所有细节。
反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接收资源的方式即可,极大的降低了学习成本,提高了开发效率。这种行为也称为查找的被动形式。
- ③DI
DI:Dependency Injection,依赖注入
IOC是一种反转控制的思想,而DI是对IOC的一种具体实现
1.1IOC容器在Spring中的实现
Spring的IOC容器就是IOC思想的一个落地的产品实现,IOC容器中管理的组件也叫做Bean。在创建Bean之前,首先需要创建IOC容器。Spring提供了IOC容器的两种实现方式:
①BeanFactory
这是IOC容器的基本实现,是Spring内部使用的接口。面向Spring本身,不提供给开发人员使用。
②ApplicationContext
BeanFactory的子接口,提供了更多高级特性。面向Spring的使用者,几乎所有场合都使用ApplicationContext而不是底层的BeanFactory。
③ApplicationContext的主要实现类
ClassPathXmlApplicationContext()—>通过读取路径下的XML格式的配置文件创建IOC容器对象
2.基于xml管理Bean
2.1实验一:入门案例
①创建Maven Module
②引入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
③在Spring的配置文件中配置bean
<!--
bean标签:配置一个Bean对象,将对象交给IOC容器管理
属性:
id:bean的唯一标识,不能重复
class:设置bean对象所对应的类型
-->
<bean id="helloworld" class="zyb.spring.pojo.HelloWorld"></bean>
④创建测试类测试
@Test
public void testIOC(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("Spring-ioc.xml");
//获取Bean对象
//1、根据id获取Bean对象
// Student studentOne = (Student) ioc.getBean("studentOne");
//2、根据Class类型获取Bean对象
// Student student = ioc.getBean(Student.class);
//3、根据id和Class类型获取Bean对象
Student student = ioc.getBean("studentOne", Student.class);
System.out.println(student);
}
⑤注意
当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个
当IOC容器中一共配置了两个:
<bean id="studentOne" class="zyb.spring.pojo.Student"></bean>
<bean id="studentTwo" class="zyb.spring.pojo.Student"></bean>
就会报出NoUniqueBeanDefinitionException异常
根据类型来获取Bean时,在满足Bean唯一性的前提下,其实只是看:【对象instanceof指定的类型】的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到
即通过bean的类型、bean所继承的类的类型,bean所实现的接口的类型都可以获取bean
2.2实验二:依赖注入之setter注入
依赖注入就是为当前我们的实体类的属性赋值的意思
①创建学生类
/**
* @author 一只鱼zzz
* @version 1.0
*/
public class Student {
private Integer sid;
private String sname;
private Integer age;
private String gender;
public Student() {
}
public Student(Integer sid, String sname, Integer age, String gender) {
this.sid = sid;
this.sname = sname;
this.age = age;
this.gender = gender;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
②配置bean时为属性赋值
<bean id="studentTwo" class="zyb.spring.pojo.Student">
<!--
property标签:通过成员变量的set方法进行赋值
name:设置需要赋值的属性名(和set方法有关)
value:设置为属性所赋的值
-->
<property name="sid" value="1001"></property>
<property name="sname" value="张三"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
</bean>
③测试
@Test
public void testIOC(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("Spring-ioc.xml");
//获取Bean对象
Student student = ioc.getBean("studentTwo", Student.class);
System.out.println(student);
}
2.3实验三:依赖注入之构造器注入
Spring容器
<bean id="studentThree" class="zyb.spring.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="李四"></constructor-arg>
<constructor-arg value="24"></constructor-arg>
<constructor-arg value="女"></constructor-arg>
</bean>
测试方法
@Test
public void testDI(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("Spring-ioc.xml");
Student student = ioc.getBean("studentThree", Student.class);
System.out.println(student);
}
2.4实验四:特殊值处理
①字面量赋值
什么是字面量?
int a = 10;
声明一个变量a,初始化为10,此时a就不代表字母a了,而是作为一个变量的名字。当我们引用a的时候,我们实际上拿到的值是10.
而如果a是带引号的:‘a’,那么它现在不是变量,它代表的就是a本身这个字母,这就是字面量。所以字面量没有引申含义,就是我们看到的这个数据本身。
<!--使用value属性给bean属性赋值的时候,Spring会把Value属性的值看做字面量-->
<property name="sname" value="张三"></property>
②null值
假如说我现在通过set注入,将gender属性的值赋值为null,那么此时我们输出打印的gender的属性值为‘null’这个字符串,而不是传统意义上的空值null。
要想得到传统意义上的空值null,我们必须通过使用null标签来进行设置,如下:
③xml实体
比如我想让王五改为<王五>,那么在xml解析语言中,是不能直接识别小于号或者大于号这种特殊字符的,因此我们需要通过特殊的方式引用,如下:
2.5实验五:为类类型属性赋值
什么叫做为类类型的属性赋值呢?其实就是我们的实体类中的属性的数据类型是又一个实体类类型。
既然是为这种类型的属性赋值,那肯定不能再用之前的setter注入和构造器注入这种方法了,下面展示几种方法。
2.5.1引用外部bean
首先我们还是通过一个bean对其它的属性进行setter注入,但是我们的实体类类型的属性clazz,我们选择用ref标签属性
ref属性:引用IOC容器中的某个bean的id
<bean id="studentFive" class="zyb.spring.pojo.Student">
<property name="sid" value="1004"></property>
<property name="sname" value="赵六"></property>
<property name="age" value="26"></property>
<property name="gender" value="男"></property>
<property name="clazz" ref="clazzOne"></property>
</bean>
<bean id="clazzOne" class="zyb.spring.pojo.Clazz">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班"></property>
</bean>
可以看到一个bean中的ref属性可以对应另一个bean标签的id,这样形成联立,和之前的resultMap有点像
2.5.2使用内部bean
该内部bean只能在当前bean的内部使用,不能直接通过外部的IOC容器获取
<bean id="studentFive" class="zyb.spring.pojo.Student">
<property name="sid" value="1004"></property>
<property name="sname" value="赵六"></property>
<property name="age" value="26"></property>
<property name="gender" value="男"></property>
<!--<property name="clazz" ref="clazzOne"></property>-->
<property name="clazz">
<bean id="clazzInner" class="zyb.spring.pojo.Clazz">
<property name="cname" value="远大前程班"></property>
<property name="cid" value="2222"></property>
</bean>
</property>
</bean>
2.6实验六:为数组类型的属性赋值
例如我现在在Student类中新加入一个属性
private String[] hobby;
现在我想通过Spring依赖注入,为该数组类型的属性赋值方法如下
我们通过property标签的子标签array标签,然后再array标签中使用value标签或者ref标签进行赋值
如果是一个字面量类型的属性,直接使用value即可,如果数组中的元素是类类型的话,那么我们需要通过value引入外部bean的id
<bean id="studentFive" class="zyb.spring.pojo.Student">
<property name="sid" value="1004"></property>
<property name="sname" value="赵六"></property>
<property name="age" value="26"></property>
<property name="gender" value="男"></property>
<!--<property name="clazz" ref="clazzOne"></property>-->
<property name="clazz">
<bean id="clazzInner" class="zyb.spring.pojo.Clazz">
<property name="cname" value="远大前程班"></property>
<property name="cid" value="2222"></property>
</bean>
</property>
<property name="hobby">
<array>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</array>
</property>
</bean>
2.7实验七:为list集合类型的属性赋值
方式一:通过内部引用list标签进行赋值
我们通过list标签下的ref标签,获取其它bean的唯一标识id,为我们的list集合进行注入
<bean id="clazzOne" class="zyb.spring.pojo.Clazz">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班"></property>
<property name="students">
<list>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
<ref bean="studentFour"></ref>
</list>
</property>
</bean>
方式二:通过外部引用list标签赋值
使用外部list引用bean,一定要手动加上util约束
<bean id="clazzOne" class="zyb.spring.pojo.Clazz">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班"></property>
<property name="students" ref="studentList">
<!-- <list>-->
<!-- <ref bean="studentTwo"></ref>-->
<!-- <ref bean="studentThree"></ref>-->
<!-- <ref bean="studentFour"></ref>-->
<!-- </list>-->
</property>
</bean>
<util:list id="studentList">
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
<ref bean="studentFour"></ref>
</util:list>
2.8实验八:为map集合类型的属性赋值
方法一:通过内部引用entry标签赋值
因为我们声明的teacherMap是一个<String,Teacher>类型的,所以我们的key是字面量可以直接引用,而value是类类型的,所以需要进行ref的引用
<property name="teacherMap">
<map>
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</map>
</property>
于此同时,因为我们需要引入两个teacher的bean对象,所以我们还要声明两个teacherbean
<bean id="teacherOne" class="zyb.spring.pojo.Teacher">
<property name="tid" value="10086"></property>
<property name="tname" value="大宝"></property>
</bean>
<bean id="teacherTwo" class="zyb.spring.pojo.Teacher">
<property name="tid" value="10010"></property>
<property name="tname" value="小宝"></property>
</bean>
方式二:通过外部的map标签引入
这个方法和上面的为list集合类型的属性赋值的第二种方法相同,都是引入util约束,然后在外面引入map
<util:map id="teacherMap">
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</util:map>
这样的话,我们就可以在主bean中直接进行引入
<property name="teacherMap" ref="teacherMap"></property>
2.9实验九:引入外部属性文件
①加入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
②创建外部属性文件
例如我想通过DruidDataSource连接数据库,那么Druid类就是我们的外部属性文件
③引入属性文件
<!--引入jdbc.properties,之后就可以通过${key}的方式访问value-->
<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>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
⑤测试
@Test
public void testDataSource() throws SQLException {
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
DruidDataSource bean = ioc.getBean(DruidDataSource.class);
System.out.println(bean.getConnection());
}
2.10实验十:bean的作用域
概念
在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义如下表
取值 | 含义 | 创建对象的时机 |
---|---|---|
singleton(默认) | 在IOC容器中,这个bean的对象始终为单实例 | IOC容器初始化时 |
prototype(多例) | 这个bean在IOC容器中有多个实例 | 获取bean时 |
singleton也就是单例的意思,即创建ioc容器获取的该bean的多个对象都是同一个。
例:
<bean id="student" class="zyb.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="sname" value="张三"></property>
</bean>
@Test
public void testScope(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-scope.xml");
Student student1 = ioc.getBean(Student.class);
Student student2 = ioc.getBean(Student.class);
System.out.println(student1==student2);
}
此时我们输出打印,会发现打印框内的结果是true,也就是说student1和student2是同一个对象
2.11实验十一:bean的生命周期
具体的生命周期过程
1、bean对象创建(调用无参构造器)
2、给bean对象设置属性
3、bean对象初始化之前的操作(由bean的后置处理器负责)
4、bean对象初始化(需要在配置bean时指定初始化方法)
5、bean对象初始化之后的操作(由bean的后置处理器负责)
6、bean对象就绪可以使用
7、bean对象销毁(需要在配置bean时指定销毁方法)
8、IOC容器关闭
直观体现过程:
创建一个测试用的User类
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", 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:销毁");
}
}
spring配置文件
<bean id="user" class="zyb.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod">
<property name="id" value="1"></property>
<property name="username" value="admin"></property>
<property name="password" value="12346"></property>
<property name="age" value="23"></property>
</bean>
测试类
public void test(){
/*
* 1、实例化
* 2、依赖注入
* 3、初始化,需要通过bean的init-method属性指定初始化的方法
* 4、IOC容器关闭时销毁,需要通过bean的destroy-method属性指定销毁方法
*/
//ConfigurableApplicationContext是ApplicationContext的子接口,其中扩展了刷新和关闭容器的方法
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = ioc.getBean(User.class);
System.out.println(user);
ioc.close();
}
注:获取声明周期的前三个步骤在创建ioc容器的时候就已经进行了
也就是说,如果bean的作用域为单例的时候,生命周期的前三个步骤会在获取IOC容器时执行
如果bean的作用域为多例时,生命周期的前三个步骤会在获取bean时执行
2.12实验十二:FactoryBean
简介:FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同,配置一个FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是getObject()方法的返回值。通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来,只把最简洁使用界面展示给我们。
将来整合Myabati时,Spring就是通过使用FactoryBean机制帮助我们创建SqlSessionFactory对象的。
FactoryBean是一个接口,需要创建一个实现类实现该接口。
其中有三个方法:
getObject():通过一个对象交给IOC容器管理
getObjectType():设置所提供对象的类型
isSingleton():所提供的对象是否单例
当把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;
}
}
spring配置文件
<bean class="zyb.spring.factory.UserFactoryBean"></bean>
test类
@Test
public void testFactoryBean(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-factory.xml");
User bean = ioc.getBean(User.class);
System.out.println(bean);
}
2.13实验十三:基于xml的自动装配
自动装配:
根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口属性赋值
我们通过Controller、Service、Dao三层架构来模拟基于xml的自动装配
创建一个ControllerUser类
在这个类中我们提供了一个saveUser方法,调用userService层中的saveUser方法。
/**
* @author 一只鱼zzz
* @version 1.0
*/
public class UserController {
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void saveUser(){
userService.saveUser();
}
}
创建UserService接口和UserServiceImpl实现类
public interface UserService {
/**
* 保存用户信息
*/
void saveUser();
}
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void saveUser() {
userDao.saveUser();
}
}
创建UserDao接口和UserDaoImpl实现类
public interface UserDao {
/**
* 保存用户信息
*/
void saveUser();
}
public class UserDaoImpl implements UserDao {
@Override
public void saveUser() {
System.out.println("保存成功");
}
}
spring-autowire文件
<bean id="userController" class="zyb.spring.controller.UserController" autowire="byType">
<!-- <property name="userService" ref="userService"></property>-->
</bean>
<bean id="userService" class="zyb.spring.service.impl.UserServiceImpl" autowire="byType">
<!-- <property name="userDao" ref="userDao"></property>-->
</bean>
<bean id="userDao" class="zyb.spring.dao.impl.UserDaoImpl"></bean>
测试类
@Test
public void testAutoWire(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-autowire.xml");
UserController userController = ioc.getBean(UserController.class);
userController.saveUser();
}
小结:
-
自动装配:根据指定的策略,在IOC容器中匹配某个bean,自动为bean中的类类型的属性或接口类型的属性赋值
-
我们可以通过bean标签中的autowire属性设置自动装配的策略
-
自动装配策略:
-
①no,default:表示不装配,即bean中的属性不会自动匹配某个bean为属性赋值,此时属性使用默认值
-
②byType:根据要赋值的属性的类型,在IOC容器中匹配某个bean,为属性赋值
-
注意:1、如果通过类型没有找到任何一个类型匹配的bean,此时不装配,属性使用默认值
-
2、如果通过类型找到了多个类型匹配的bean,此时会抛出异常:NoUniqueBeanDefinitionException
-
总结:当使用了byType实现自动装配时,IOC容器中有且只有一个类型匹配的bean能够为属性赋值
-
③byName:将要赋值的属性 的属性名作为bean的id 在IOC容器中匹配某个bean,为属性赋值
-
总结:当类型匹配的bean有多个时,此时可以使用byName实现自动装配
3.基于注解管理bean
3.1实验一:标记与扫描
注解介绍:和xml配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。
本质:其实所有的操作都是Java代码来执行的,xml和注解只是告诉框架中的Java代码要如何执行。
举例:元旦联欢晚会要布置教室,蓝色、红色、黄色各个位置都贴上指定的道具
我们的班长尽职尽责,在墙上做好了所有的标记,只需要同学们来完成具体的工作。
而墙上的标记相当于我们在代码中使用的注解,后面同学们做的工作,相当于框架的具体操作.
扫描:Spring为了知道我们在哪些地方做了标记(注解),就需要通过扫描的方式,来进行检测,然后根据注解进行后续操作。
注解种类:
@Component:将类表示为普通组件
@Controller:将类表示为控制层组件
@Service:将类标识为业务层组件
@Repository:将类标识为持久层组件
通过查看源码我们知道,@Controller,@Service,@Repository这三个注解只是在@Component注解的基础上起了三个新的名字。
对于Spring使用IOC容器管理这些组件来说没有区别。所以这三个注解只是给我们开发人员看的,让我们能够便于分辨组件的作用。
创建组件:
创建控制层组件
@Controller
public class UserController {
}
创建Service接口,并创建接口的实现类->业务层组件UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
}
创建Dao接口,并创建接口的实现类->持久层组件UserDaoImpl
@Repository
public class UserDaoImpl implements UserDao {
}
扫描组件:
情况一:最基本的扫描方式
base-packaage:指定要扫描的bean所在的package,这里只要是zyb.spring包下的bean类都会被扫描到
<!--扫描组件-->
<context:component-scan base-package="zyb.spring"> </context:component-scan>
情况二:指定要排除的组件
<context:component-scan base-package="zyb.spring">
<!--排除指定类型的注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--排除指定类-->
<context:exclude-filter type="assignable" expression="zyb.spring.controller.UserController"/>
</context:component-scan>
情况三:指定仅仅扫描的组件
测试:
@Test
public void testAutowireByAnnotation(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = ac.getBean(UserController.class);
System.out.println(userController);
UserService userService = ac.getBean(UserService.class);
System.out.println(userService);
UserDao userDao = ac.getBean(UserDao.class);
System.out.println(userDao);
}
组件所对应的bean的id:
在我们使用xml方式管理bean的时候,每个bean都有一个唯一标识,以便于在其他地方引用。现在使用注解以后,每个组件仍然应该有一个唯一标识
默认情况:
类名首字母小写就是bean的id。例如:UserController类对应的bean的id就是userController。
自定义bean的id
可以通过标识组件的注解的value属性设置自定义的bean的id
例如Service(“ xxxx")
3.2实验二:基于注解的自动装配
参考基于xml的自动装配
我们在UserController中声明UserService对象
在UserServiceImpl中声明UserDao对象
@Autowired注解
在成员变量上直接标记@Autowired注解即可完成自动装配,不需要提供setXxxx()方法,以后我们在项目中的正式用法就是这样。
原理:1、默认通过byType的方式,在IOC容器中通过类型匹配某个bean为属性赋值
2、如果有多个类型匹配的bean,此时会自动转换为byName的方式来实现自动装配的效果,也就是把要赋值的属性的属性名作为bean的id匹配某个bean为属性值
@Controller
public class UserController {
@Autowired
private UserServiceImpl userService;
public void saveUser(){
userService.saveUser();
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void saveUser() {
userDao.saveUser();
}
}
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void saveUser() {
System.out.println("保存成功");
}
}
二、AOP面向切面编程
2.1场景模拟
2.1.1声明接口
声明计算器接口Calculator,包含加减乘除的抽象方法。
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);
}
2.1.2创建实现类
public class CalculatorPureImpl 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;
}
}
2.1.3创建带日志功能的实现类
public class CalculatorLogImpl implements Calculator {
@Override
public int add(int i, int j) {
System.out.println("[日志] add 方法开始了,参数是:" + i + "," + j);
int result = i + j;
System.out.println("方法内部 result = " + result);
System.out.println("[日志] add 方法结束了,结果是:" + result);
return result;
}
@Override
public int sub(int i, int j) {
System.out.println("[日志] sub 方法开始了,参数是:" + i + "," + j);
int result = i - j;
System.out.println("方法内部 result = " + result);
System.out.println("[日志] sub 方法结束了,结果是:" + result);
return result;
}
@Override
public int mul(int i, int j) {
System.out.println("[日志] mul 方法开始了,参数是:" + i + "," + j);
int result = i * j;
System.out.println("方法内部 result = " + result);
System.out.println("[日志] mul 方法结束了,结果是:" + result);
return result;
}
@Override
public int div(int i, int j) {
System.out.println("[日志] div 方法开始了,参数是:" + i + "," + j);
int result = i / j;
System.out.println("方法内部 result = " + result);
System.out.println("[日志] div 方法结束了,结果是:" + result);
return result;
}
}
2.1.4提出问题
现有代码缺陷
针对日志功能的实现类,我们发现如下缺陷:
- 对核心业务功能有干扰,导致我们在开发核心业务功能时分散了精力
- 附加功能分散在各个业务功能方法中,不利于统一维护
解决思路
解决这两个问题,核心就是:解耦。
我们需要把附加功能从业务功能代码中抽取出来
困难
要抽取的代码在方法内部,靠以前把子类中的重复代码抽取到父类的方式没法解决,所以需要引入新的技术。
2.2代理模式
- 介绍
提供一个代理类,我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用
。
让不属于目标方法核心逻辑的代码从目标方法中剥离出来——解耦
。
调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。
使用代理前:
使用代理后:
-
生活中的代理
- 广告商找明星拍广告需要经过经纪人
- 房产中介是买卖双方的代理
-
术语
- 代理:
将非核心逻辑剥离出来后,封装这些非核心逻辑的类、对象、方法
- 目标:
被代理“套用”了非核心逻辑代码的类、对象、方法
- 代理:
2.2.1静态代理
创建静态代理类:
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;
}
}
静态代理确实实现了解耦,但是由于代码都写死了,完全不具备任何的灵活性。
比如日志功能,将来其他地方也需要附加日志,还得再声明更多个静态代理类,那就产生了大量重复的代码,日志功能还是分散的,没有统一管理。
如果将日志功能集中到一个代理类中,将来有任何日志需求,都通过这一个代理类来实现
,就需要使用动态代理技术
2.2.2动态代理
生产代理对象的工厂类:
public class ProxyFactory {
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
public Object getProxy(){
/**
* newProxyInstance():创建一个代理实例
* 其中有三个参数:
* 1、classLoader:加载动态生成的代理类的类加载器
* 2、interfaces:目标对象实现的所有接口的class对象所组成的数组
* 3、invocationHandler:设置代理对象实现目标对象方法的过程,即代理类中如何重写接
口中的抽象方法
*/
ClassLoader classLoader = target.getClass().getClassLoader();
Class<?>[] interfaces = target.getClass().getInterfaces();
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
/**
* proxy:代理对象
* method:代理对象需要实现的方法,即其中需要重写的方法
* args:method所对应方法的参数
*/
Object result = null;
try {
System.out.println("[动态代理][日志] "+method.getName()+",参
数:"+ Arrays.toString(args));
result = method.invoke(target, args);
System.out.println("[动态代理][日志] "+method.getName()+",结
果:"+ result);
} catch (Exception e) {
e.printStackTrace();
System.out.println("[动态代理][日志] "+method.getName()+",异
常:"+e.getMessage());
} finally {
System.out.println("[动态代理][日志] "+method.getName()+",方法
执行完毕");
}
return result;
}
};
return Proxy.newProxyInstance(classLoader, interfaces,
invocationHandler);
}
}
- 测试
@Test
public void testDynamicProxy(){
ProxyFactory factory = new ProxyFactory(new CalculatorLogImpl());
Calculator proxy = (Calculator) factory.getProxy();
proxy.div(1,0);
//proxy.div(1,1);
}
2.3AOP概念及相关术语
2.3.1概述
AOP是一种设计思想,是软件设计领域中的面向切面编程,它是面向对象编程的一种补充和完善,它以通过预编译方式
和运行期动态代理
方法实现在不修改源代码的情况下给程序动态统一添加额外功能
的一种技术。
2.3.2相关术语
横切关注点
从每个方法中抽取出来的同一类非核心业务
。
在同一个项目中,我们可以使用多个横切关注点对相关方法进行多个不同方面的增强。
这个概念不是语法层面天然存在的,而是根据附加功能的逻辑上的需要:有十个附加功能,就有十个横切关注点。
通知
每一个横切关注点上要做的事情都需要写一个方法来实现,这样的方法就叫通知方法
。
-
前置通知:在被代理的目标方法
前
执行 -
返回通知:在被代理的目标方法
成功结束
后执行 -
异常通知:在被代理的目标方法
异常结束
后执行 -
后置通知:在被代理的目标方法
最终结束
后执行 -
环绕通知:使用try…catch…finally结构围绕
整个
被代理的目标方法,包括上面四种通知对应的所有位置
切面
封装通知方法的类
目标
被代理的目标对象
代理
向目标对象应用通知之后创建的代理对象
连接点
纯逻辑概念,不是语法定义的。
把方法排成一排,每一个横切位置看成x轴方向,把方法从上到下执行的顺序看成是y轴,x轴和y轴的交叉点就是连接点。
切入点
定位连接点的方式。
每个类的方法中都包含多个连接点,所以连接点是类中客观存在的事务。
如果把连接点看作数据库中的记录,那么切入点就是查询记录的SQL语句。
Spring的AOP技术可以通过切入点定位到特定的连接点。
2.3.3作用
- 简化代码:把方法中固定位置
重复的代码抽取出来
,让被抽取的方法更专注于自己的核心功能,提高内聚性。 - 代码增强:把特定的功能封装到切面类中,看哪里有需要,就往上套,被
套用
了切面逻辑的方法就被切面给增强了。
2.4基于注解的AOP
2.4.1技术说明
- 动态代理:JDK原生的实现方式,需要被代理的目标类必须实现接口。因为这个技术要求
代理对象和目标对象实现同样的接口
(兄弟两个拜把子模式) - cglib:通过
继承被代理的目标类
(认干爹模式)实现代理,所以不需要目标类实现接口
2.4.2准备工作
添加依赖
<!-- spring-aspects会帮我们传递过来aspectjweaver -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>
准备被代理的目标资源
- 接口
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);
}
- 实现类
@Component
public class CalculatorPureImpl 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;
}
}
2.4.3创建切面类并配置
// @Aspect表示这个类是一个切面类
@Aspect
// @Component注解保证这个切面类能够放入IOC容器
@Component
public class LogAspect {
@Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.*
(..))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",参
数:"+args);
}
@After("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->后置通知,方法名:"+methodName);
}
@AfterReturning(value = "execution(*
com.atguigu.aop.annotation.CalculatorImpl.*(..))", returning = "result")
public void afterReturningMethod(JoinPoint joinPoint, Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->返回通知,方法名:"+methodName+",结
果:"+result);
}
@AfterThrowing(value = "execution(*
com.atguigu.aop.annotation.CalculatorImpl.*(..))", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
}
@Around("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
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;
}
}
2.4.4各种通知
-
前置:使用@Before注解标识
-
返回:使用@AfterReturning注解标识
-
异常:使用@AfterThrowing注解标识
-
后置:使用@After注解标识
-
环绕:使用@Around注解标识
各种通知的执行顺序:
- Spring版本5.3.x以前:
- 前置
- 目标操作
- 后置
- 返回通知或异常通知
- Spring版本5.3.x以后:
- 前置
- 目标操作
- 返回通知或异常通知
- 后置
2.4.5切入点表达式语法
2.4.6重用切入点表达式
重用切入点表达式的意思就是,我们的execution切入点表达式语法太长了,没必要在每个方法上加,只需要声明一次,然后重复使用即可。
- 声明
@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void pointCut(){}
- 在同一个切面中使用
@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}
- 在不同切面中使用
@Before("com.atguigu.aop.CommonPointCut.pointCut()")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}
2.4.7获取通知的相关信息
- ①获取连接点信息
获取连接点信息可以在通知方法的参数位置设置JoinPoint类型的形参
@Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint){
//获取连接点的签名信息
String methodName = joinPoint.getSignature().getName();
//获取目标方法到的实参信息
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}
- ②获取目标方法的返回值
@AfterReturning中的属性returning,用来将通知方法的某个形参,接收目标方法的返回值
@AfterReturning(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*
(..))", returning = "result")
public void afterReturningMethod(JoinPoint joinPoint, Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
}
- ③获取目标方法的异常
@AfterThrowing中的属性throwing,用来将通知方法的某个形参,接收目标方法的异常
@AfterThrowing(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*
(..))", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
}