Spring AOP - AspectJ - @AfterReturning example
2015-09-17 14:54
706 查看
Last few pages talked about spring advices (before advice, after return advice, around advice and pointcut examples). In this page you will get an idea about how to integrate AspectJ annotations with Spring AOP framework. Using spring AOP and AspectJ, you can easily intercept methods.
AspectJ comes with below annotations:
1)
2)
3)
4)
5)
In this page we will see an example for
My business logic service class:
Here is the Aspect class example. The
Here is the xml based configuration file. By adding “
Here is the final demo class:
Output:
AspectJ comes with below annotations:
1)
@Before
2)
@After
3)
@AfterReturning
4)
@AfterThrowing
5)
@Around
In this page we will see an example for
@AfterReturningannotation.
@AfterReturningannotation intercepts method after calling it. You have seen similar behavior with
@Afterannotation in the previous example, but
@AfterReturningwill intercept the return value as well.
pom.xmlfile gives all required dependencies: Add dependencies for
spring-aopjar,
aspectjrtjar and
aspectjweaverjar file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringJavaBasedConfig</groupId> <artifactId>SpringJavaBasedConfig</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> </dependencies> </project>
My business logic service class:
package com.java2novice.bean; public class MyBusinessService { public String runMyBusinessLogic(){ System.out.println("************************************"); System.out.println("Running business logic..."); System.out.println("************************************"); return "Successfully executed my business logic"; } public void testThrowException() { throw new NullPointerException(); } }
Here is the Aspect class example. The
after()method will be executed after calling the method. Note here you have specified “
returning” attribute as part of your annotation. Make sure that the annotation returning attribute value should be same as method input parameter.
package com.java2novice.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; @Aspect public class RunAfterExecution { @AfterReturning( pointcut="execution(* com.java2novice.bean.MyBusinessService.runMyBusinessLogic(..))", returning="returnValue") public void runAfter(JoinPoint joinPoint, String returnValue) throws Throwable { System.out.println("Inside RunAfterExecution.afterReturning() method..."); System.out.println("inserted after : " + joinPoint.getSignature().getName()); System.out.println("Method returned value is : " + returnValue); } }
Here is the xml based configuration file. By adding “
<aop:aspectj-autoproxy />” tag, you can enable AspectJ with in your application. Add bean definitions for your normal bean and your Aspect.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <aop:aspectj-autoproxy /> <bean id="busService" class="com.java2novice.bean.MyBusinessService" /> <bean id="beforeAspectBean" class="com.java2novice.aop.RunAfterExecution" /> </beans>
Here is the final demo class:
package com.java2novice.test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java2novice.bean.MyBusinessService; public class SpringDemo { public static void main(String a[]){ String confFile = "applicationContext.xml"; ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(confFile); MyBusinessService busServ = (MyBusinessService) context.getBean("busService"); busServ.runMyBusinessLogic(); } }
Output:
************************************ Running business logic... ************************************ Inside RunAfterExecution.afterReturning() method... inserted after : runMyBusinessLogic Method returned value is : Successfully executed business logic
相关文章推荐
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- java使用动态代理来实现AOP(日志记录)的实例代码
- JavaScript之AOP编程实例
- Javascript aop(面向切面编程)之around(环绕)分析
- php笔记之:AOP的应用
- 实例讲解Java的Spring框架中的AOP实现
- autoload-cache-1.7 发布
- Spring3 AOP配置异常。执行时间记录
- AOP切面编程
- spring 切面memcached的环绕通知
- 关于HouseMD
- Spring AOP介绍及源码分析
- 基于 Java 语言的极速 WEB + ORM 框架 JFinal 1.6 发布
- SpringAOP实现自动生成日志
- Spring Aspect Oriented Programming
- 30、最简单的mvc框架tiny,增加Aop
- 31、最简单的mvc框架tiny,增加Ioc,jdbc工具类(1个类),连接池(1个类)