黑马程序员技术交流社区

标题: 【成都校区】SpringAOP前置和后置通知 [打印本页]

作者: 小蜀哥哥    时间: 2019-12-6 12:03
标题: 【成都校区】SpringAOP前置和后置通知
AOP 配置:
加入 JAR 包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
在配置文件中加入 AOP 的命名空间在配置文件中加入
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
把横切关注点的代码抽象到切面类中.切面首先是一个 IOC 中的 bean, 即加入 @Component 注解切面还需加入 @Aspect 注解在类中声明各种通知:声明一个方法在方法前加入 @Before 注解可以在通知方法中声明一个类型 JoinPoint 的参数, 然后就能访问链接细节. 如方法名称和参数

[Java] 纯文本查看 复制代码
public interface ArithmeticCalculator {
        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("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
        @Override
        public int add(int i, int j) {
                int result = i + j;
                return result;
        }
        @Override
        public int sub(int i, int j) {
                int result = i - j;
                return result;
        }
        @Override
        public int mul(int i, int j) {
                int result = i * j;
                return result;
        }
        @Override
        public int div(int i, int j) {
                int result = i / j;
                return result;
        }
}

/**
* AOP 的 helloWorld
* 1. 加入 jar 包
* com.springsource.net.sf.cglib-2.2.0.jar
* com.springsource.org.aopalliance-1.0.0.jar
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
* spring-aspects-4.0.0.RELEASE.jar
*
* 2. 在 Spring 的配置文件中加入 aop 的命名空间。
*
* 3. 基于注解的方式来使用 AOP
* 3.1 在配置文件中配置自动扫描的包: <context:component-scan base-package="com.spring.aop.Impl"></context:component-scan>
* 3.2 加入使 AspjectJ 注解起作用的配置: <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
* 为匹配的类自动生成动态代理对象.
*
* 4. 编写切面类:
* 4.1 一个一般的 Java 类
* 4.2 在其中添加要额外实现的功能.
*
* 5. 配置切面
* 5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解
* 5.2 声明是一个切面: 添加 @Aspect
* 5.3 声明通知: 即额外加入功能对应的方法.
* 5.3.1 前置通知: @Before("execution(public int com.spring.aop.ArithmeticCalculator.*(int, int))")
* @Before 表示在目标方法执行之前执行 @Before 标记的方法的方法体.
* @Before 里面的是切入点表达式:
*
* 6. 在通知中访问连接细节: 可以在通知方法中添加 JoinPoint 类型的参数, 从中可以访问到方法的签名和方法的参数.
*
* 7. @After 表示后置通知: 在方法执行之后执行的代码.
*/
//通过添加 @Aspect 注解声明一个 bean 是一个切面!
@Aspect
@Component
public class LoggingAspect {
        @Before("execution(public int com.spring.aop.Impl.ArithmeticCalculator.add(int, int))")
        public void beforeMethod(JoinPoint joinPoint){
                String methodName = joinPoint.getSignature().getName();
                Object [] args = joinPoint.getArgs();
               
                System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
        }
        @After("execution(* com.spring.aop.Impl.*.*(int, int))")
        public void afterMethod(JoinPoint joinPoint){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method " + methodName + " ends");
        }
}

<?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"
        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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd">
        <context:component-scan base-package="com.spring.aop.Impl"></context:component-scan>
        <!-- 使 AspjectJ 注解起作用: 自动为匹配的类生成代理对象 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>


public class Main {
        
        public static void main(String[] args) {
                 
                //1. 创建 Spring 的 IOC 容器
                ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-aop.xml");
               
                //2. 从 IOC 容器中获取 bean 实例
                ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
               
                //3. 使用 bean
                int add = arithmeticCalculator.add(2, 2);
                System.out.println(add);
        }
        
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2