Spring学习笔记--------AOP操作

* AOP操作可与通过注解和xml的方式实现;
* 有个问题,如果高手看到了可以考虑下:采用注解实现,用后置通知的时候,如果后置通知方法名叫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方法
这是后置通知
这是最终通知
退出环绕通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值