* AOP操作可与通过注解和xml的方式实现;
* 有个问题,如果高手看到了可以考虑下:采用注解实现,用后置通知的时候,如果后置通知方法名叫after()的话,则先会打印最终通知,后打印后置通知!!!,用xml没有这个问题,很怪啊????
* 采用注解方式,切面也必须交给sping管理;
*配置实例
application.xml------->
注解实现的MyInterceptorAnnotation------->
xml实现的MyInterceptorXML------->
结果------->
这是前置通知
进入环绕通知
这是update方法
这是后置通知
这是最终通知
退出环绕通知
* 有个问题,如果高手看到了可以考虑下:采用注解实现,用后置通知的时候,如果后置通知方法名叫after()的话,则先会打印最终通知,后打印后置通知!!!,用xml没有这个问题,很怪啊????
* 采用注解方式,切面也必须交给sping管理;
*配置实例
application.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="productService" class="com.xx.dao.impl.ProductServiceBean"></bean>
<!-- 注解方式 -->
<!--
<aop:aspectj-autoproxy/>
<bean id="myInterceptor" class="com.xx.aspect.MyInterceptorAnnotation"></bean>
-->
<!-- xml配置方式 -->
<bean id="myInterceptor" class="com.xx.aspect.MyInterceptorXML"></bean>
<aop:config >
<aop:aspect ref="myInterceptor">
<aop:pointcut id="myAop" expression="execution(java.lang.String com.xx.dao.impl.ProductServiceBean.*(..))"/>
<aop:before pointcut-ref="myAop" method="before"/>
<aop:after-throwing pointcut-ref="myAop" method="exception"/>
<aop:after-returning pointcut-ref="myAop" method="afterReturn"/>
<aop:after pointcut-ref="myAop" method="after"/>
<aop:around pointcut-ref="myAop" method="around"/>
</aop:aspect>
</aop:config>
</beans>
注解实现的MyInterceptorAnnotation------->
package com.xx.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
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.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 切面类(annotation配置实现)
* @author user
*
*/
@Aspect
public class MyInterceptorAnnotation {
@Pointcut("execution(* com.xx.dao.impl.ProductServiceBean.*(..))")
private void anyMethod(){}//声明一个切入点
@Before("anyMethod()")
public void before(){
System.out.println("这是前置通知");
}
@AfterReturning("anyMethod()")
public void afterReturn(){
System.out.println("这是后置通知");
}
@After("anyMethod()")
public void after1(){ //如果改为after()的话,则先会打印最终通知,后打印后置通知!!!,用xml没有这个问题,很怪啊。
System.out.println("这是最终通知");
}
@AfterThrowing("anyMethod()")
public void exception(){
System.out.println("这是例外通知");
}
@Around("anyMethod()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入环绕通知");
Object result = pjp.proceed();
System.out.println("退出环绕通知");
return result;
}
}
xml实现的MyInterceptorXML------->
package com.xx.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 切面类(XML配置实现)
* @author user
*
*/
public class MyInterceptorXML {
public void before(){
System.out.println("这是前置通知");
}
public void afterReturn(){
System.out.println("这是后置通知");
}
public void exception(){
System.out.println("这是例外通知");
}
public void after(){
System.out.println("这是最终通知");
}
//环绕通知时固定写法
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入环绕通知");
Object result = pjp.proceed();
System.out.println("退出环绕通知");
return result;
}
}
结果------->
这是前置通知
进入环绕通知
这是update方法
这是后置通知
这是最终通知
退出环绕通知