1、Spring

1.2、优点

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

总结一句话:Spring 是一个轻量级的控制反转和面向切面编程的框架

1.3、组成

1.4、拓展

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

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

2、IOC 理论推导

  1. UserDao 接口
  2. UserDaoImpl 实现类
  3. UserService 业务接口
  4. UserServiceImpl 业务实现类

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

我们使用一个 Set 接口实现:

private UserDao userDao; //利用set进行动态实现值的注入!

public void setUserDao(UserDao userDao)
{
    this.userDao=userDao;
}
  • 之前,程序是主动创建对象!控制权在程序员手上
  • 使用 set 之后,程序不在具有主动性,而是被动的接受对象(对扩展开发,对修改关闭)

从本质上解决问题,不需要管理对象的创建,耦合性降低,专注于业务实现,这就是 IOC 的原型。

IOC 本质

IOC 控制反转是一种设计思想,DI(依赖注入) 是实现 IOC 的一种方法。没有 IOC 的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制。控制反转之后将对象的创建转移给第三方,个人理解控制反转就是:获得依赖对象的方式发生转变了

IOC 是 Spring 框架的核心内容,使用多种方式完美的实现的 Ioc,可以使用 xml 配置,也可以使用注解,新版本的 Spring 也可以零配置实现 IOC(自动装配)。

采用 xml 方式配置 Bean 的时候,Bean 的定义信息和实现是分离的,采用注解的方式可以把两者合为一体。Bean 的定义信息直接以注解的形式在定义在实现类中,从而达到零配置的目的。

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

3、HelloSpring

public class Hello {
    private String str;

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

    public String getStr() {
        return str;
    }

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

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

<?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="hello" class="com.kuang.pojo.Hello">
        <property name="str" value="Hello Spring"/>
        <!--
			ref:引用Spring容器中创建好的对象
			value:引用具体的值,基本数据类型
		-->
    </bean>
</beans>
public class MyTest {
    public static void main(String[] args) {
        //获取ApplicationContext:拿取Spring的容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //需要什么,就get什么
        Hello hello = (Hello) context.getBean("hello");

        System.out.println(hello.toString());
    }
}
  • Hello 对象是谁创建的?
    • hello 对象是由 Spring 创建的
  • Hello 对象的属性是怎么设置的
    • hello 对象的属性是由 Spring 容器设置的

上面就用配置文件的形式,实现了控制反转

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

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

依赖注入:利用 set 的方法来进行注入

IOC 是一种变成思想,由主动的编程变成被动的编程

所谓 IOC 就是:对象由 Spring 来创建,管理,装配!

4、IOC 创建对象的方式

<?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="com.kuang.pojo.User">
        <property name="name" value="saber01"/>
    </bean>

    <!--第一种:构造函数参数名赋值-->
    <bean id="user" class="com.kuang.pojo.User">
        <constructor-arg name="name" value="saber02"/>
    </bean>

    <!--第二种:构造函数参数索引赋值-->
    <bean id="user" class="com.kuang.pojo.User">
        <constructor-arg index="0" value="saber02"/>
    </bean>

    <!--第三种:构造函数参数类型匹配赋值-->
    <bean id="user" class="com.kuang.pojo.User">
        <constructor-arg type="java.lang.String" value="saber02"/>
    </bean>
</beans>

总结:在配置文件加载时,容器中管理的对象就已经被初始化了,且是单例的

5、Spring 配置

5.1、别名

<alias name="user" alias="userNew"/>
User user = (User) context.getBean("userNew");

添加了别名(alias),可以用别名获取对象

5.2、Bean 的配置

<!--
	id:bean的唯一标识,相当于对象名
	class:bean对象所对应的全限定名:包名+类型
	name:也是别名,而且name可以同时取多个,可以用各种符号分开
-->
<bean id="hello" class="com.kuang.pojo.Hello" name="user2 u2;u3,u4">
    <property name="str" value="Hello Spring"/>
</bean>

5.3、import

一般用于团队开发,可以将多个配置文件合并成一个:applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="beans.xml"/>
    <import resource="beans02.xml"/>

</beans>

更多的信息可以前往

进行查看

6、依赖注入

6.1、构造器方式注入

上面的就是

6.2、Set 方式注入【重点】

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

【环境搭建】

  1. 复杂类型

    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
  2. 真实测试对象

    //get set略
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbys;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
    }
  3. beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="student" class="com.kuang.pojo.Student">
            <property name="name" value="saber"/>
        </bean>
    
    </beans>
  4. 测试类

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

完善注入信息

<?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 id="address" class="com.kuang.pojo.Address">
        <property name="address" value="000000000000000"/>
    </bean>

    <bean id="student" class="com.kuang.pojo.Student">

        <property name="name" value="saber"/>
        <property name="address" ref="address"/>

        <!--数组-->
        <property name="books">
            <array>
                <value>a1</value>
                <value>a2</value>
                <value>a3</value>
                <value>a4</value>
            </array>
        </property>

        <!--list-->
        <property name="hobbys">
            <list>
                <value>l1</value>
                <value>l2</value>
                <value>l3</value>
                <value>l4</value>
            </list>
        </property>

        <!--Map-->
        <property name="card">
            <map>
                <entry key="a" value="a1"/>
                <entry key="b" value="b2"/>
                <entry key="c" value="c3"/>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value>s1</value>
                <value>s2</value>
                <value>s3</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null></null>
        </property>

        <!--Properties-->
        <property name="info">
            <props>
                <prop key="Email_1">1347216032@qq.com</prop>
                <prop key="Email_2">3081453914@qq.com</prop>
            </props>
        </property>
    </bean>
</beans>

6.3、拓展方式注入

C 与 P 命名空间

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.kuang.pojo.User" p:name="saber-u" p:age="18"></bean>

    <!--c命名空间注入,通过构造器注入属性的值:construct-args-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="saber-u-2" c:age="20"/>

</beans>

测试

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user2", User.class);

    System.out.println(user.toString());
}

注意:p/c 命名空间不能直接使用,要引入 xml 约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframew

6.4、bean 的作用域

image-20210324163350637

  1. 单例模式(spring 的默认模式)

    <!-- the following is equivalent, though redundant (singleton scope is the default) -->
    <bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

    image-20210324163751380

  2. 原型模式:每次从容器 get 的时候,都会产生一个新的对象

    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

    image-20210324163817956

  3. 其余的requestsessionapplicatoin这些只能在 web 开发中使用!

7、Bean 的自动装配

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

在 Spring 中有三种装配方式

  1. 在 xml 中显示的配置
  2. 在 java 中显示配置
  3. 隐式的自动装配 bean

7.1、ByName 自动装配

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

<!--
	byName:会自动在容器中上下文查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="perple" class="com.kuang.pojo.People" autowire="byName">
    <property name="name" value="saber"/>
</bean>

7.2、ByType 自动装配

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

<!--
	byType:会自动在容器中上下文查找,和自己对象属性类型相同的bean,
-->
<bean id="perple" class="com.kuang.pojo.People" autowire="byType">
    <property name="name" value="saber"/>
</bean>

小结:

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

7.3、使用注解实现自动装配

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

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

使用注解须知:

  1. 导入约束

  2. 配置注解的支持

    <?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>

@Autowired

直接在属性上使用即可,也可以在 set 方法使用

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

@Autowired 默认根据类型进行匹配,若 xml 中同时存在多个相同类型时,则按名称进行匹配,若没用相同名称的配置时,则需要使用@Qualifier(value = “name”)注解进行指定,同时@Autowired 中还存在 required 属性,默认为 true,若为 false 时,说明对象可以为 null,否则不允许为空

    <bean id="cat" class="com.kuang.pojo.Cat"></bean>
<!--    <bean id="dog" class="com.kuang.pojo.Dog"></bean>-->
    <bean id="dog2" class="com.kuang.pojo.Dog"></bean>
    <bean id="dog22" class="com.kuang.pojo.Dog"></bean>

    <bean id="perple" class="com.kuang.pojo.People">
        <property name="name" value="saber"/>
    </bean>
@Autowired
private Cat cat;

@Autowired
@Qualifier(value = "dog22")
private Dog dog;

private String name;

@Resource

@Resource(name = "cat22")
private Cat cat;

@Resource
private Dog dog;

小结:

@Resource 和@Autowired 的区别:

  • 都是用于自动装配,都可以放在字段上
  • @Autowired 通过 bytype 的方式实现,而且对象必须存在
  • @Resource 默认通过 byname 实现,如果找不到名字,则通过 bytyoe 实现,如果两个都找不到,则报错

8、使用注解开发

<!--注意:需要引入AOP包-->
<!--需要扫描的包-->
<context:component-scan base-package="com.kuang.pojo"/>
<context:annotation-config/>

image-20210324191701964

@Component 组件

//等价于<bean id="user" class="com.kuang.pojo.User"/> 无须在xml中写
@Component
public class User {
    public String name="saber";
}

@Value

@Component
public class User {
    //等价于 <property name="name" value="saber"/>
    @Value("SABER")
    public String name;
}

@Component 衍生注解,根据不同的层使用不同的注解

  1. dao 层【@Repository】
  2. service 层【@Service】
  3. controller 层【@Controller】

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

作用域也可以直接通过@Scope("")实现

小结:

xml 与注解:

  • xml 更加万能,适用于各种场合,维护简单
  • 注解不是自己类使用不了,维护相对复杂

xml 与注解的最佳实践:

  • xml 来管理 bean

  • 注解只负责完成属性的注入

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

    <!--需要扫描的包-->
    <context:component-scan base-package="com.kuang"/>
    <context:annotation-config/>

9、使用 Java 的方式配置 Spring

JavaConfig 是 Spring 的一个子项目

实体类:

package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//@Component
public class User {
    @Value("saber")
    private String name;

    public String getName() {
        return name;
    }

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

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

配置文件类:

package com.kuang.config;

import com.kuang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//这个也会被Spring容器托管,注册到容器中,因为它本身就是一个@Component
//@Configuration表示这是一个配置类,就是之前的beans.xml
@Configuration
//@ComponentScan("com.kuang.pojo") //扫描指定包
@Import(SaberConfig2.class) //相当于Import标签
public class SaberConfig {

    //注册一个bean 相当于之前的<bean>标签
    //方法名相当于bean标签的id
    //返回值相当于bean标签的class属性
    @Bean
    public User getUser(){
        return new User();//我们要注入到bean的对象
    }
}
//如果你没用扫描指定包@ComponentScan("com.kuang.pojo") 则必须使用@Bean主动进行配置,
//如果使用@ComponentScan("com.kuang.pojo")扫描,则扫描对象必须包含@Component

测试方法:

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SaberConfig.class);
    User user = ctx.getBean(User.class);//也可以用方法名getUser,也可以用@Bean指定的名称
    System.out.println(user.getName());
}

10、代理模式

代理模式的分类:

  • 静态代理
  • 动态代理

image-20210325152819245

10.1、静态代理

角色分析:

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

代码步骤:

  1. 接口

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

    public class Host implements Rent{
        @Override
        public void rent() {
            System.out.println("房东要租房子");
        }
    }
  3. 代理角色

    public class Proxy implements Rent{
    
        private Host host;
    
        public Proxy() {
        }
    
        public Proxy(Host host) {
            this.host = host;
        }
    
        @Override
        public void rent() {
            seeHouse();
            host.rent();
            hetong();
            fare();
        }
    
        public void seeHouse(){
            System.out.println("kan fang");
        }
    
        public void hetong(){
            System.out.println("qian he tong");
        }
    
        public void fare(){
            System.out.println("shou zhong jie fei");
        }
    }
  4. 客户端访问代理角色

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

代理模式的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共服务
  • 公共服务交给代理角色,实现业务划分
  • 公共业务发生扩展时,方便集中管理

缺点:

  • 一个真实角色就会产生一个代理角色,代码量翻倍,开发效率变低

10.2、动态代理

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

需要了解两个类:Proxy:代理, InvocationHandler:调用处理程序

动态代理的好处:

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

动态代理的代码实现:

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

public class StarProxy implements InvocationHandler
{
    // 目标类,也就是被代理对象
    private Object target;

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    {
        // 这里可以做增强
        System.out.println("收钱");

        Object result = method.invoke(target, args);

        return result;
    }

    // 生成代理类
    public Object CreatProxyedObj()
    {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

}

11、AOP

11.1、什么是 AOP

AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,AOP 是 OOP 的延续,是软件开发中的热点,是函数是编程的一种衍生范型,利用 AOP 可以对业务逻辑的各部分进行隔离,从而使业务逻辑各部分之间的耦合降低,提高可重用性。

11.2、AOP 在 Spring 的作用

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

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

image-20210326092139762

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

image-20210326092851839

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

11.3、使用 Spring 实现 AOP

使用 AOP 织入,需要导入一个依赖包

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

方法一:使用 Spring 接口

@Component
public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //objects:参数
    //o:目标对象
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName() +"的"+method.getName()+"方法被执行");
    }
}
@Component
public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName() +"方法,返回结果:"+returnValue);
    }
}

上面实现两个测试用的切面方法

<?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
        https://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.kuang"/>
    <context:annotation-config/>

    <!--方法一:使用原生SpringAPI接口-->
    <!--配置aop:需要导入aop约束-->
    <aop:config>
        <!--DOTO:切入点:expression:表达式,execution(要执行的位置!)-->
        <aop:pointcut id="pointuct" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

        <!--要执行的环绕增加-->
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointuct"/>
        <aop:advisor advice-ref="log" pointcut-ref="pointuct"/>
    </aop:config>

</beans>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userServiceImpl",UserService.class);

        userService.add();
    }
}

方法二:自定义类实现 AOP

将配置文件修改为:

<?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
        https://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.kuang"/>
    <context:annotation-config/>

    <!--方法二:自定义类-->
    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect ref="diyPintCut">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

            <!--通知-->
            <aop:after method="after" pointcut-ref="point"/>
            <aop:before method="before" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

</beans>

实现的自定义类:

import org.springframework.stereotype.Component;
@Component
public class DiyPintCut {
    public void before(){
        System.out.println("=======berfre========");
    }
    public void after(){
        System.out.println("=======after========");
    }
}

方式三:使用注解实现

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;
import org.springframework.stereotype.Component;

@Aspect //标注这是一个切面
@Component
public class AnnotationPointCut {
    @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")//注意 这是annotation包下的注解
    public void before() {
        System.out.println("=======berfre========");
    }

    @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("=======after========");
    }

    //在环绕增强中,我们可以定一个参数,代表我们要获取处理的切入点
    @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("before-around");
        System.out.println(jp.getSignature());//获取签名

        Object proceed = jp.proceed();//执行方法 proceed:返回值

        System.out.println("after-around");
        System.out.println(proceed);
    }
}
<?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
        https://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.kuang"/>
    <context:annotation-config/>
    <!--开启注解支持 需要添加这个才能使用注解AOP-->
    <!--有一个参数 默认为false 则为使用JDK实现动态代理 true则使用cglib   proxy-target-class="true"-->
    <aop:aspectj-autoproxy/>

</beans>

参考:

文章:https://www.jianshu.com/p/34efe69582c8