简介

  • Spring:春天—->给软件行业带来了春天!
  • 2002,首次推出了 Spring 框架的雄性:interface21 框架!
  • Spring 框架即以 interface21 框架为基础,经过重新设计,并不断丰富其内涵,于 2004 年 3 月 24 日发布了 1.0 正式版
  • Rod Johnson Spring Framework 创始人,著名作者,很难想象 Rod Johnson 的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学
  • Spring 理念:使现有的技术更加容易使用,本事是一个大杂烩,整合了现有的技术框架
  • SSH:Struct2 + Spring + Hibernate
  • SSM:SpringMVC + Spring + Mybatis

官网:https://spring.io/

Spring Framework 5.3.12 API:https://docs.spring.io/spring-framework/docs/current/javadoc-api/

Version 5.3.12:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

官方下载地址:https://repo.spring.io/ui/native/release/org/springframework/spring/

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbk</artifactId>
<version>5.3.12</version>
</dependency>

优点

  • Spring 是一个开源的免费的框架(容器)!
  • Spring 是一个轻量级的、非入侵式的框架!
  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架的整合的支持 r

总结:开源免费容器,轻量级非侵入式,控制反转,面向切面,支持事务,支持框架整合

Spring 就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!

组成

img

拓展

在 Spring 的官网有这个介绍:现代化的 Java 开发!也就是基于 Spring 的开发

img

  • Spring Boot
    • 一个快速开发的脚手架
    • 基于 SpringBoot 可以快速的开发单个微服务
    • 约定大于配置
  • Spring Cloud
    • SpringCloud 是基于 SpringBoot 实现的

因为现在大多数公司都在使用 SpringBoot 进行快速开发,学习 SpringBoot 的前提,需要完全掌握 Spring 及 SpringMVC!承上启下的作用!

弊端:发展了太久之后,违背了原来的理念,配置十分繁琐,人称:“配置地狱”

IOC 理论推导

传统的调用

  1. UserDao 接口

    1
    2
    3
    4
    package dao;
    public interface UserDao {
    void getUser();
    }
  2. UserDaoImpl 实现类

    1
    2
    3
    4
    5
    6
    package dao;
    public class UserDaoImpl implements UserDao{
    public void getUser() {
    System.out.println("默认获取用户数据");
    }
    }
  3. UserService 业务接口

    1
    2
    3
    4
    5
    package Service;
    public interface UserService {
    void getUser();
    }

  4. UserServiceImpl 业务实现类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package Service;
    import dao.UserDao;
    import dao.UserDaoImpl;

    public class UserServiceImpl implements UserService{
    UserDao userDao = new UserDaoImpl();
    public void getUser(){
    userDao.getUser();
    }
    }
  5. 测试

1
2
3
4
5
6
7
8
9
10
11
package holle0;
import Service.UserService;
import Service.UserServiceImpl;

public class MyTest0 {
public static void main(String[] args) {
// 用户实际调用的是业务层,dao层他们不需要接触
UserService userService = new UserServiceImpl();
userService.getUser();
}
}

在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!

image-20200801122742581

改良:我们使用一个 Set 接口实现。已经发生了革命性的变化!

1
2
3
4
5
6
7
//在Service层的实现类(UserServiceImpl)增加一个Set()方法
//利用set动态实现值的注入!
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}

set() 方法实际上是动态改变了 UserDao userDao 的 初始化部分(new UserDaoImpl()

测试中加上

1
((UserServiceImpl)userService).setUserDao(new UserDaoImpl());
  • 之前,程序是主动创建对象!控制权在程序猿手上!
  • 使用了 Set 注入后,程序不再具有主动性,而是变成了被动的接受对象!

本质上解决了问题,程序员不用再去管理对象的创建

系统的耦合性大大降低,可以更专注在业务的实现上

这是 IOC(控制反转)的原型,反转(理解):主动权交给了用户

image-20200801122805769

IOC 本质

控制反转 IOC(Inversion of Control),是一种设计思想,ID(依赖注入)是实现 IOC 的一种方法

控制反转是一种通过描述(XML 或注解)并通过第三方去生产获取特定对象的方式,在 Spring 中实现控制反转的是 IOC 容器,其实方法是依赖注入

image-20200801123518974

img

image-20200801123348207

image-20200801123450897

HelloSpring

在父模块中导入 jar 包

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>

pojo 的 Hello.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package pojo;

public class Hello {

private String str;

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

@Override
public String toString() {
return "Holle [str=" + str + "]";
}
}

在 resource 里面的 xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">


<!--在Spring中创建对象,在Spring这些都称为bean
类型 变量名 = new 类型();
Holle holle = new Holle();

bean = 对象(holle)
id = 变量名(holle)
class = new的对象(new Holle();)
property 相当于给对象中的属性设值,让str="Spring"
-->

<bean id="hello" class="pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>

测试类 MyTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package holle1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pojo.Hello;

public class MyTest {

public static void main(String[] args) {
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象下能在都在spring·中管理了,我们要使用,直接取出来就可以了
Hello holle = (Hello) context.getBean("hello");
System.out.println(holle.toString());
}

}

核心用 set 注入,所以必须要有下面的 se()方法

1
2
3
4
//Hello类
public void setStr(String str) {
this.str = str;
}

思考:

image-20200801165156259

IOC:对象由 Spring 来创建,管理,装配!

现实中的理解:
原来这套程序是:你写好菜单买好菜,客人来了自己把菜炒好招待,就相当于你请人吃饭
现在这套程序是:你告诉楼下餐厅,你要哪些菜,客人来的时候,餐厅把做好的你需要的菜送上来
IoC:炒菜这件事,不再由你自己来做,而是委托给了第三方__餐厅来做

此时的区别就是,如果我还需要做其他的菜,我不需要自己搞菜谱买材料再做好,而是告诉餐厅,我要什么菜,什么时候要,你做好送来

在前面第一个 module 试试引入 Spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDaomSql" class="dao.UserDaoMysqlImpl"></bean>

<bean id="userServiceImpl" class="service.UserServiceImp">
<!--ref引用spring中已经创建很好的对象-->
<!--value是一个具体的值,基本数据类型-->
<property name="userDao" ref="userDaomSql"/>
</bean>

</beans>

第一个 module 改良后测试

1
2
3
4
5
6
7
8
9
10
11
12
13
package holle0;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserServiceImpl;

public class MyTest0 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
userServiceImpl.getUser();
}
}

总结:

所有的类都要装配的 beans.xml 里面;

所有的 bean 都要通过容器去取;

容器里面取得的 bean,拿出来就是一个对象,用对象调用方法即可;

控制:传统应用程序的对象是有程序本身控制创建的,使用 Spring 后,对象是由 Spring 来创建的

反转:程序本身不创建对象,而变成被动的接受对手

依赖注入:就是利 Set 方法来实现注入的

IOC 是一中编程思想,由主动的编程变成被动的接受

IOC 创建对象的方式

  1. 下标赋值

    index 指的是有参构造中参数的下标,下标从 0 开始;

1
2
3
4
5
6
7
8
9
10
11
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="user" class="pojo.User">
<constructor-arg index="0" value="chen"/>
</bean>
</beans>

  1. 类型赋值 (不建议使用)
1
2
3
4
 <!--    有参构造 2.通过类型创建,不建议使用 -->
<bean id="user" class="com.hgm.pojo.User">
<constructor-arg type="java.lang.String"value="黄帅哥"/>
</bean>
  1. 直接通过参数名(掌握)
1
2
3
4
5
 <!--   3.直接通过参数名来设置-->
<bean id="user" class="com.hgm.pojo.User">
<constructor-arg name="name" value="黄帅哥"/>
</bean>
<!-- 比如参数名是name,则有name="具体值" -->

注册 bean 之后就对象的初始化了(类似 new 类名()

name 方式还需要无参构造和 set 方法,index 和 type 只需要有参构造

就算是 new 两个对象,也是只有一个实例(单例模式:全局唯一

1
2
3
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
system.out.println(user == user2)//结果为true

总结:在配置文件加载的过程中,容器(< bean>)中管理的对象就已经初始化了

Spring 配置

别名

1
2
3
4
5
6
7
8
9
<bean id="user" class="pojo.User">
<constructor-arg name="name" value="chen"></constructor-arg>
</bean>

<alias name="user" alias="userLove"/>
<!-- 使用时
User user2 = (User) context.getBean("userLove");
-->

Bean 配置

1
2
3
4
5
6
7
8
9
10
<!--id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的会限定名:包名+类型
name:也是别名,而且name可以同时取多个别名 -->
<bean id="user" class="pojo.User" name="u1 u2,u3;u4">
<property name="name" value="chen"/>
</bean>
<!-- 使用时
User user2 = (User) context.getBean("u1");
-->

import

这个 import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个

假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的 bean 中,我们可以利
用 import 将所有人的 beans.xml 合并为一个总的!

  • 张三(beans.xm1)
  • 李四(beans2.xm1)
  • 王五(beans3.xm1)
  • applicationContext.xml
1
2
3
<import resource="beans.xm1"/>
<import resource="beans2.xml"/>
<import resource="beans3.xm1"/>

使用的时候,直接使用总的配置就可以了

弹幕评论:

按照在总的 xml 中的导入顺序来进行创建,后导入的会重写先导入的,最终实例化的对象会是后导入 xml 中的那个

依赖注入

构造器注入

第四点有提到

set 方式注入【重点】

依赖注入:set 注入!

  • 依赖:bean 对象的创建依赖于容器
  • 注入:bean 对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型

    Address 类

  2. 真实测试对象

    Student 类

  3. beans.xml

  4. 测试

    MyTest3

Student 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package pojo;

import java.util.*;
@Get
@Set
public class Student {
//别忘了写get和set方法(用lombok注解也行)
private String name;
private Address address;

private String[] books;
private List<String> hobbies;

private Map<String, String> card;
private Set<String> game;

private Properties infor;
private String wife;

@Override
public String toString() {
return "Student{" +"\n"+
"name='" + name + '\'' +"\n"+
", address=" + address.toString() +"\n"+
", books=" + Arrays.toString(books) +"\n"+
", hobbies=" + hobbies +"\n"+
", card=" + card +"\n"+
", game=" + game +"\n"+
", infor=" + infor +"\n"+
", wife='" + wife + '\'' +"\n"+
'}';
}
}

Address 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package pojo;

public class Address {

private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

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

beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="address" class="pojo.Address">
<property name="address" value="address你好" />
</bean>

<bean id="student" class="pojo.Student">
<!--第一种,普通值注入 -->
<property name="name" value="name你好" />
<!--第二种,ref注入 -->
<property name="address" ref="address" />

<!--数组注入 -->
<property name="books">
<array>
<value>三国</value>
<value>西游</value>
<value>水浒</value>
</array>
</property>

<!--list列表注入 -->
<property name="hobbies">
<list>
<value></value>
<value></value>
<value>rap</value>
<value>篮球</value>
</list>
</property>

<!--map键值对注入 -->
<property name="card">
<map>
<entry key="username" value="root" />
<entry key="password" value="root" />
</map>
</property>

<!--set(可去重)注入 -->
<property name="game">
<set>
<value>wangzhe</value>
<value>lol</value>
<value>galname</value>
</set>
</property>

<!--空指针null注入 -->
<property name="wife">
<null></null>
</property>

<!--properties常量注入 -->
<property name="infor">
<props>
<prop key="id">20200802</prop>
<prop key="name">cbh</prop>
</props>
</property>
</bean>
</beans>

MyTest3

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;

public class MyTest3 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student stu = (Student) context.getBean("student");
System.out.println(stu.toString());
}
}

拓展注入

image-20200802142717216

pojo 增加 User 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package pojo;

public class User {
private String name;
private int id;
public User() {

}
public User(String name, int id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User [name=" + name + ", id=" + id + "]";
}
}

注意: beans 里面加上这下面两行

使用 p 和 c 命名空间需要导入 xml 约束

xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--p命名空间注入/set注入,可以直接注入属性的值-》property-->
<bean id="user" class="pojo.User" p:name="cxk" p:id="20" >
</bean>

<!--c命名空间,通过构造器注入,需要写入有参和无参构造方法-》construct-args-->
<bean id="user2" class="pojo.User" c:name="cbh" c:id="22"></bean>
</beans>

测试

1
2
3
4
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user",User.class);//确定class对象,就不用再强转了
System.out.println(user.toString());

Bean 作用域

image-20200802143401165

image-20200802143342586

  1. 单例模式(默认)
1
<bean id="user2" class="pojo.User" c:name="cxk" c:age="19" scope="singleton"></bean>

弹幕评论:单例模式是把对象放在 pool 中,需要再取出来,使用的都是同一个对象实例

  1. 原型模式: 每次从容器中 get 的时候,都产生一个新对象!
1
<bean id="user2" class="pojo.User" c:name="cxk" c:age="19" scope="prototype"></bean>
  1. 其余的 request、session、application 这些只能在 web 开放中使用!

Bean 的自动装配

  • 自动装配是 Spring 满足 bean 依赖的一种方式
  • Spring 会在上下文自动寻找,并自动给 bean 装配属性

在 Spring 中有三种装配的方式

  1. 在 xml 中显示配置

  2. 在 java 中显示配置

  3. 隐式的自动装配 bean 【重要】

  4. 环境搭建:一个人有两个宠物

  5. byType 自动装配:byType 会自动查找,和自己对象 set 方法参数的类型相同的 bean

    保证所有的 class 唯一(类为全局唯一)

  6. byName 自动装配:byName 会自动查找,和自己对象 set 对应的值对应的 id

    保证所有 id 唯一,并且和 set 注入的值一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<context:annotation-config/>
<!-- 找不到id和多个相同class -->
<bean id="cat" class="com.hgm.pojo.Cat"/>
<bean id="dog" class="com.hgm.pojo.Dog"/>
<!--
Bean自动装配
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id,
需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
byType:会自动在容器上下文中查找,和自己对象属性相同的bean,
需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型值一致
-->
<bean id="people" class="com.hgm.pojo.People">
<property name="name" value="黄帅哥"/>
</bean>

测试:自动装配

pojo 的 Cat 类

1
2
3
4
5
public class Cat {
public void shut(){
System.out.println("miao");
}
}

pojo 的 Dog 类

1
2
3
4
5
6
7
public class Dog {

public void shut(){
System.out.println("wow");
}

}

pojo 的 People 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package pojo;
public class People {

private Cat cat;
private Dog dog;
private String name;

public Cat getCat() {
return cat;
}

public void setCat(Cat cat) {
this.cat = cat;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}

xml 配置 -> byType 自动装配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="cat" class="pojo.Cat"/>
<bean id="dog" class="pojo.Dog"/>

<!--byType会在容器自动查找,和自己对象属性相同的bean
例如,Dog dog; 那么就会查找pojo的Dog类,再进行自动装配
-->
<bean id="people" class="pojo.People" autowire="byType">
<property name="name" value="cbh"></property>
</bean>

</beans>

xml 配置 -> byName 自动装配

1
2
3
4
5
6
7
8
9
10
<bean id="cat" class="pojo.Cat"/>
<bean id="dog" class="pojo.Dog"/>
<!--byname会在容器自动查找,和自己对象set方法的set后面的值对应的id
例如:setDog(),取set后面的字符作为id,则要id = dog 才可以进行自动装配

-->
<bean id="people" class="pojo.People" autowire="byName">
<property name="name" value="cbh"></property>
</bean>

弹幕评论:byName 只能取到小写,大写取不到

​ 小结:

  • byname 的时候,需要保证所有 bean 的 id 唯一,并且这个 bean 需要和自动注入的属性的 set 方法的值一致
  • ByType 的时候,需要保证所有 bean 的 class 唯一,并且这个 bean 需要和自动注入的属性的类型值一致

使用注解实现自动装配

jdk1.5 支持的注解,Spring2.5 就支持注解了!

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.(翻译:基于注释的配置的引入提出了一个问题,即这种方法是否比 XML“更好”)

  1. 导入 context 约束
1
1. xmlns:context="http://www.springframework.org/schema/context"
  1. 配置注解的支持:< context:annotation-config/>
1
2
3
4
5
6
7
8
9
10
11
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>
</beans>

@Auotwired

默认是 byType 方式,如果匹配不上,就会 byName

直接在属性上使用即可! 也可以在 set 方式上使用

使用 Auotwired 我们可以不用编写 Set 方法了,前提是你这个自动导入装配的属性在 IOC (Spring) 容器中存在,且符合名字 Byname

科普: @Nullable 字段标记了这个注解,说明这个字段可以为 null

1
2
3
4
5
6
7
8
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}

@Nullable 字段标记了这个注解,说明该字段可以为空

public name(@Nullable String name){

}

1
2
3
4
5
//源码
public @interface Autowired {
boolean required() default true;
}

如果定义了 Autowire 的 require 属性为 false,说明这个对象可以为 null,否则不允许为空(false 表示找不到装配,不抛出异常)

@Autowired+@Qualifier

@Autowired 不能唯一装配时,需要@Autowired+@Qualifier

如果@Autowired 自动装配的环境比较复杂,自动装配无法通过一个注解[@Autowired]完成的时候,我们可以使用@Qualifier(value = “xx”)去配置@Autowired 的使用,指定一个唯一的 bean 对象注入!

1
2
3
4
5
6
7
8
9
public class People {
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
}

弹幕评论:

如果 xml 文件中同一个对象被多个 bean 使用,Autowired 无法按类型找到,可以用@Qualifier 指定 id 查找

@Resource

默认是 byName 方式,如果匹配不上,就会 byType

1
2
3
4
5
6
7
8
public class People {
Resource(name="cat")
private Cat cat;
Resource(name="dog")
private Dog dog;
private String name;
}

弹幕评论:

Autowired 是 byType,@Autowired+@Qualifier = byType || byName

Autowired 是先 byteType,如果唯一則注入,否则 byName 查找。resource 是先 byname,不符合再继续 byType

@Resource 和@Auotwired 的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Auotwired 是通过 Byname 的方式实现,而且必须要求这个对象存在[常用]
  • @Resource 默认是通过 byname 的方式实现,如果找不到名字,则通过 byType 实现,如果两个都找不到的情况下就报错!
  • 执行顺序不同:@Auotwired 通过 byType 的方式实现.@Resource 默认是通过 byname 的方式实现,

使用注解开发

在 spring4 之后,要使用注解开发,必须保证 aop 的包导入了

image-20200802201924490

使用注解需要导入 context 约束,增加注解的支持

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>

</beans>

bean

弹幕评论:
有了< context:component-scan>,另一个< context:annotation-config/>标签可以移除掉,因为已经被包含进去了。

所以:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">


<!--指定扫描包下的注解-->
<context:component-scan base-package="com.ms"/>
<!-- &lt;!&ndash;开启注解支持&ndash;&gt;--> 这里就可以注释掉了
</beans>

属性如何注入 @Value

1
2
3
4
5
6
7
8
9
//等价于    <bean id="user" class="com.hgm.pojo.User"/>      Component:组件
@Component //注册bean用Component代替了
public class User {
public String name; //相当于 <property name="name" value="SerMs"/>
@Value("SerMs")
public void setName(String name) {
this.name = name;
}
}

衍生的注解

在类里面使用了注解就说明这个类被 Spring 托管了,也就是成为 Spring 的组件了

@Component 有几个衍生注解,会按照 web 开发中,mvc 架构中分层。

  1. @Component 有几个衍生注解,我们在 web 开发中,会按照 MVC 三层架构分层!
  2. dao 层【@Repository】 (dao 层我们都会用 Repository 来注解)
  3. service 层【@Service】(service 层我们都会用 Serivce 来注解)
  4. Controller 层【@Controller】

这四个功能都是一样的,都是代表将某个类注册到 Spring 中,装配 Bean

自动装配

  • @Auotwierd:自动装配通过类型、名字

  • 如果 Auotwierd 不能唯一自动装配上属性,则需要通过@Qualifier(value=”xxx”)

  • @Nullable:字段标记了这个注解,说明这个字段可以为 null
  • @Resource:自动装配通过名字,类型

作用域@scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//原型模式prototype,单例模式singleton
//scope("prototype")相当于<bean scope="prototype"></bean>
@Component
@scope("prototype")
public class User {

//相当于<property name="name" value="kuangshen"/>
@value("kuangshen")
public String name;

//也可以放在set方法上面
@value("kuangshen")
public void setName(String name) {
this.name = name;
}
}

小结

xml 与注解:

  • xml 更加万能,使用宇任何场合,维护更加方便
  • 注解 不是自己类使用不了,维护相对复杂!

xml 与注解最佳实践:

  • xml 用来管理 bean

  • 注解只负责属性的注入

  • 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持

  • ```xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

    ### 注解说明

    - @Auotwierd:自动装配通过类型、名字

    - 如果 Auotwierd 不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")

    - @Nullable:字段标记了这个注解,说明这个字段可以为 null

    - @Resource:自动装配通过名字,类型

    - @Component:组件,放在类上,说明这个类被 Spring 管理了,就是 bean

    ---

    ## 使用 Java 的方式配置 Spring

    我们现在要完全不使用 Spring 的 xml 配置了,全权交给 Java 来做~

    JavaConfig 是 Spring 的一个子项目,在 Spring4 之后他成为了一个核心功能

    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9naXRlZS5jb20vd29fYmVsbC9QaWN0dXJlQmVkL3Jhdy9tYXN0ZXIvaW1hZ2UvaW1hZ2UtMjAyMDA4MDIyMTU3NTI4NjgucG5n?x-oss-process=image/format,png)

    **实体类:pojo 的 User.java**

    ```java
    //这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
    @component
    public class User {
    private String name;

    public String getName() {
    return name;
    }
    //属性注入值
    @value("QINJIANG')
    public void setName(String name) {
    this.name = name;
    }
    @Override
    public String toString() {
    return "user{" +
    "name='" + name + '\''+
    '}';
    }
    }

弹幕评论:要么使用@Bean,要么使用@Component 和 ComponentScan,两种效果一样

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.kuang.config;

import com.kuang.pojo.User;
import org.springframework.context.annotation.*;

// @Configuration本质上也是一个Controller,也会被spring托管,注册到容器中
// @Configuration表示这是一个配置类,和之前的beans.xml的功能一样的

@Configuration
@ComponentScan("com.kuang.pojo") // @ComponentScan表示组件的扫描
@Import(MyConfig1.class) // 通过import导入其他的配置类
public class MyConfig {
// @Bean 表示注册一个bean
// 方法的名字即是bean中的id
// 方法的返回值就是bean中的class
@Bean
public User getUser(){
return new User(); // return就是返回要注入到bean中的对象
}

}

弹幕评论:ComponentScan、@Component(“pojo”) 这两个注解配合使用

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.kuang.config.MyConfig;
import com.kuang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
@Test
public void test(){
// 如果使用了java配置类的方式来注册bean,
// 只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
// 获取通过配置类中的方法getUser方法来获得的User对象
User user = (User) context.getBean("getUser");
System.out.println(user.toString());
}
}

这种纯 Java 的配置方式,在 SpringBoot 中随处可见

1
@Component

这里这个注解的意思就是说明这个类被 Spring 接管了,注册到了容器中

@Configuration 本质上还是一个@Component,代表这个一个配置类和 bean.xml 一样

@ComponentScan(“com.kuang.pojo”) // @ComponentScan 表示组件的扫描 @Import(MyConfig1.class) // 通过 import 导入其他的配置类

会创建两个相同对象问题的说明:

弹幕总结 - -> @Bean 是相当于< bean>标签创建的对象,而我们之前学的@Component 是通过 spring 自动创建的这个被注解声明的对象,所以这里相当于有两个 User 对象被创建了。一个是 bean 标签创建的(@Bean),一个是通过扫描然后使用@Component,spring 自动创建的 User 对象,所以这里去掉@Bean 这些东西,然后开启扫描。之后在 User 头上用@Component 即可达到 spring 自动创建 User 对象了


代理模式(AOP)

为什么要学代理模式?

因为这个就是 SpringAOP 的底层!【SpringAOP 和 SpringMVC】

代理模式的分类:

  • 静态代理
  • 动态代理

image-20200803101427846

静态代理

角色分析

  • 抽象角色:一般会使用接口或者抽象类来解决
  • 真实角色:被代理角色
  • 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作
  • 客户:使用代理角色来进行一些操作 .

代码步骤:

  1. rent 接口

    1
    2
    3
    4
    5
    package com.kuang.Daili;

    public interface Rent {
    public void rent();
    }
  2. 真实角色(Client 租户)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package com.kuang.Daili;

    public class Client {
    public static void main(String[] args) {
    Host host=new Host();
    //host.rent();
    Proxy proxy=new Proxy(host);

    proxy.rent();
    }
    }
  3. proxy 代理角色

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package com.kuang.Daili;

    public class Proxy {
    private Host host;
    public Proxy(){

    }
    public Proxy(Host host){
    this.host=host;
    }
    public void rent(){
    seeHouse();

    host.rent();
    hetong();
    fare();
    }
    public void seeHouse(){
    System.out.println("中介带你看房");
    }
    public void fare(){
    System.out.println("收中介费!");
    }
    public void hetong(){
    System.out.println("签租领合同");
    }

    }
  4. 客户端访问代理角色

    1
    2
    3
    4
    5
    6
    7
    public class Client {
    public static void main(String[] args) { //房东要租房子
    Host host = new Host(); //代理,中介帮房东租房子,但是代理角色会加一些附属操作
    Proxy proxy = new Proxy(host); //你不用面对房东,直接找中介租房即可
    proxy.rent();
    }
    }

分析:在这个过程中,你直接接触的就是中介,就如同现实生活中的样子,你看不到房东,但是你依旧租到了房东的房子通过代理,这就是所谓的代理模式,程序源自于生活,所以学编程的人,一般能够更加抽象的看待生活中发生的事情。

代理模式的好处:
  • 可以使真实角色的操作更加纯粹,不用去关注一些公共业务
  • 公共业务就交给了代理角色,实现了业务的分工
  • 公共业务发生扩展的时候,方便集中管理
缺点:
  • 一个真实角色就会产生一个代理角色,代码量会翻倍,开发效率会变低
  • 类多了 , 多了代理类 , 工作量变大了 . 开发效率降低 .

静态代理的再理解

  1. 创建一个抽象角色,比如咋们平时做的用户业务,抽象起来就是增删改查!

    1
    2
    3
    4
    5
    6
    7
    //抽象角色:增删改查业务
    public interface UserService {
    void add();
    void delete();
    void update();
    void query();
    }
  2. 我们需要一个真实对象来完成这些增删改查操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //真实对象,完成增删改查操作的人
    public class UserServiceImpl implements UserService {

    public void add() {
    System.out.println("增加了一个用户");
    }

    public void delete() {
    System.out.println("删除了一个用户");
    }

    public void update() {
    System.out.println("更新了一个用户");
    }

    public void query() {
    System.out.println("查询了一个用户");
    }
    }
  3. 需求来了,现在我们需要增加一个日志功能,怎么实现!

    • 思路 1 :在实现类上增加代码 【麻烦!】

    • 思路 2:使用代理来做,能够不改变原来的业务情况下,实现此功能就是最好的了!

  4. 设置一个代理类来处理日志!代理角色

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    //代理角色,在这里面增加日志的实现
    public class UserServiceProxy implements UserService {
    private UserServiceImpl userService;

    public void setUserService(UserServiceImpl userService) {
    this.userService = userService;
    }

    public void add() {
    log("add");
    userService.add();
    }

    public void delete() {
    log("delete");
    userService.delete();
    }

    public void update() {
    log("update");
    userService.update();
    }

    public void query() {
    log("query");
    userService.query();
    }

    public void log(String msg){
    System.out.println("执行了"+msg+"方法");
    }

    }

    OK,到了现在代理模式大家应该都没有什么问题了,重点大家需要理解其中的思想;我们在不改变原来的代码的情况下,实现了对原有功能的增强,这是 AOP 中最核心的思想

AOP 横向开发

image-20200803111539621

我们想要静态代理的好处,又不想要静态代理的缺点,所以 , 就有了动态代理

动态代理

  • 动态代理和静态代理角色一样
  • 动态代理的代理类是动态生成的,不是我们直接写好的
  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
    • 基于接口—-JDK 动态代理
    • 基于类:cglib
    • java 字节码实现 : Javassist

需要了解两个类:Proxy,InvocationHandler

【InvocationHandler:调用处理程序】

img

1
2
3
4
5
Object invoke(Object proxy, 方法 method, Object[] args)
//参数
//proxy - 调用该方法的代理实例
//method -所述方法对应于调用代理实例上的接口方法的实例。方法对象的声明类将是该方法声明的接口,它可以是代理类继承该方法的代理接口的超级接口。
//args -包含的方法调用传递代理实例的参数值的对象的阵列,或null如果接口方法没有参数。原始类型的参数包含在适当的原始包装器类的实例中,例如java.lang.Integer或java.lang.Boolean 。.

实例:

接口 Host.java

1
2
3
4
5
6
//接口
package pojo2;
public interface Host {
public void rent();

}

接口 Host 实现类 HostMaster.java

1
2
3
4
5
6
7
//接口实现类
package pojo2;
public class HostMaster implements Host{
public void rent() {
System.out.println("房东要租房子");
}
}

代理角色的处理程序类 ProxyInvocationHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package pojo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

///用这个类,自动生成代理
public class ProxyInvocationHandler implements InvocationHandler {

// Foo f =(Foo) Proxy.NewProxyInstance(Foo. Class.GetClassLoader(),
// new Class<?>[] { Foo.Class },
// handler);

// 被代理的接口
public HostMaster hostMaster ;

public void setHostMaster(HostMaster hostMaster) {
this.hostMaster = hostMaster;
}

// 得到生成的代理类
public Object getProxy() {
// newProxyInstance() -> 生成代理对象,就不用再写具体的代理类了
// this.getClass().getClassLoader() -> 找到加载类的位置
// hostMaster.getClass().getInterfaces() -> 代理的具体接口
// this -> 代表了接口InvocationHandler的实现类ProxyInvocationHandler
return Proxy.newProxyInstance(this.getClass().getClassLoader(), hostMaster.getClass().getInterfaces(), this);


// 处理代理实例并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
seeHouse();
// 动态代理的本质,就是使用反射机制实现的
// invoke()执行它真正要执行的方法
Object result = method.invoke(hostMaster, args);
fee();
return result;
}

public void seeHouse() {
System.out.println("看房子");
}

public void fee() {
System.out.println("收中介费");
}

}

用户类 My2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package holle4_proxy;

import pojo2.Host;
import pojo2.Host2;
import pojo2.HostMaster;
import pojo2.ProxyInvocationHandler;

public class My2 {

public static void main(String[] args) {

//真实角色
HostMaster hostMaster = new HostMaster();

//代理角色,现在没有;用代理角色的处理程序来实现Host接口的调用
ProxyInvocationHandler pih = new ProxyInvocationHandler();

//pih -> HostMaster接口类 -> Host接口
pih.setHostMaster(hostMaster);

//获取newProxyInstance动态生成代理类
Host proxy = (Host) pih.getProxy();

proxy.rent();

}
}

弹幕评论:
什么时候调用 invoke 方法的?
代理实例调用方法时 invoke 方法就会被调用,可以 debug 试试

改为万能代理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
///用这个类,自动生代理
public class ProxyInvocationHandler implements InvocationHandler {

// Foo f =(Foo) Proxy.NewProxyInstance(Foo. Class.GetClassLoader(),
// new Class<?>[] { Foo.Class },
// handler);

// 被代理的接口
public Object target;

public void setTarget(Object target) {
this.target = target;
}

// 得到生成的代理类 -> 固定的代码
public Object getProxy() {
// newProxyInstance() -> 生成代理对象,就不用再写具体的代理类了
// this.getClass().getClassLoader() -> 找到加载类的位置
// hostMaster.getClass().getInterfaces() -> 代理的具体接口
// this -> 代表了接口InvocationHandler的实现类ProxyInvocationHandler
return Proxy.newProxyInstance(this.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}

// 处理代理实例并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//前置通知
pre();

//打印日志,通过反射的方式
log(method.getName());

// 动态代理的本质,就是使用反射机制实现的
// invoke()执行它真正要执行的方法
Object result = method.invoke(target, args);

//后置通知
post();

return result;
}

public void pre() {
System.out.println("[前置通知]");
}

public void post() {
System.out.println("[后置通知]");
}

//日志打印
public void log(String msg) {
System.out.println("[Debug]执行了" + msg + "方法");
}
}

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public class Client {
public static void main(String[] args) {
//真实角色
UserServiceImpl userService = new UserServiceImpl();

//代理角色,不存在
ProxyInvocationHandler proxyInvocationHandler = new ProxyInvocationHandler();

//设置要代理的对象
proxyInvocationHandler.setTarget(userService);

//动态生产代理类
UserService proxy = (UserService) proxyInvocationHandler.getProxy();

//执行业务操作
proxy.add();
}
}

动态代理的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
  • 公共也就交给代理角色!实现了业务的分工
  • 公共业务发生扩展的时候,方便集中管理
  • 一个动态代理类代理的是一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可

AOP


什么是 AOP

AOP 意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术,AOP 是 OOP 的延续,是软件开发中的一个热点,也是 String 框架中的一个重要内容,是函数式编程的一种衍生范型,利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。

dIUZkV.png

术语:

  1. Aspect:切面,表示增强的功能,就是一推代码,完成某一个功能,非业务功能,常见的切面功能有日志,事务,统计信息,参数检查,权限验证
  2. JoinPoint:链接点,连接业务方法和切面的位置,就某类中的业务的方法
  3. 目标对象:给哪个类的方法增加功能,这个类就是目标对象

切面有关的三要素:

  1. 切面的功能代码,切面干什么
  2. 切面的执行位置,使用 Pointcut 表示切面执行的位置
  3. 切面的执行时间,使用 Advice 表示时间,在目标方法之前,还是目标方法之后

AOP 在 Spring 中的使用

提供声明式事务,允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…
  • 切面(Aspect):横切关注点 被模块化的特殊对象。即,它是一个类。(Log 类)
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。(Log 类中的方法)
  • 目标(Target):被通知对象。(生成的代理类)
  • 代理(Proxy):向目标对象应用通知之后创建的对象。(生成的代理类)
  • 切入点(PointCut):切面通知执行的”地点”的定义。(最后两点:在哪个地方执行,比如:method.invoke())
  • 连接点(JointPoint):与切入点匹配的执行点。

image-20200803154043909

SpringAOP 中,通过 Advice 定义横切逻辑,Spring 中支持 5 种类型的 Advice:

image-20200803135937435

即 AOP 在不改变原有代码的情况下,去增加新的功能。(代理)

使用 Spring 实现 AOP【重点】

  • 使用 AOP 织入,需要导入一个依赖包!
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>

方法一:使用原生 spring 接口

springAPI 接口实现

需要先导入 aop 约束

1
2
3
4
5
6
7
8
9
10
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

创建 applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--注册bean-->
<bean id="userservice" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>

<!--方式一,使用原生Spring API接口-->

<!--配置aop,还需要导入aop约束-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!--UserServiceImpl.*(..) -》 UserServiceImpl类下的所以方法(参数)-->
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<!-- 环绕,在id="pointcut"的前后切入 -->
</aop:config>
</beans>

execution(返回类型,类名,方法名(参数))->execution( com.service.,*(…))

UserService.java

1
2
3
4
5
6
7
package service;
public interface UserService {
public void add() ;
public void delete() ;
public void query() ;
public void update();
}

UserService 的实现类 UserServiceImp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package service;

public class UserServiceImpl implements UserService {

public void add() {
System.out.println("add增");
}
public void delete() {
System.out.println("delete删");
}
public void update() {
System.out.println("update改");
}
public void query() {
System.out.println("query查");
}
}

前置 Log.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}

后置 AfterLog.java

1
2
3
4
5
6
7
8
9
10
11
package log;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class AfterLog implements AfterReturningAdvice {
//returnVaule: 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回值是"+returnValue);
}
}

测试类 MyTest5

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

public class MyTest5 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意:动态代理代理的是接口
UserService userService = (UserService) context.getBean("userservice");
userService.add();
}
}

方法二:自定义类实现 AOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--注册bean-->
<bean id="userservice" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>
<!-- 方式二,自定义 -->
<bean id="diy" class="diy.DiyPointcut"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>

</beans>

1
2
3
4
5
6
7
8
9
10
11
12
package diy;
public class DiyPointcut {

public void before(){
System.out.println("插入到前面");
}

public void after(){
System.out.println("插入到后面");
}
}

1
2
3
4
5
6
7
8
9
//测试
public class MyTest5 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意:动态代理代理的是接口
UserService userService = (UserService) context.getBean("userservice");
userService.add();
}
}

方法三:使用注解实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.kuang.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointcut {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法执行前---------");
}

@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法执行后---------");
}

@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
//执行目标方法proceed
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}

第二步:在 Spring 配置文件中,注册 bean,并增加支持注解的配置

1
2
3
<!--第三种方式:注解实现-->
<bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>

测试

1
2
3
4
5
6
7
8
public class MyTest5 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意:动态代理代理的是接口
UserService userService = (UserService) context.getBean("userservice");
userService.add();
}
}

输出结果:
image-20200803175642064

aop:aspectj-autoproxy:说明

通过 aop 命名空间的声明自动为 spring 容器中那些配置@aspectJ 切面的 bean 创建代理,织入切面。当然,spring 在内部依旧采用 AnnotationAwareAspectJAutoProxyCreator 进行自动代理的创建工作,但具体实现的细节已经被隐藏起来了

有一个 proxy-target-class 属性,默认为 false,表示使用 jdk 动态代理织入增强,当配为时,表示使用 CGLib 动态代理技术织入增强。不过即使 proxy-target-class 设置为 false,如果目标类没有声明接口,则 spring 将自动使用 CGLib 动态代理。

aop 的技术实现框架:

  1. spring:Spring 在内部实现了 aop 的规范,能做了 aop 的工作
    1. 我们项目开发中很少使用 Spring 的 aop 实现,因为 spring 的 aop 比较笨重
  2. AspectJ:一个开源的专门处理 Aop 的框架,Spring 框架中集成了 aspectj 框架,通过 spring 就能使用 aspectj 的功能
    1. 使用 xml 的配置文件:配置全局事务
    2. 使用注解,我们在项目中要做 aop 功能,一般都用注解,aspectj 有五个注解