1.在pom.xml文件中添加jar包依赖,spring-aspects
2.编写业务逻辑代码:MathCalculator.java
3.编写切面类:LogAspects.java
1.@Before
2.@After
3.@AfterReturning
4.@AfterThrowing
5.@Around
6.@Pointcut
需要注意:注解里面的value写上需要切面的方法,如下所示:
@Before(“com.spring.aop.LogAspects.pointCut()”)
@AfterReturning和@AfterThrowing注解还需要配置返回值和异常信息,如下所示:
@AfterReturning(value = “com.spring.aop.LogAspects.pointCut()”,returning = “obj”)
@AfterThrowing(value = “com.spring.aop.LogAspects.pointCut()”,throwing = “e”)
获取切面方法的相关内容可以通过在切面类方法参数里面配置JoinPoint类的方法实现,如下所示:
获取切面方法名:joinPoint.getSignature().getName()
4.编写主配置类:MainConfigOfAOP.java,添加逻辑代码和切面类的组件
5.在主配置类中添加注解 @EnableAspectJAutoProxy,启用AOP功能
6.在切面类中添加注解@Aspect
代码如下所示:
业务逻辑类
public class MathCalculator {
public int div(int i,int j){
System.out.println("MathCalculator...div...");
return i/j;
}
}
切面类
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import java.awt.*;
import java.util.Arrays;
@Aspect//告诉Spring当前类是一个切面类
public class LogAspects {
@Pointcut("execution(public int com.spring.aop.MathCalculator.div(..))")
public void pointCut(){}
@Before("com.spring.aop.LogAspects.pointCut()")
public void logStart(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
System.out.println("方法"+joinPoint.getSignature().getName()+"运行。。。参数列表是{"+ Arrays.asList(args)+"}");
}
@After("com.spring.aop.LogAspects.pointCut()")
public void logEnd(JoinPoint joinPoint){
System.out.println("方法"+joinPoint.getSignature().getName()+"结束。。。");
}
@AfterReturning(value = "com.spring.aop.LogAspects.pointCut()",returning = "obj")
public void logReturn(JoinPoint joinPoint,Object obj){
System.out.println("方法"+joinPoint.getSignature().getName()+"返回。。。运行结果是{"+obj+"}");
}
@AfterThrowing(value = "com.spring.aop.LogAspects.pointCut()",throwing = "e")
public void logException(JoinPoint joinPoint,Exception e){
System.out.println("方法"+joinPoint.getSignature().getName()+"异常返回内容为:"+e);
}
}
主配置类
package com.spring.config;
import com.spring.aop.LogAspects;
import com.spring.aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class MainConfigOfAOP {
@Bean
public MathCalculator mathCalculator(){
return new MathCalculator();
}
@Bean
public LogAspects logAspects(){
return new LogAspects();
}
}
pom.xml文件的相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
test类
package com.spring.test;
import com.spring.aop.MathCalculator;
import com.spring.config.MainConfigOfAOP;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AOPTest {
@Test
public void test01(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
MathCalculator bean = applicationContext.getBean(MathCalculator.class);
bean.div(2,1);
}
}
运行结果:
方法div运行。。。参数列表是{[2, 1]}
MathCalculator...div...
方法div结束。。。
方法div返回。。。运行结果是{2}