spring aop配置及用例说明(2)
2013-12-13 16:37
399 查看
欢迎交流转载:/article/5970599.html
这里先介绍下几个annotation的含义,
@Before:表示在切入点之前执行。
@AfterReturning:表示返回之后执行。
@AfterThrowing:表示在切入点,如果抛出异常就执行这个方法。
@After:表示在找到该方法执行后,它是在切入点方法返回前执行。通常用于释放资源。
接上篇介绍,在使用“AfterReturning建议”时候,如果想要得到返回参数可以这样写:其中retVal是代表返回的参数对象。我这里直接打印出来了toString方法。
同样,如果你希望对方法抛出的异常进行处理的话你也可以去捕获。spring声明式事务管理就是这个原理。
关于pointcut表达式说明:
the execution of any public method:匹配所有public的方法
the execution of any method with a name beginning with "set":匹配一set开头的所有方法
the execution of any method defined by the
the execution of any method defined in the service package:匹配com.xyz.service包下的所有类的方法,不包含子包
the execution of any method defined in the service package or a sub-package:匹配com.xyz.service包以及子包下的所有类的方法,包含子包
any join point (method execution only in Spring AOP) within the service package:匹配在com.xyz.service包下类的所有方法
any join point (method execution only in Spring AOP) within the service package or a sub-package:
any join point (method execution only in Spring AOP) where the proxy implements the
'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
'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
'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
any join point (method execution only in Spring AOP) where the target object has an
'@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
'@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
'@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
'@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 '
any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression '
这里贴上我用于测试的代码:
manager类,相当于service层
切面类:
junit测试类:
这里先介绍下几个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
AccountServiceinterface:匹配类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
AccountServiceinterface:实现
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
AccountServiceinterface:
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
@Transactionalannotation:
@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
@Transactionalannotation:
@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
@Transactionalannotation:
@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
@Classifiedannotation:
@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配置及用例说明(4)
- spring aop配置及用例说明(1)
- LVS-NAT(Virtual Server via NAT)原理说明与配置用例
- spring aop配置说明
- LVS-DR(Virtual Server via Direct Routing)原理说明与配置用例
- spring aop配置及用例说明(3)
- LVS-TUN(Virtual Server via IP Tunneling)原理说明与配置用例
- Nginx配置文件详细说明
- (搜索引擎之solr) schema.xml 配置文件说明
- 用例模板说明
- proxool连接池配置详细说明
- Hibernate ehcache配置二级缓存及说明
- awstats配置说明
- web项目的web.xml配置详细说明
- eclipse tomcat lomboz的安装配置说明
- displaytag中配置说明
- IIS7.0中伪静态配置说明
- php.ini中文件上传功能配置选项说明
- 一般什么时候使用类图,使用到什么程度、领域模型概念、 顺序图说明、协助图、状态图、系统的顺序(用例驱动)、需求分析
- 关于Spring中Commons Validator的使用说明及MVC验证的配置步骤