注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

清风幻影的博客

Where there is love, I will be there.

 
 
 

日志

 
 
关于我

同是天涯沦落人,相逢何必曾相识. 天生我材必有用,千金散尽还复来. 天若有情天亦老,人间正道是沧桑. 月影西斜人已去, 堤上梅花情依旧, 此情故已成追忆, 美人如玉夜留香

网易考拉推荐

Spring AOP 样例  

2013-05-20 09:20:39|  分类: spring_1资料 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
http://joe5456536.blog.163.com/blog/static/85374773201121164219558/
applicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
              <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
             <bean id="myAspect" class="com.demo.spring.aop.MyAspect" />
             <bean id="helloWorld" class="com.demo.spring.test.HelloWorld"/>
</beans>

声明一个切面类 :

package com.demo.spring.aop;

import org.aspectj.lang.JoinPoint;
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;

@Aspect
public class MyAspect {

       @Pointcut("execution(* execute(. .)) && target(com.demo.spring.test.HelloWorld)" )   //切入点函数
        public void pointcut(){
        }


       @Before("pointcut()")  //前置通知 (Before Advice)
        public void beforeExecute(JoinPoint thisJoinPoint){

                       System.out.println("连接点类型:" + thisJoinPoint.getKind());
                       System.out.println("代理类名:" + thisJoinPoint.getSourceLocation().getClass().getName());
                       System.out.println("目标类名:" + thisJoinPoint.getTarget().getClass().getName());
                       System.out.println("目标函数名:" + thisJoinPoint.getSignature().getName());
                       System.out.println("参数个数:" + thisJoinPoint.getArgs().length);

                       System.out.println("Before Advice!");
        }


        @AfterReturning(pointcut = "pointcut()", returning = "retVal")  //返回后通知(After returning Advice)
        public void afterExecuet(Object retVal){
                     System.out.println("返回值为:" + retVal);
        }


        @AfterThrowing(pointcut = "pointcut()", throwing = "ex")  //抛出异常后通知(After Throwing Advice)
        public void afterThrowing(Exception ex){
                    System.out.println("Throwing "+ex);
        }


        @After("pointcut() && args(arg, . .)")  //后通知 (After Advice)
        public void afterExecute(Obecjt arg){

                   System.out.print("参数值为:" + args);
                   System.out.print("After Advice!");
        }


        @Around("pointcut()")   //环绕通知(Around Advice)
        public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
                   System.out.println("Around Advice!");
                    return thisJoinPoint.proceed();
         }
}

定义一个目标函数:

package com.demo.spring.test;

import org.springframework.context.ApplicationContext;

public class HelloWorld {

          public String execute(String message){
                 System.out.println("Hello " + message + "!");

                 return "Goodbye " + message;
           }

           public static void main(String[] args){  
                 //ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
                 ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
                 HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
                 helloWorld.execute("World");
            }
}

  评论这张
 
阅读(541)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018