栗子:
项目结构
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--启用自动扫描加载指定的bean-->
<context:component-scan base-package="com.lyx.service,com.lyx.aspect">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* com.lyx.service.*.*(..))"/>
<aop:aspect ref="myAspect" order="2">
<aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut" throwing="ex"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut" returning="str"/>
</aop:aspect>
<aop:aspect ref="myAspect2" order="1">
<aop:around method="around" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
Target.java
@Component("target")
public class Target
{
public String addUser(String name)
{
System.out.println("添加用户:" + name);
return name;
}
public void deleteUser(Integer id)
{
System.out.println("删除用户,id为:" + id);
if (id<0)
{
throw new IllegalArgumentException("用户id不小于0");
}
}
}
MyAspect.java
@Component
public class MyAspect
{
public void before()
{
System.out.println("【Before】");
}
public void afterReturning(String str)
{
System.out.println("【AfterReturning】,目标方法返回String值:"+str);
}
public void afterThrowing(Exception ex)
{
System.out.println("【AfterThrowing】,抛出的异常:" + ex);
}
public void after(JoinPoint jp)
{
System.out.println("【after】");
System.out.println(jp.getSignature().getName());
System.out.println(Arrays.toString(jp.getArgs()));
System.out.println(jp.getTarget());
System.out.println(jp.getThis());
}
}
MyAspect2.java
public class MyAspect2
{
//Around方法会替换原来的方法,所以一般必须回调原方法,否则会阻止原方法的执行
public Object around(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("【around_Up】");
//获取原方法的参数
var args = pjp.getArgs();
if (args!=null && args.length>0 && args[0].getClass()==String.class)
//修改原方法的参数
args[0]="【参数前缀】"+args[0];
//回调原方法(目标方法)
var obj = pjp.proceed(args);
System.out.println("【around_Down】");
//返回目标方法的返回值(保持一致)
return obj;
}
}
MyDriver.java
public class MyDriver
{
public static void main(String[] args) throws Exception {
var ctx = new ClassPathXmlApplicationContext("beans.xml");
var t = (Target)ctx.getBean("target");
t.addUser("玛卡巴卡");
System.out.println("-------------------------------------------------------------------");
t.deleteUser(-2);
}
}
执行结果
【around_Up】
【Before】
添加用户:【参数前缀】玛卡巴卡
【after】
addUser
[【参数前缀】玛卡巴卡]
com.lyx.service.Target@bf71cec
com.lyx.service.Target@bf71cec
【AfterReturning】,目标方法返回String值:【参数前缀】玛卡巴卡
【around_Down】
-------------------------------------------------------------------
【around_Up】
【Before】
删除用户,id为:-2
【after】
deleteUser
[-2]
com.lyx.service.Target@bf71cec
com.lyx.service.Target@bf71cec
【AfterThrowing】,抛出的异常:java.lang.IllegalArgumentException: 用户id不小于0
Exception in thread "main" java.lang.IllegalArgumentException: 用户id不小于0
......
基于注解的SpirngAOP实现请见:基于注解的SpirngAOP实现