Spring AOP Advices - After returning advice example - xml based configuration
来源:互联网 发布:家装cad平面设计软件 编辑:程序博客网 时间:2024/06/05 06:10
In Spring, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development.
In this page you will see an example for Spring AOP - After returning advice. Spring After returning advice will be executed after the every method execution.
pom.xml
file gives all required dependencies:
<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>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> </dependencies></project>
My business logic service class: It has only one method runMyBusinessLogic()
contains the business logic, we want to run advice method after every execution of this method.
package com.java2novice.bean;public class MyBusinessService { public void runMyBusinessLogic(){ System.out.println("**************************"); System.out.println("Running business logic..."); System.out.println("**************************"); }}
Now create “After Returning Advice”. Create a class which implements AfterReturningAdvice
interface. It will execute after the method every execution.
package com.java2novice.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class RunAfterExecution implements AfterReturningAdvice{ @Override public void afterReturning(Object retVal, Method methodArg, Object[] args, Object target) throws Throwable { System.out.println("Inside RunAfterExecution.afterReturning() method..."); System.out.println("Running after advice..."); }}
Here is the xml based configuration file. Add bean entry for RunAfterExecution
class (after return advice class). Also create a proxy for MyBusinessService
class. In this proxy configuration, you should add two properties called ‘target
’ and ‘interceptorNames
‘. ‘target
’ defines in which bean you want to introduce advice. ‘interceptorNames
’ defines that which advice class you want to introduce to the said target.
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="busService" class="com.java2novice.bean.MyBusinessService" /> <bean id="afterAdv" class="com.java2novice.aop.RunAfterExecution" /> <bean id="busServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean" > <property name="target" ref="busService" /> <property name="interceptorNames"> <list> <value>afterAdv</value> </list> </property> </bean></beans>
Here is the final demo class: Note that we are calling proxy bean object, not the business service bean directly.
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("busServiceProxy"); busServ.runMyBusinessLogic(); }}
Output:
************************************Running business logic...************************************Inside RunAfterExecution.afterReturning() method...Running after advice...
- Spring AOP Advices - After returning advice example - xml based configuration
- Spring AOP Advices - After throwing advice example - xml based configuration
- Spring AOP Advices - Around advice example - xml based configuration
- Spring AOP Advices - Before advice example - xml based configuration - See more at: http://www.java2
- Spring AOP Example Tutorial – Aspect, Advice, Pointcut, JoinPoint, Annotations, XML Configuration
- Spring AOP + AspectJ in XML configuration example
- Spring AOP + AspectJ in XML configuration example
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- Spring AOP - AspectJ - @After example
- Spring AOP 配置after-returning时异常问题
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- AspectJ之Advice定义之After returning advice
- Spring AOP Advice - Pointcuts – Name match example
- 使用CXF框架学习搭建WebService(三)
- Javascript的setTimeOut和setInterval用法
- 【android学习笔记】 android.os.NetworkOnMainThreadException错误
- Hadoop启动异常的处理方法
- 资源打包后项目中的文件
- Spring AOP Advices - After returning advice example - xml based configuration
- Groovy集合类型
- Unity LOD LODGroup (LOD Bias)
- Android实现系统下拉栏的消息提示——Notification
- 使用json来定义函数,在里面可以定义多个函数
- Easyui Datebox设置可选时间范围
- MVC Kendo总结之-----> Grid
- 潜龙(CHLoong)可视化图谱
- Android Scroller类的详细分析