一 配置
<?xml version="1.0" encoding="GBK"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<aop:config>
<!-- 将fourAdviceBean转换成切面Bean 切面Bean的新名称为:fourAdviceAspect 指定该切面的优先级为2 -->
<aop:aspect id="fourAdviceAspect" ref="fourAdviceBean"
order="2">
<!-- 定义一个After增强处理, 直接指定切入点表达式 以切面Bean中的release()方法作为增强处理方法 -->
<aop:after
pointcut="execution(* org.crazyit.app.service.impl.*.*(..))"
method="release" />
<!-- 定义一个Before增强处理, 直接指定切入点表达式 以切面Bean中的authority()方法作为增强处理方法 -->
<aop:before
pointcut="execution(* org.crazyit.app.service.impl.*.*(..))"
method="authority" />
<!-- 定义一个AfterReturning增强处理, 直接指定切入点表达式 以切面Bean中的log()方法作为增强处理方法 -->
<aop:after-returning
pointcut="execution(* org.crazyit.app.service.impl.*.*(..))"
method="log" returning="rvt" />
<!-- 定义一个Around增强处理, 直接指定切入点表达式 以切面Bean中的processTx()方法作为增强处理方法 -->
<aop:around
pointcut="execution(* org.crazyit.app.service.impl.*.*(..))"
method="processTx" />
</aop:aspect>
<!-- 将secondAdviceBean转换成切面Bean 切面Bean的新名称为:secondAdviceAspect 指定该切面的优先级为1,该切面里的增强处理将被优先织入 -->
<aop:aspect id="secondAdviceAspect" ref="secondAdviceBean"
order="1">
<!-- 定义一个Before增强处理, 直接指定切入点表达式 以切面Bean中的authority()方法作为增强处理方法 且该参数必须为String类型(由authority方法声明中msg参数的类型决定) -->
<aop:before
pointcut="execution(* org.crazyit.app.service.impl.*.*(..)) and args(aa)"
method="authority" />
</aop:aspect>
</aop:config>
<!-- 定义一个普通Bean实例,该Bean实例将被作为Aspect Bean -->
<bean id="fourAdviceBean"
class="org.crazyit.app.aspect.FourAdviceTest" />
<!-- 再定义一个普通Bean实例,该Bean实例将被作为Aspect Bean -->
<bean id="secondAdviceBean"
class="org.crazyit.app.aspect.SecondAdviceTest" />
<bean id="hello" class="org.crazyit.app.service.impl.HelloImpl" />
<bean id="world" class="org.crazyit.app.service.impl.WorldImpl" />
</beans>
二 切面类
FourAdviceTest
package org.crazyit.app.aspect;
import org.aspectj.lang.*;
import java.util.Arrays;
public class FourAdviceTest {
public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
System.out.println("Around增强:执行目标方法之前,模拟开始事务...");
// 访问执行目标方法的参数
Object[] args = jp.getArgs();
// 当执行目标方法的参数存在,
// 且第一个参数是字符串参数
if (args != null && args.length > 0 && args[0].getClass() == String.class) {
// 修改目标方法调用参数的第一个参数
args[0] = "【增加的前缀】" + args[0];
}
// 执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(args);
System.out.println("Around增强:执行目标方法之后,模拟结束事务...");
// 如果rvt的类型是Integer,将rvt改为它的平方
if (rvt != null && rvt instanceof Integer)
rvt = (Integer) rvt * (Integer) rvt;
return rvt;
}
public void authority(JoinPoint jp) {
System.out.println("②Before增强:模拟执行权限检查");
// 返回被织入增强处理的目标方法
System.out.println("②Before增强:被织入增强处理的目标方法为:" + jp.getSignature().getName());
// 访问执行目标方法的参数
System.out.println("②Before增强:目标方法的参数为:" + Arrays.toString(jp.getArgs()));
// 访问被增强处理的目标对象
System.out.println("②Before增强:被织入增强处理的目标对象为:" + jp.getTarget());
}
public void log(JoinPoint jp, Object rvt) {
System.out.println("AfterReturning增强:获取目标方法返回值:" + rvt);
System.out.println("AfterReturning增强:模拟记录日志功能...");
// 返回被织入增强处理的目标方法
System.out.println("AfterReturning增强:被织入增强处理的目标方法为:" + jp.getSignature().getName());
// 访问执行目标方法的参数
System.out.println("AfterReturning增强:目标方法的参数为:" + Arrays.toString(jp.getArgs()));
// 访问被增强处理的目标对象
System.out.println("AfterReturning增强:被织入增强处理的目标对象为:" + jp.getTarget());
}
public void release(JoinPoint jp) {
System.out.println("After增强:模拟方法结束后的释放资源...");
// 返回被织入增强处理的目标方法
System.out.println("After增强:被织入增强处理的目标方法为:" + jp.getSignature().getName());
// 访问执行目标方法的参数
System.out.println("After增强:目标方法的参数为:" + Arrays.toString(jp.getArgs()));
// 访问被增强处理的目标对象
System.out.println("After增强:被织入增强处理的目标对象为:" + jp.getTarget());
}
}
SecondAdviceTest
package org.crazyit.app.aspect;
import org.aspectj.lang.*;
import java.util.Arrays;
public class SecondAdviceTest {
// 定义Before增强处理
public void authority(String aa) {
System.out.println("目标方法的参数为:" + aa);
System.out.println("①号Before增强:模拟执行权限检查");
}
}
三 接口
Hello
package org.crazyit.app.service;
public interface Hello {
// 定义一个简单方法,模拟应用中的业务逻辑方法
void foo();
// 定义一个addUser()方法,模拟应用中的添加用户的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void bar();
}
四 实现类
HelloImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello
{
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void foo()
{
System.out.println("执行Hello组件的foo()方法");
}
// 定义一个addUser()方法,模拟应用中的添加用户的方法
public int addUser(String name , String pass)
{
System.out.println("执行Hello组件的addUser添加用户:" + name);
if(name.length() < 3 || name.length() > 10)
{
throw new IllegalArgumentException("name参数的长度必须大于3,小于10!");
}
return 20;
}
}
WorldImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void bar() {
System.out.println("执行World组件的bar()方法");
}
}
五 测试类
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest {
public static void main(String[] args) {
// 创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = ctx.getBean("hello", Hello.class);
hello.foo();
System.out.println("addUser()的返回值为:" + hello.addUser("孙悟空", "7788"));
World world = ctx.getBean("world", World.class);
world.bar();
}
}
六 测试结果
②Before增强:模拟执行权限检查
②Before增强:被织入增强处理的目标方法为:foo
②Before增强:目标方法的参数为:[]
②Before增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
Around增强:执行目标方法之前,模拟开始事务...
执行Hello组件的foo()方法
Around增强:执行目标方法之后,模拟结束事务...
AfterReturning增强:获取目标方法返回值:null
AfterReturning增强:模拟记录日志功能...
AfterReturning增强:被织入增强处理的目标方法为:foo
AfterReturning增强:目标方法的参数为:[]
AfterReturning增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
After增强:模拟方法结束后的释放资源...
After增强:被织入增强处理的目标方法为:foo
After增强:目标方法的参数为:[]
After增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
②Before增强:模拟执行权限检查
②Before增强:被织入增强处理的目标方法为:addUser
②Before增强:目标方法的参数为:[孙悟空, 7788]
②Before增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
Around增强:执行目标方法之前,模拟开始事务...
执行Hello组件的addUser添加用户:【增加的前缀】孙悟空
Around增强:执行目标方法之后,模拟结束事务...
AfterReturning增强:获取目标方法返回值:400
AfterReturning增强:模拟记录日志功能...
AfterReturning增强:被织入增强处理的目标方法为:addUser
AfterReturning增强:目标方法的参数为:[孙悟空, 7788]
AfterReturning增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
After增强:模拟方法结束后的释放资源...
After增强:被织入增强处理的目标方法为:addUser
After增强:目标方法的参数为:[孙悟空, 7788]
After增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.HelloImpl@5c909414
addUser()的返回值为:400
②Before增强:模拟执行权限检查
②Before增强:被织入增强处理的目标方法为:bar
②Before增强:目标方法的参数为:[]
②Before增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.WorldImpl@4b14c583
Around增强:执行目标方法之前,模拟开始事务...
执行World组件的bar()方法
Around增强:执行目标方法之后,模拟结束事务...
AfterReturning增强:获取目标方法返回值:null
AfterReturning增强:模拟记录日志功能...
AfterReturning增强:被织入增强处理的目标方法为:bar
AfterReturning增强:目标方法的参数为:[]
AfterReturning增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.WorldImpl@4b14c583
After增强:模拟方法结束后的释放资源...
After增强:被织入增强处理的目标方法为:bar
After增强:目标方法的参数为:[]
After增强:被织入增强处理的目标对象为:org.crazyit.app.service.impl.WorldImpl@4b14c583