Spring AOP Advices - Around advice example - xml based configuration
2015-09-17 14:34
288 查看
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 - Around advice. It combines all three advices (before advice, after returning advice and around advice) during method execution.
My business logic service class:
Now create “Around Advice”. Create a class which implements
Here is the xml based configuration file. Add bean entry for
Here is the final demo class: Note that we are calling proxy bean object, not the business service bean directly.
Output:
In this page you will see an example for Spring AOP - Around advice. It combines all three advices (before advice, after returning advice and around advice) during method execution.
pom.xmlfile 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:
package com.java2novice.bean; public class MyBusinessService { public void runMyBusinessLogic(){ System.out.println("************************************"); System.out.println("Running business logic..."); System.out.println("************************************"); } public void testThrowException() { throw new NullPointerException(); } }
Now create “Around Advice”. Create a class which implements
MethodInterceptorinterface. You must call Object
result = metInvocation.proceed()method to proceed on the original method execution, else the original method will not execute.
package com.java2novice.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class ExecuteAroundMethod implements MethodInterceptor{ @Override public Object invoke(MethodInvocation metInvocation) throws Throwable { System.out.println("Inside RunBeforeExecution.before() method..."); System.out.println("Running before advice..."); try{ Object result = metInvocation.proceed(); System.out.println("Inside RunAfterExecution.afterReturning() method..."); System.out.println("Running after advice..."); return result; } catch(NullPointerException ne){ //this is for ThrowsAdvice throw ne; } } }
Here is the xml based configuration file. Add bean entry for
ExecuteAroundMethodclass (around advice class). Also create a proxy for
MyBusinessServiceclass. 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="aroundThrow" class="com.java2novice.aop.ExecuteAroundMethod" /> <bean id="busServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean" > <property name="target" ref="busService" /> <property name="interceptorNames"> <list> <value>aroundThrow</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:
Inside RunBeforeExecution.before() method... Running before advice... ************************************ Running business logic... ************************************ Inside RunAfterExecution.afterReturning() method... Running after advice...
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- java使用动态代理来实现AOP(日志记录)的实例代码
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- JavaScript之AOP编程实例
- Javascript aop(面向切面编程)之around(环绕)分析
- php笔记之:AOP的应用
- 基于Spring框架的Shiro配置方法