Spring AOP+AspectJ注解实例

在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法。
常见AspectJ的注解:
  1. @Before – 方法执行前运行。
  2. @After – 运行在方法返回结果后。
  3. @AfterReturning – 运行在方法返回一个结果后,在拦截器返回结果。
  4. @AfterThrowing – 运行方法在抛出异常后。
  5. @Around – 围绕方法执行运行,结合以上这三个通知。


String AOP所需的jar包

1. 目录结构

看到这个例子的目录结构。

2. Spring Beans

普通 bean 中有几个方法,后来通过 AspectJ 注解拦截。

File:CustomerBo.java

public interface CustomerBo {

	void addCustomer();
	
	String addCustomerReturnValue();
	
	void addCustomerThrowException() throws Exception;
	
	void addCustomerAround(String name);
}

File:CustomerBoImpl.java

public class CustomerBoImpl implements CustomerBo{

	@Override
	public void addCustomer() {
		System.out.println("addCustomer() is running");
	}

	@Override
	public String addCustomerReturnValue() {
		System.out.println("addCustomerReturnValue() is running");
		return "abc";
	}

	@Override
	public void addCustomerThrowException() throws Exception {
		System.out.println("addCustomerThrowException() is running");
		throw new Exception("Error");
	}

	@Override
	public void addCustomerAround(String name) {
		System.out.println("addCustomerAround() is running, args: " + name);
	}

}

3. 启用AspectJ

在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean。

File:applicationContext.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"	
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<aop:aspectj-autoproxy/>
	
	<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
	
	<!-- Aspect -->
	<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
</beans>

4. AspectJ @Before

在下面例子中,logBefore()方法将在 customerBo接口的 addCustomer()方法的执行之前被执行。
AspectJ的“切入点”是用来声明哪种方法将被拦截,应该参考 Spring AOP切入点指南,支持切入点表达式的完整列表。

File : LoggingAspect.java

@Aspect
public class LoggingAspect {

	@Before("execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))")
	public void logBefore(JoinPoint joinPoint) {
		System.out.println("logBefore() is running!");
		System.out.println("getName(): " + joinPoint.getSignature().getName());
		System.out.println("******");
	}
}

执行程序

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerBo customer = (CustomerBo) context.getBean("customerBo");
		customer.addCustomer();
	}
}

执行结果

logBefore() is running!
getName(): addCustomer
******
addCustomer() is running

5. AspectJ @After

在下面例子中,logAfter()方法将在 customerBo 接口的 addCustomer()方法的执行之后执行。

File : LoggingAspect.java

@Aspect
public class LoggingAspect {

	@After("execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))")
	public void logAfter(JoinPoint joinPoint) {
		System.out.println("logAfter() is running!");
		System.out.println("getName(): " + joinPoint.getSignature().getName());
		System.out.println("******");
	}
}
执行结果
addCustomer() is running
logBefore() is running!
getName(): addCustomer
******

6. AspectJ @AfterReturning

在下面例子中,logAfterReturning()方法将在 customerBo 接口的addCustomerReturnValue()方法执行之后执行。此外,还可以截取返回的值使用“returning”属性。

要截取返回的值,对“returning”属性(结果)的值必须用相同的方法参数(结果)。

File : LoggingAspect.java

@Aspect
public class LoggingAspect {

	@AfterReturning(
			pointcut = "execution(* com.ray.customer.bo.CustomerBo.addCustomerReturnValue(..))",
			returning = "result")
	public void logAfterReturning(JoinPoint joinPoint,Object result) {
		System.out.println("logAfterReturning() is running!");
		System.out.println("getName(): " + joinPoint.getSignature().getName());
		System.out.println("Method returned value is : " + result);
		System.out.println("******");
	}
}

执行程序

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerBo customer = (CustomerBo) context.getBean("customerBo");
		customer.addCustomerReturnValue();
	}
}

执行结果

addCustomerReturnValue() is running
logAfterReturning() is running!
getName(): addCustomerReturnValue
Method returned value is : abc
******

7. AspectJ @AfterThrowing

在下面的例子中,如果 customerBo 接口的addCustomerThrowException()方法抛出异常logAfterThrowing()方法将被执行。

File : LoggingAspect.java

@Aspect
public class LoggingAspect {

	@AfterThrowing(
			pointcut = "execution(* com.ray.customer.bo.CustomerBo.addCustomerThrowException(..))",
			throwing = "error")
	public void logAfterThrowing(JoinPoint joinPoint,Throwable error) {
		System.out.println("logAfterThrowing() is running!");
		System.out.println("getName(): " + joinPoint.getSignature().getName());
		System.out.println("Exception : " + error);
		System.out.println("******");
	}
}
执行程序
public class Test {

	public static void main(String[] args) throws Exception {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerBo customer = (CustomerBo) context.getBean("customerBo");
		customer.addCustomerThrowException();
	}
}
执行结果
addCustomerThrowException() is running
logAfterThrowing() is running!
getName(): addCustomerThrowException
Exception : java.lang.Exception: Error
******
Exception in thread "main" java.lang.Exception: Error
	at com.ray.customer.bo.impl.CustomerBoImpl.addCustomerThrowException(CustomerBoImpl.java:21)

8. AspectJ @Around

在下面例子中,logAround()方法将在customerBo接口的addCustomerAround()方法执行之前执行, 必须定义“proceed();” 控制何时拦截器返回控制到原来的addCustomerAround()方法。

File : LoggingAspect.java

@Aspect
public class LoggingAspect {

	@Around("execution(* com.ray.customer.bo.CustomerBo.addCustomerAround(..))")
	public void logAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("logAround() is running!");
		System.out.println("getName(): " + pjp.getSignature().getName());
		System.out.println("getArgs(): " + Arrays.toString(pjp.getArgs()));
		
		System.out.println("Around before is running!");
		pjp.proceed(); //continue on the intercepted method
		System.out.println("Around after is running!");
		
		System.out.println("******");
	}
}

执行程序

public class Test {

	public static void main(String[] args) throws Exception {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerBo customer = (CustomerBo) context.getBean("customerBo");
		customer.addCustomerAround("Ray");
	}
}

执行结果:

logAround() is running!
getName(): addCustomerAround
getArgs(): [Ray]
Around before is running!
addCustomerAround() is running, args: Ray
Around after is running!
******

总结

它总是建议采用最少 AspectJ 注解。这是关于Spring AspectJ 的一篇相当长的文章。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值