欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html

这里先介绍下几个annotation的含义,

  • @Before:表示在切入点之前执行。
  • @AfterReturning:表示返回之后执行。
  • @AfterThrowing:表示在切入点,如果抛出异常就执行这个方法。
  • @After:表示在找到该方法执行后,它是在切入点方法返回前执行。通常用于释放资源。

接上篇介绍,在使用“AfterReturning建议”时候,如果想要得到返回参数可以这样写:其中retVal是代表返回的参数对象。我这里直接打印出来了toString方法。

 @AfterReturning(pointcut="execution(public * com.bing.test..*.*(..))",returning="retVal")
     public void afterReturning(Object retVal) {
         if(retVal!=null)
         System.out.println("参数是:"+retVal);
         System.out.println("afterReturning Method");
     }

同样,如果你希望对方法抛出的异常进行处理的话你也可以去捕获。spring声明式事务管理就是这个原理。

 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.AfterThrowing;

 @Aspect
 public class AfterThrowingExample {

   @AfterThrowing(
     pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
     throwing="ex")
   public void doRecoveryActions(DataAccessException ex) {
     // ...
   }

 }

关于pointcut表达式说明:

  • the execution of any public method:匹配所有public的方法

    execution(public * *(..))
  • the execution of any method with a name beginning with "set":匹配一set开头的所有方法

    execution(* set*(..))
  • the execution of any method defined by the AccountService interface:匹配类com.xyz.service.AccountService下所有方法

    execution(* com.xyz.service.AccountService.*(..))
  • the execution of any method defined in the service package:匹配com.xyz.service包下的所有类的方法,不包含子包

    execution(* com.xyz.service.*.*(..))
  • the execution of any method defined in the service package or a sub-package:匹配com.xyz.service包以及子包下的所有类的方法,包含子包

    execution(* com.xyz.service..*.*(..))
  • any join point (method execution only in Spring AOP) within the service package:匹配在com.xyz.service包下类的所有方法

    within(com.xyz.service.*)
  • any join point (method execution only in Spring AOP) within the service package or a sub-package:

    within(com.xyz.service..*)
  • any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:实现AccountService接口的所有类

    this(com.xyz.service.AccountService)

    'this' is more commonly used in a binding form :- see the following section on advice for how to make the proxy object available in the advice body.

  • any join point (method execution only in Spring AOP) where the target object implements the AccountService interface:

    target(com.xyz.service.AccountService)

    'target' is more commonly used in a binding form :- see the following section on advice for how to make the target object available in the advice body.

  • any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable:

    args(java.io.Serializable)

    'args' is more commonly used in a binding form :- see the following section on advice for how to make the method arguments available in the advice body.

    Note that the pointcut given in this example is different to execution(* *(java.io.Serializable)): the args version matches if the argument passed at runtime is Serializable, the execution version matches if the method signature declares a single parameter of type Serializable.

  • any join point (method execution only in Spring AOP) where the target object has an @Transactional annotation:

    @target(org.springframework.transaction.annotation.Transactional)

    '@target' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

  • any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation:

    @within(org.springframework.transaction.annotation.Transactional)

    '@within' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

  • any join point (method execution only in Spring AOP) where the executing method has an @Transactional annotation:

    @annotation(org.springframework.transaction.annotation.Transactional)

    '@annotation' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

  • any join point (method execution only in Spring AOP) which takes a single parameter, and where the runtime type of the argument passed has the @Classified annotation:

    @args(com.xyz.security.Classified)

    '@args' can also be used in a binding form :- see the following section on advice for how to make the annotation object(s) available in the advice body.

  • any join point (method execution only in Spring AOP) on a Spring bean named 'tradeService':

    bean(tradeService)
  • any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression '*Service':

    bean(*Service)

这里贴上我用于测试的代码:

manager类,相当于service层

 package com.bing.test;

 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;

 @Component("manager")
 public class Manager {
     @Value("${user.name}")
     private String myName;
     @Value("${user.description}")
     private String description;

     public void sayHello() {
         System.out.println("Hello " + myName);
     }

     public void getDes() {
         System.out.println(description);

     }
     public String getName(){
         System.out.println("执行getName");
         return myName;
     }
     public String throwTest() throws Exception {
             if (true) {

                 throw new Exception("new throwing test!");
             }
         return "sdf";
     }
 }

切面类:

 package com.bing.test;

 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.AfterReturning;
 import org.aspectj.lang.annotation.AfterThrowing;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;
 import org.springframework.stereotype.Component;

 @Aspect
 // 定义切面类
 @Component
 // 把类装载到容器,与@service等作用一样
 public class NotVeryUsefulAspect {
     //配置切入点集合,这样在下面可以直接引入
      @Pointcut("execution(public * com.bing.test..*.sayHello(..))")
       public void inManager() {}
      @Pointcut("within(com.bing.test..*)")
      public void excutionManager() {}
     // 表示在方法前面执行
     @Before("com.bing.test.NotVeryUsefulAspect.inManager()")
     public void before() {

         System.out.println("before Method");
     }
     @AfterReturning(pointcut="execution(public * com.bing.test..*.*(..))",returning="retVal")
     public void afterReturning(Object retVal) {
         if(retVal!=null)
         System.out.println("参数是:"+retVal);
         System.out.println("afterReturning Method");
     }
     //@After("execution(public * com.bing.test..*.*(..))")
     @After("within(com.bing.test.Manager)")
     public void after() {

         System.out.println("after Method");
     }
     @AfterThrowing(pointcut="execution(* com.bing.test.*.throwTest(..))",throwing="ex")
     public void afterThrow(Exception ex){

         System.out.println(ex.getMessage());

         System.out.println("AfterThrowing Method!");
     }
 }

junit测试类:

 package com.bing.jtest;

 import javax.annotation.Resource;

 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 import com.bing.test.Manager;

 @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
 @RunWith(SpringJUnit4ClassRunner.class)
 public class Testr {

     @Resource(name="manager")
     private Manager manager;

     @Test
     public void test() {
         manager.sayHello();
         //manager.getDes();
     }
     @Test
     public void TestAfterReturning(){

         manager.getName();
     }
     @Test
     public void TestAfterThrow(){

             try {
                 manager.throwTest();
             } catch (Exception e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }

     }
 }

spring aop配置及用例说明(2)的更多相关文章

  1. spring aop配置及用例说明(1)

    欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...

  2. spring aop配置及用例说明(4)

    欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3476161.html 这里简单对xml的配置方式做一下描述.代码还是上一篇(http://www.cnblo ...

  3. spring aop配置及用例说明(3)

    欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...

  4. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  5. Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理

    一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...

  6. spring aop配置文档部分翻译

    欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...

  7. Spring——AOP配置时的jar包异常

    首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...

  8. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  9. Spring学习笔记之二----基于XML的Spring AOP配置

    在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...

随机推荐

  1. C语言 &#183; 出现次数最多的数

    问题描述 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20.然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来.如果有两 ...

  2. Android 源码获取-----在Windows环境下通过Git得到Android源代码

    在学习Android的过程中,深入其源代码研究对我们来说是非常重要的,这里将介绍如何通过在Windows环境下使用Git来得到我们的Android源代码. 1.首先确保你电脑上安装了Git,这个通过  ...

  3. jlink安装

    https://www.segger.com/jlink-software.html?step=1&file=JLink_510p

  4. 【转载】GBDT(MART) 迭代决策树入门教程 | 简介

    转载地址:http://blog.csdn.net/w28971023/article/details/8240756 GBDT(Gradient Boosting Decision Tree) 又叫 ...

  5. 【转】tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  6. Android多语言支持以及各国语言Values文件夹命名规则

    创建好的项目工程由于需求 需要做多国语言的支持  下面介绍怎么快捷的创建文件夹 建好一个android 的项目后,默认的res下面 有layout.values.drawable等目录 这些都是程序默 ...

  7. 修改、设置root密码

    参考文档: http://zhidao.baidu.com/link?url=OaUTAj6FrMGDjbPZHWv3NNDOaIl3HNqZz_3lF_Zpi8oZpLkBfnHfPlpgE1EvN ...

  8. ubuntu下python flask环境搭建

    ubuntu下python flask环境搭建 1. 安装pip sudo apt-get install python-dev pyhton-pip 2. 安装virtualenv sudo apt ...

  9. sql中常见日期获取

    获取当前年月日 --获取当前月份 ,GETDATE())) --获取当前月份的下个月 ,GETDATE())) --获取当前月份的上个月 year()获取年 select year(GETDATE() ...

  10. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...