一、xml形式
1.1、切面类
package aop;
import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Component //注解形式生产bean无需xml配置
public class XmlAspect {
public void doBefore(){
System.out.println("前置通知");
}
//声明后置通知
public void doAfterReturning(String result){
System.out.println("后置通知");
System.out.println("---" + result + "---");
}
//声明异常通知
public void doAfterThrowing(Exception e){
System.out.println("异常通知");
System.out.println(e.getMessage());
}
public void doAfter(){
System.out.println("最终通知");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
Gson gson = new Gson();
System.out.println("代理信息:被代理对象:"+ pjp.getTarget() + ",代理方法:"+ (MethodSignature)pjp.getSignature() +",方法参数:" + gson.toJson(pjp.getArgs()));
System.out.println("进入方法---环绕通知");
Object o = pjp.proceed();
System.out.println("退出方法---环绕通知");
return o;
}
}
1.2.目标对象:AServiceImpl
package aop;
import org.springframework.stereotype.Component;
@Component //注解形式生产bean无需xml配置
public class AServiceImpl implements AService {
@Override
public void barA(){
System.out.println("AServiceImpl.barA()");
}
@Override
public void fooA(String msg){
System.out.println("AServiceImpl.fooA(msg:" + msg + ")");
}
}
1.3.目标对象:BServiceImpl
package aop;
import org.springframework.stereotype.Component;
@Component //注解形式生产bean无需xml配置
public class BServiceImpl {
public void barB(String msg, int type){
System.out.println("BServiceImpl.barB(msg:"+msg+"type:"+type+")");
if (type == 1){
throw new IllegalArgumentException("测试异常");
}
}
public void fooB(){
System.out.println("BServiceImpl.fooB()");
}
}
1.4.applicationContext:Spring xml配置
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="aop"/>
<context:annotation-config/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--xml 配置aop-->
<aop:config>
<aop:aspect id="TestAspect" ref="xmlAspect">
<aop:pointcut id="businessService" expression="execution(* aop.*.*(..))"></aop:pointcut>
<aop:around pointcut-ref="businessService" method="doAround"></aop:around>
</aop:aspect>
</aop:config>
</beans>
1.5.测试类
import aop.AService;
import aop.BServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
@Test
public void aopAnnonationTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
AService aService = (AService) ac.getBean("AServiceImpl");
aService.fooA("test");
BServiceImpl bService = (BServiceImpl)ac.getBean("BServiceImpl");
bService.barB("message info", 2);
}
}
1.6.输出结果
二、注解形式
2.1切面类
package aop;
import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component //注解形式生产bean无需xml配置
public class AnnotationAspect {
@Pointcut("execution(* aop.*.*(..))")
private void pointCutMethod(){
}
//声明前置通知
@Before("pointCutMethod()")
public void doBefore(){
System.out.println("前置通知");
}
//声明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "result")
public void doAfterReturning(String result){
System.out.println("后置通知");
System.out.println("---" + result + "---");
}
//声明异常通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e){
System.out.println("异常通知");
System.out.println(e.getMessage());
}
@After("pointCutMethod()")
public void doAfter(){
System.out.println("最终通知");
}
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
Gson gson = new Gson();
System.out.println("代理信息:被代理对象:"+ pjp.getTarget() + ",代理方法:"+ (MethodSignature)pjp.getSignature() +",方法参数:" + gson.toJson(pjp.getArgs()));
System.out.println("进入方法---环绕通知");
Object o = pjp.proceed();
System.out.println("退出方法---环绕通知");
return o;
}
}
2.2 applicationContext:Spring xml配置
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="aop"/>
<context:annotation-config/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--xml 配置aop-->
<!--<aop:config>
<aop:aspect id="TestAspect" ref="xmlAspect">
<aop:pointcut id="businessService" expression="execution(* aop.*.*(..))"></aop:pointcut>
<aop:around pointcut-ref="businessService" method="doAround"></aop:around>
</aop:aspect>
</aop:config>-->
</beans>
目标类同1.3、1.4,测试类同1.5
2.3 输出结果
最后,再拓展提醒下:
任何通知(Advice)方法可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型。JoinPoint接口提供了一系列有用的方法, 比如 getArgs() (返回方法参数)、getThis() (返回代理对象)、getTarget() (返回目标)、getSignature() (返回正在被通知的方法相关信息)和 toString() (打印出正在被通知的方法的有用信息。
其中getSignature()返回的Signature对象可强制转换为MethodSignature,其功能非常强大,能获取包括参数名称在内的一切方法信息。
附pom依赖(Spring各组件版本尽量保持一致,不然由于版本不一致有可能会报错):
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!--spring所需依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!--aspectJ-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.3</version>
</dependency>
拓展练习:
AOP注解的方式校验参数:https://blog.csdn.net/qq496013218/article/details/78595572
AOP实现机制:https://www.iteye.com/topic/1116696