How to write Throws Advice in Spring AOP?
Throws Advice Example
Throws Advice is used when throwing exception from the business methods. This interceptor will be called when there is any exception, so one can do any logic to candle the exception.
For writing the Throws Advice implementation you have to implement ThrowsAdvice interface. This is typed or marker interface. Marker interface doesn’t have any methods. The implementation class will have to implement a method names afterThrowing() which normally takes four parameters. But, passing one parameter is enough.
SpringAopMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| package javabeat.net.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* source : www.javabeat.net
*/
public class SpringAopMain {
public static void main(String[] args) {
// Read the configuration file
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"spring-config.xml");
// Instantiate an object
BusinessInterface businessInterface = (BusinessInterface) ctx.getBean("businesslogicbean");
// Execute the public method of the bean
businessInterface.businessLogicMethod();
}
} |
BusinessInterface.java
1
2
3
4
5
6
7
8
| package javabeat.net.spring.aop;
/**
* source : www.javabeat.net
*/
public interface BusinessInterface {
void businessLogicMethod();
} |
BusinessInterfaceImpl.java
1
2
3
4
5
6
7
8
9
10
11
| package javabeat.net.spring.aop;
/**
* source : www.javabeat.net
*/
public class BusinessInterfaceImpl implements BusinessInterface{
public void businessLogicMethod() {
System.out.println("BusinessLogic Method Called");
throw new RuntimeException();
}
} |
ThrowsAdviceExample.java
1
2
3
4
5
6
7
8
9
10
11
12
| package javabeat.net.spring.aop;
import org.springframework.aop.ThrowsAdvice;
/**
* source : www.javabeat.net
*/
public class ThrowsAdviceExample implements ThrowsAdvice{
public void afterThrowing(RuntimeException runtimeException){
System.out.println("Inside After Throwing");
}
} |
spring-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Bean configuration -->
<bean id="businesslogicbean"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>javabeat.net.spring.aop.BusinessInterface</value>
</property>
<property name="target">
<ref local="beanTarget" />
</property>
<property name="interceptorNames">
<list>
<value>theTracingThrowsAdvisor</value>
</list>
</property>
</bean>
<!-- Bean Classes -->
<bean id="beanTarget" class="javabeat.net.spring.aop.BusinessInterfaceImpl" />
<!-- Advisor pointcut definition for before advice -->
<bean id="theTracingThrowsAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="theTracingThrowsAdvice" />
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>
<!-- Advice classes -->
<bean id="theTracingThrowsAdvice"
class="javabeat.net.spring.aop.ThrowsAdviceExample" />
</beans> |
Comments
comments
July 15, 2008
Spring Framework