`

Spring的AOP_XXXXXX

阅读更多

1、相关概念:
1)Aspect(切面):指横切性关注点的抽象即为切面。与类相似,只是两者的关注点不同,类是对物体特征的抽象,而切面是横切性关注点的抽象。
2)Joinpoint(连接点):指一些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点,实际上连接点还可以是field或类构造器。
3)Advice(通知):指拦截到连接点之后所要做的事情,分为前置通知,后置通知,异常通知,最终通知和环绕通知。
4)Pointcut(切入点):指对那些Jointpoint进行拦截的定义。
5)Target(目标对象):代理要实现的目标对象。
6)Weave(织入):指将Aspects应用到Target对象并导致proxy对象创建的过程。
7)Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期间为类动态地添加一些方法或Field.

 

2、使用Spring的注解方式实现AOP
首先要在XML文件中添加以下语句:
<?xml version="1.0" encoding="UTF-8"?>
<beans ……
       …… 
       xmlns:aop="
http://www.springframework.org/schema/aop"     
       ……
      
http://www.springframework.org/schema/aop                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
      
 <aop:aspectj-autoproxy/>
       
 ……

</beans>
然后就可以将类声明为切面,在切面中设置切入点,通知等,如:
@Aspect
//@Aspect声明一个切面  @Component 将切面交给Spring管理

public class MyInterceptor {
	@Pointcut("execution(* com.spring.aop.service.impl.PersonserviceBean.*(..))")
	private void anyMethod(){}//声明一个切入点
	
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name){//设置前置通知
		System.out.println("here is before advice!"+name);
	}
	
	@AfterReturning(pointcut="anyMethod()",returning="result")
	public void doAfterReturning(String result){//设置后置通知
		System.out.println("here is AfterReturning advice! "+result);
	}
	
	@After("anyMethod()")
	public void doAfter(){//设置最终通知
		System.out.println("here is After advice!");
	}
	
	@Around("anyMethod()")//设置环绕通知,必须有下面的参数话和调用proceed()方法
	public Object doAroundAdvice(ProceedingJoinPoint pjp)throws Throwable{
		System.out.println("go into the method!");
		Object result = pjp.proceed();
		System.out.println("drop back the method!");
		return result;	
	}
}

 

3、使用Spring的XML配置文件实现AOP
 只需要将声明为切面的Bean在配置文件中声明,在使用配置文件中的切面设置进行具体配置,如:
<?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:context="
http://www.springframework.org/schema/context"
       xmlns:aop="
http://www.springframework.org/schema/aop"     
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <aop:aspectj-autoproxy/>
      
        <bean id="aspetbean" class="com.fojian.service.MyInterceptor"/><!--将需要做切面的Bean交Spring管理-->
        <aop:config>
         <aop:aspect id="asp" ref="aspetbean"><!--声明切面-->
          <aop:pointcut id="mycut" expression="execution(* com.fojian.service..*.*(..))"/><!--声明切入点-->
          <aop:before pointcut-ref="mycut" method="doAccessCheck"/><!--声明前置通知-->
          <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/><!--声明后置通知-->
   <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/><!--声明例外通知-->
   <aop:after pointcut-ref="mycut" method="doAfter"/><!--声明最终通知-->
   <aop:around pointcut-ref="mycut" method="doBasicProfiling"/><!--声明环绕通知-->
         </aop:aspect>
        </aop:config>
</beans>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics