java学习

java学习

 

spring中aop 的配置实现

四种通知的执行地方:前置通知
try{
业务代码
后置通知
} catch{
异常通知
} finally{
最终通知
}
1.需要的jar包:aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1.3.jar
2.在配置文件中加入

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

">
<bean id="myXmlAspect" class="com.yjw.aspect.MyXmlAspect"/>
<!-- 配置事务切面-->
<aop:config>
 <aop:aspect ref="myXmlAspect">
  <aop:pointcut expression="execution(* com.yjw.dao..*.*(..))" id="pointcut"/>
  <aop:before method="beforeAdvice" pointcut-ref="pointcut"/>
  <aop:after-returning method="afterReturningAdvice" returning="value" pointcut-ref="pointcut"/>
  <aop:after-throwing method="exceptionAdvice" pointcut-ref="pointcut" throwing="ex"/>
  <aop:after method="afterAdvice" pointcut-ref="pointcut"/>
 </aop:aspect>
</aop:config>
</beans>

 

package com.yjw.aspect;

 

 

public class MyXmlAspect {


 
 //前置通知

 public void  beforeAdvice(){
  System.out.println("前置通知");
 }
 //异常通知,接收异常信息

 public  void  exceptionAdvice(Exception  ex){
  System.out.println("异常出现"+ex.getMessage());
 }
 //后置通知,可以接收方法的返回值
 
 public  void  afterReturningAdvice(Object  value){
  System.out.println("后置通知"+value);
 }
 //最终通知

 public  void  afterAdvice(){
  System.out.println("after");
 }
}

 

posted on 2013-04-29 11:50 杨军威 阅读(235) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜