【关于Spring那些事】——Spring AOP的实现

在Spring中使用AOP可以通过XML配置的方式,也可以通过注解的方式,下面通过简单的案例分别使用。

一、基于XML模式的配置

  1. 创建maven项目

  2. 添加依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.18</version>
</dependency>
  1. Spring配置文件,并引入aop的命名空间
<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>
  1. 新建一个Student类,并添加两个方法
public class Student {
    
    
    public void update() {
    
    
        System.out.println("修改学生信息!!!");
    }
    public void delete() {
    
    
        System.out.println("删除学生信息!!!");
    }
}
  1. 创建一个切面类
public class MyAspect {
    
    
    public void before(){
    
    
        System.out.println("----------方法执行前----------");
    }
    public void after(){
    
    
        System.out.println("----------方法执行后----------");
    }
}
  1. 配置AOP
<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 id="student" class="com.aop.demo1.StudentImpl"/>
    <!--声明切面类-->
    <bean id="myAspect" class="com.aop.demo1.MyAspect"/>
    <!--aop的配置-->
    <aop:config>
        <!--声明切入点-->
        <aop:pointcut id="pointCut" expression="execution(* com.aop.demo1.StudentImpl.*(..))"/>
        <!--切面-->
        <aop:aspect ref="myAspect">
            <!--前置通知-->
            <aop:before method="before" pointcut-ref="pointCut"/>
            <!--后置通知-->
            <aop:after method="after" pointcut-ref="pointCut"/>
        </aop:aspect>
    </aop:config>

</beans>
  1. 测试
public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentImpl student = (StudentImpl) context.getBean("student");
        //student.update();
        student.delete();
    }
}

二、使用注解实现

  1. 编写一个注解实现的类
@Aspect
public class AnnotationRealize {
    
    
    @Before("execution(* com.aop.demo1.Student.*(..))")
    public void before(){
    
    
        System.out.println("=======方法执行前=======");
    }
    @After("execution(* com.aop.demo1.Student.*(..))")
    public void after(){
    
    
        System.out.println("=======方法执行后=======");
    }
    @Around("execution(* com.aop.demo1.Student.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        System.out.println("----------环绕前----------");
        Object proceed = joinPoint.proceed();
        System.out.println("----------环绕后----------");
    }
}
  1. 在Spring配置文件中,注册bean并且添加支持注解的配置
<bean id="annotationRealize" class="com.aop.demo1.AnnotationRealize"/>
<!--通过XML配置启用@AspectJ支持  注解支持-->
<aop:aspectj-autoproxy/>

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

猜你喜欢

转载自blog.csdn.net/weixin_52986315/article/details/124283778