Spring AOP Advices - After throwing advice example - xml based configuration
来源:互联网 发布:掏耳朵 知乎 编辑:程序博客网 时间:2024/06/06 00:47
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 throwing advice. Spring After throwing advice will be executed after the every exception throw.
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: If you call testThrowException()
method, it will throw an exception. We want to run after throwing advice method.
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 “After Throwing Advice”. Create a class which implements ThrowsAdvice
interface. It will execute after occuring every exception throw.
package com.java2novice.aop;import org.springframework.aop.ThrowsAdvice;public class CatchThrowException implements ThrowsAdvice{ public void afterThrowing(NullPointerException e) throws Throwable { System.out.println("Inside CatchThrowException.afterThrowing() method..."); System.out.println("Running after throwing exception..."); }}
Here is the xml based configuration file. Add bean entry for CatchThrowException
class (after throwing 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="afterThrow" class="com.java2novice.aop.CatchThrowException" /> <bean id="busServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean" > <property name="target" ref="busService" /> <property name="interceptorNames"> <list> <value>afterThrow</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"); try{ busServ.testThrowException(); } catch(Exception ex){ } }}
Output:
Inside CatchThrowException.afterThrowing() method...Running after throwing exception...
- Spring AOP Advices - After throwing advice example - xml based configuration
- Spring AOP Advices - After returning 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 中 advice 的四种类型 before after throwing advice around
- Spring AOP 中 advice 的四种类型 before after throwing advice around
- spring AOP 中 advice 的四种类型 before after throwing advice around
- 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中的After throwing增强问题
- Spring AOP - AspectJ - @After example
- AspectJ之Advice定义之After throwing advice
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- iOS9适配
- HTTP返回码中301与302的区别
- test3.11
- CString ,string,char*转换
- UIView的frame和bounds区别
- Spring AOP Advices - After throwing advice example - xml based configuration
- 韩顺平 java 第二讲 变量 数据类型
- Python生成一定范围内指定数目的无重复随机数
- 马良建模学习笔记——软包制作二
- VS2013 快捷键乱掉如何修改回来
- maven项目使用svn相关问题汇总
- SAT数学细心比知识点更重要【zhasite】
- Remove Element 100%哟
- iOS中的委托和代理模式