基于注解的Spring AOP的配置和使用

AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。

我们现在做的一些非业务,如:日志、事务、安全等都会写在业务代码中(也即是说,这些非业务类横切于业务类),但这些代码往往是重复,复制——粘贴式的代码会给程序的维护带来不便,AOP就实现了把这些业务需求与系统需求分开来做。这种解决的方式也称代理机制。

先来了解一下AOP的相关概念,《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,UserService.get的调用或者UserService.delete抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如ServiceAspect。
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定。
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true。

通知(Advice)类型:

  • 前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法。
  • 后置通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,ServiceAspect中的returnAfter方法,所以Teser中调用UserService.delete抛出异常时,returnAfter方法仍然执行。
  • 返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,ServiceAspect中的around方法。
  • 抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,ServiceAspect中的returnThrow方法。

注:可以将多个通知应用到一个目标对象上,即可以将多个切面织入到同一目标对象。

使用Spring AOP可以基于两种方式,一种是比较方便和强大的注解方式,另一种则是中规中矩的xml配置方式。

先说注解,使用注解配置Spring AOP总体分为两步,第一步是在xml文件中声明激活自动扫描组件功能,同时激活自动代理功能(同时在xml中添加一个UserService的普通服务层组件,来测试AOP的注解功能):

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns=http://www.springframework.org/schema/beans
	xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
	xmlns:context=http://www.springframework.org/schema/context
	xmlns:aop=http://www.springframework.org/schema/aop
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		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-3.0.xsd">

	<!-- 激活组件扫描功能,在包及其子包下面自动扫描通过注解配置的组件 -->
	<context:component-scan base-package="com.jaeson"/>
	<!-- 激活自动代理功能 -->
	<aop:aspectj-autoproxy proxy-target-class="true"/></beans>

第二步是为Aspect切面类添加注解:

package com.jaeson.springstudy.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopExample {
 
 private static final Logger logger = LoggerFactory.getLogger(AopExample.class);
 @Pointcut("execution(* com.jaeson..dao..*.*(..))")
    public void dataAccessOperation() {}
 
    @Pointcut("execution(* com.jaeson..service..*.*(..))")
    public void businessService() {}
    
    @Pointcut("execution(* com.jaeson.springstudy.aop.*Service.*(..)))")
    public void aspect() {}
    /*
  * 配置前置通知,使用在方法businessService()上注册的切入点
  * 同时接受JoinPoint切入点对象,可以没有该参数
  */
 @Before("businessService()")
 public void before(JoinPoint joinPoint) {
  
  logger.info("before {}", joinPoint);
 }
 //配置后置通知,使用在方法businessService()上注册的切入点
 @After("businessService()")
 public void after(JoinPoint joinPoint) {
   logger.info("after {}", joinPoint);
 }
 //配置环绕通知,使用在方法dataAccessOperation()上注册的切入点
 //@Around的返回类型必须为Object,否则在织入非void返回类型的切入点时会抛出异常:
 //Null return value from advice does not match primitive return type for:
 @Around("dataAccessOperation()")
 public Object around(JoinPoint joinPoint) throws Throwable {
  
  Object result = null;
  long start = System.currentTimeMillis();
  
  logger.info("begin around {} !", joinPoint);
  result = ((ProceedingJoinPoint) joinPoint).proceed();
  long end = System.currentTimeMillis();
  logger.info("end around {} Use time : {} ms!", joinPoint, (end - start));
  
  return result;
 }
 //配置后置返回通知,使用在方法businessService()上注册的切入点
 @AfterReturning("businessService()")
 public void afterReturn(JoinPoint joinPoint) {
  logger.info("afterReturn {}", joinPoint);
 }
 //配置抛出异常后通知,使用在方法businessService()上注册的切入点
 @AfterThrowing(pointcut="businessService()", throwing="ex")
 public void afterThrow(JoinPoint joinPoint, RuntimeException ex) {
  
  logger.info("afterThrow {} with exception : {}", joinPoint, ex.getMessage());
 }
 
 //配置前置通知,拦截返回值类型为com.jaeson.hibernatestudy.bean.User的方法
 @Before("execution(com.jaeson.hibernatestudy.bean.User com.jaeson.springstudy.aop.*Service.*(..))")
 public void beforeReturnUser(JoinPoint joinPoint) {
  logger.info("beforeReturnUser {}", joinPoint);
 }
 //配置前置通知,拦截参数类型为com.jaeson.hibernatestudy.bean.User的方法
 @Before("execution(* com.jaeson.springstudy.aop.*Service.*(com.jaeson.hibernatestudy.bean.User))")
 public void beforeArgUser(JoinPoint joinPoint) {
  logger.info("beforeArgUser {}", joinPoint);
 }
 //配置前置通知,拦截含有long类型参数的方法,并将参数值注入到当前方法的形参id中
 @Before("aspect() && args(id)")
 public void beforeArgId(JoinPoint joinPoint, long id) {
  logger.info("beforeArgId {} ({})", joinPoint, id);
 }
}

测试代码:

package com.jaeson.springstudy.aop;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jaeson.hibernatestudy.bean.User;
import com.jaeson.springstudy.aop.AopService;
public class TestAop {
 private ClassPathXmlApplicationContext context;
 
 @Before
 public void before() {
  context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "classpath:spring-hibernate.xml", "spring-mybatis.xml"});
 }
 
 @After
 public void after() {
  context.close();
 }
 
 @Test
 public void testMethodAop() {
  
  AopService service = context.getBean("aopService", AopService.class);
  service.get(100086L);
  service.save(new User());
  try {
   service.delete(95977L);
  } catch (RuntimeException ex) {
   System.out.println(ex.getMessage());
  }
 }
}

控制台输出如下:

 

    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopExample.beforeArgId(AopExample.java:95) beforeArgId execution(User com.jaeson.springstudy.aop.AopService.get(long)) (100086) 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopExample.beforeReturnUser(AopExample.java:81) beforeReturnUser execution(User com.jaeson.springstudy.aop.AopService.get(long)) 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopService.get(AopService.java:16) AopService.get() method . . . 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopExample.beforeArgUser(AopExample.java:88) beforeArgUser execution(void com.jaeson.springstudy.aop.AopService.save(User)) 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopService.save(AopService.java:22) AopService.save() method . . . 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopExample.beforeArgId(AopExample.java:95) beforeArgId execution(void com.jaeson.springstudy.aop.AopService.delete(long)) (95977) 
    [INFO][2016-04-26 15:56:06] com.jaeson.springstudy.aop.AopService.delete(AopService.java:27) AopService.delete() method . . . 
    AopService.delete() throw UnsupportedOperationException

 

可以看到,正如我们预期的那样,虽然我们并没有对UserSerivce类包括其调用方式做任何改变,但是Spring仍然拦截到了其中方法的调用,或许这正是AOP的魔力所在。

再简单说一下xml配置方式,其实也一样简单:

 

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns=http://www.springframework.org/schema/beans
	xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
	xmlns:context=http://www.springframework.org/schema/context
	xmlns:aop=http://www.springframework.org/schema/aop
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		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-3.0.xsd">

	<!-- 系统服务组件的切面Bean -->
	<bean id="serviceAspect" class="com.jaeson.springstudy.aop.AopExample" />
	<!-- AOP配置 -->
	<aop:config>
		<!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
		<aop:aspect id="simpleAspect" ref="serviceAspect">
			<!-- 配置一个切入点,相当于@Pointcut -->
			<aop:pointcut expression="execution(* com.jaeson.springstudy.aop.service..*(..))" id="simplePointcut" />
			<!-- 配置通知,相当于@Before、@After、@AfterReturn、@Around、@AfterThrowing -->
			<aop:before pointcut-ref="simplePointcut" method="before" />
			<aop:after pointcut-ref="simplePointcut" method="after" />
			<aop:after-returning pointcut-ref="simplePointcut" method="afterReturn" />
			<aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex" />
		</aop:aspect>
	</aop:config>
</beans>

 

 

 AopService 的代码(其实很简单):

 

package com.jaeson.springstudy.aop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.jaeson.hibernatestudy.bean.User;
@Service
public class AopService {
 
 private static final Logger logger = LoggerFactory.getLogger(AopService.class);
 
 public User get(long id) {
  logger.info("AopService.get() method . . .");
  return new User();
 }
 public void save(User user) {
  
  logger.info("AopService.save() method . . .");
 }
 public void delete(long id) throws RuntimeException {
  logger.info("AopService.delete() method . . .");
  throw new UnsupportedOperationException("AopService.delete() throw UnsupportedOperationException");
 }
}

 

应该说学习Spring AOP有两个难点,第一点在于理解AOP的理念和相关概念,第二点在于灵活掌握和使用切入点表达式。概念的理解通常不在一朝一夕,慢慢浸泡的时间长了,自然就明白了,下面我们简单地介绍一下切入点表达式的配置规则吧。

通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下:

 

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

 

  • modifiers-pattern:方法的操作权限
  • ret-type-pattern:返回值
  • declaring-type-pattern:方法所在的包
  • name-pattern:方法名
  • parm-pattern:参数名
  • throws-pattern:异常

其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。

最后说一下通知参数

可以通过args来绑定参数,这样就可以在通知(Advice)中访问具体参数了。例如,<aop:aspect>配置如下:

 

<aop:config>
	<aop:aspect id="TestAspect" ref="aspectBean">
		<aop:pointcut id="businessService" expression="execution(* com.jaeson.springstudy.service.*.*(String,..)) and args(msg,..)" />
		<aop:after pointcut-ref="businessService" method="doAfter" />
	</aop:aspect>
</aop:config>

 

上面的代码args(msg,..)是指将切入点方法上的第一个String类型参数添加到参数名为msg的通知的入参上,这样就可以直接使用该参数啦。

访问当前的连接点

在上面的Aspect切面Bean中已经看到了,每个通知方法第一个参数都是JoinPoint。其实,在Spring中,任何通知(Advice)方法都可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型用以接受当前连接点对象。JoinPoint接口提供了一系列有用的方法, 比如 getArgs() (返回方法参数)、getThis() (返回代理对象)、getTarget() (返回目标)、getSignature() (返回正在被通知的方法相关信息)和 toString() (打印出正在被通知的方法的有用信息)。

猜你喜欢

转载自jaesonchen.iteye.com/blog/2294002