面向切面编程:
基于Spring的AOP(注解方式)
1-配置:pom文件:
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring-aspects会帮我们传递过来aspectjweaver -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
2-规定包结构
因为我们使用的是jdk的aop,所以必须需要 "接口":
细节:
"接口、实现类、切面类" ---- > "必须被spring管理"(所以需要spring包扫描)
-
2.1-包扫描:
<?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 http://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">
<!--基于注解的AOP的实现:
1、将目标对象和切面交给IOC容器管理(注解+扫描)
2、开启AspectJ的自动代理,为目标对象自动生成代理-<aop:aspectj-autoproxy/>
3、将切面类通过注解@Aspect标识
-->
<!--2-开启基于'注解的aop'-->
<context:component-scan base-package="com.atguigu.spring.aop.annotation"/>
</beans>
-
2.2-接口:
public interface Calculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
-
2.3-实现类:
@Component
public class CalculatorPureImpl implements Calculator {
@Override
public int add(int i, int j) {
//Before
int result = i + j;
//AfterRunning
//AfterThrowing,异常时候会执行
return result;
//After,不管怎么样都会执行
}
@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;
}
}
-
2.4-切面类:
package com.atguigu.spring.aop.annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import java.util.Arrays;
// @Aspect表示这个类是一个切面类
@Aspect
// @Component注解保证这个切面类能够放入IOC容器
@Component
@ComponentScan("com.atguigu.spring.aop.annotation")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LogAspect {
//切入点表达式的"重用"
@Pointcut("execution( * com.atguigu.spring.aop.annotation.CalculatorPureImpl.* (..))")
public void pointCut(){}
//@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorPureImpl.add(int,int))")
//@Before("execution( * com.atguigu.spring.aop.annotation.CalculatorPureImpl.* (..))")
@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs()); //返回的是Object[],用Arrays.toString包一下,不然输出的是地址
System.out.println("Logger-->前置通知,方法名:" + methodName + ",参 数:" + args);
}
/**
* 相当于catch,finally的部分,不论怎么样,都会执行
*/
@After("pointCut()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
//String args = Arrays.toString(joinPoint.getArgs()); //不需要参数
System.out.println("Logger-->后置通知,方法名:" + methodName +" 执行完毕····");
}
/**
* 细节:
1- returning = "result" 和 Object result 的"result名字",需要对应 == 返回值
*/
@AfterReturning(value = "pointCut()",returning = "result")
public void afterRunningMethod(JoinPoint joinPoint , Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->中间通知,方法名:" + methodName +" 方法的返回值"+result);
}
/**
* 在异常的时候,爆出:AfterThrowing
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "pointCut()",throwing = "ex")
public void affterThrowingMethod(JoinPoint joinPoint , Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->异常通知,方法名:" + methodName +" 方法存在异常: "+ex);
}
}
3-测试:
细节:
被spring配置成代理类,就不可以使用"目标对象",只能使用他的"代理类和父类",
所以这个地方不可以使用CalculatorPureImpl这个目标对象,只能使用它的"父类"-Calculator获取代理对象
package com.atguigu.spring.testes;
import com.atguigu.spring.aop.annotation.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTestes {
@Test
public void getAnnotationAop() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
/**
被spring配置成代理类,就不可以使用"目标对象",只能使用他的"代理类和父类",
所以这个地方不可以使用CalculatorPureImpl这个目标对象,只能使用它的"父类"-Calculator获取代理对象
*/
Calculator calculatorBean = ioc.getBean(Calculator.class);
calculatorBean.add(1,1);
calculatorBean.div(1,0);
}
}
标签:int,Spring,joinPoint,spring,result,AOP,注解,import,public
From: https://www.cnblogs.com/chen-zhou1027/p/17278435.html