目 录CONTENT

文章目录

spring的Aop(3)

Eric
2022-02-15 / 0 评论 / 0 点赞 / 151 阅读 / 1,943 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-12-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1 概念

  • 面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2 AOP底层原理

2.1 概述

  • AOP的底层原理:

    • ①使用JDK的动态代理。
    • ②使用CGLIB的动态代理。

2.2 应用示例

  • 示例:使用JDK的动态代理

  • UserDao.java

package top.open1024.spring.dao;

public interface UserDao {

    int add(int a, int b);

    String update(String id);

}
  • UserDaoImpl.java
package top.open1024.spring.dao.impl;

import top.open1024.spring.dao.UserDao;

public class UserDaoImpl implements UserDao {
    @Override
    public int add(int a, int b) {

        System.out.println("add...执行了");

        return a + b;
    }

    @Override
    public String update(String id) {

        System.out.println("update...执行了");

        return id;
    }
}
  • 测试:
package top.open1024.spring;

import top.open1024.spring.dao.UserDao;
import top.open1024.spring.dao.impl.UserDaoImpl;

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

public class SpringTest {
    public static void main(String[] args) {

        UserDao userDao = new UserDaoImpl();

        UserDao proxy = (UserDao) Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("在" + method.getName() + "之前执行");
                Object result = method.invoke(userDao, args);
                System.out.println("在" + method.getName() + "之后执行");
                return result;
            }
        });

        int result = proxy.add(1, 2);
        System.out.println("result = " + result);

        String id = proxy.update("1");
        System.out.println("id = " + id);

    }
}

3 AOP术语

  • 连接点:类里面那些方法可以被增强,这些方法称为连接点。

  • 切入点:实际被真正增强的方法,称为切入点。

  • 通知(增强):实际增强的逻辑部分,称为通知(增强)。

  • 通知的类型:

    • 前置通知。
    • 后置通知。
    • 环绕通知。
    • 异常通知。
    • 最终通知。
  • 切面:把通知应用到切入点的过程。

4 AOP的准备

  • 在Spring框架一般基于AspectJ(AspectJ不是Spring的部分,是一个独立的AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP的操作)来实现AOP操作。

  • 导入相关jar包的Maven坐标:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.23</version>
</dependency>
  • 切入点表达式:

    • 作用:对哪个类里面的哪个方法进行增强。
    • 语法:
execution([权限修饰符][返回类型][类全路径][方法名称([参数列表])])

例子:

# 对top.open1024.spring.dao.BookDao类中add方法进行增强:
execution(* top.open1024.spring.dao.BookDao.add(..))
# 对top.open1024.spring.dao.BookDao类中的所有方法进行增强
execution(* top.open1024.spring.dao.BookDao.*(..))

5 AspectJ注解形式应用示例

  • 被增强的类User.java
package top.open1024.spring;

import org.springframework.stereotype.Component;

/**
 * 被增强类
 */
@Component
public class User {

    public void add(){
        System.out.println("add ...");
    }
}
  • 增强的类,即代理类UserAspect.java
package top.open1024.spring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 增强的类
 */
@Component
@Aspect //生成代理对象
@Order(1) //在多个增强类对同一个方法进行增强,设置增强类的优先级,@Order中的value属性值越小优先级越高
public class UserAspect {
    /**
     * 切入点表达式
     */
    @Pointcut("execution(* top.open1024.spring.User.add(..))")
    public void pointcut() {
    }

    /**
     * 前置通知
     */
    @Before(value = "pointcut()")
    public void beforeAdd() {
        System.out.println("before add ...");
    }

    /**
     * 后置通知
     */
    @AfterReturning(value = "pointcut()", returning = "obj")
    public void afterReturningAdd(Object obj) {
        System.out.println("afterReturning add ..." + obj);
    }


    /**
     * 环绕通知
     */
    @Around(value = "pointcut()")
    public void aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后");
    }

    /**
     * 异常通知
     */
    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public void afterThrowingAdd(Exception ex) {
        System.out.println("afterThrowing add ..." + ex);
    }

    /**
     * 最终通知
     */
    @After(value = "pointcut()")
    public void afterAdd() {
        System.out.println("after add ...");
    }
}
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="top.open1024.spring"></context:component-scan>


    <!-- 开启aspectj生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
  • 测试:
package top.open1024.spring;

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

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        User user = context.getBean(User.class);

        user.add();
    }
}

6 完全使用注解开发AspectJ

  • 被增强的类User.java
package top.open1024.spring;

import org.springframework.stereotype.Component;

/**
 * 被增强类
 */
@Component
public class User {

    public void add(){
        System.out.println("add ...");
    }
}
  • 增强的类,即代理类UserAspect.java
package top.open1024.spring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 增强的类
 */
@Component
@Aspect //生成代理对象
@Order(1) //在多个增强类对同一个方法进行增强,设置增强类的优先级,@Order中的value属性值越小优先级越高
public class UserAspect {
    /**
     * 切入点表达式
     */
    @Pointcut("execution(* top.open1024.spring.User.add(..))")
    public void pointcut() {
    }

    /**
     * 前置通知
     */
    @Before(value = "pointcut()")
    public void beforeAdd() {
        System.out.println("before add ...");
    }

    /**
     * 后置通知
     */
    @AfterReturning(value = "pointcut()", returning = "obj")
    public void afterReturningAdd(Object obj) {
        System.out.println("afterReturning add ..." + obj);
    }


    /**
     * 环绕通知
     */
    @Around(value = "pointcut()")
    public void aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后");
    }

    /**
     * 异常通知
     */
    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public void afterThrowingAdd(Exception ex) {
        System.out.println("afterThrowing add ..." + ex);
    }

    /**
     * 最终通知
     */
    @After(value = "pointcut()")
    public void afterAdd() {
        System.out.println("after add ...");
    }
}
  • SpringConfig.java
package top.open1024.spring;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = "top.open1024.spring")
@Configurable
public class SpringConfig {
}
  • 测试
package top.open1024.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        User user = context.getBean(User.class);

        user.add();
    }

}

7 XML形式应用示例

  • 被增强的类User.java
package top.open1024.spring;

/**
 * 被增强类
 */
public class User {

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

}
  • 增强的类,即代理类UserAspect.java
package top.open1024.spring;

/**
 * 增强的类
 */
public class UserAspect {

    /**
     * 前置通知
     */
    public void beforeAdd() {
        System.out.println("before add ...");
    }
}
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="user" class="top.open1024.spring.User"></bean>

    <bean id="userAspect" class="top.open1024.spring.UserAspect"></bean>

    <!-- 配置aop -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* top.open1024.spring.User.add(..)))"/>
        <!-- 配置切面 -->
        <aop:aspect ref="userAspect">
            <!-- 配置增强作用在具体的方法上 -->
            <aop:before method="beforeAdd" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>

</beans>
  • 测试:
package top.open1024.spring;

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

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        User user = context.getBean(User.class);

        user.add();
    }
}
0

评论区