• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to write Throws Advice in Spring AOP?

July 15, 2008 //  by Krishna Srinivasan//  Leave a Comment

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.

also read:

  • Spring Tutorials
  • Spring 4 Tutorials
  • Spring Interview Questions

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

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

package javabeat.net.spring.aop;

/**
 * source : www.javabeat.net
 */
public interface BusinessInterface {
    void businessLogicMethod();
}

BusinessInterfaceImpl.java

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

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");
    }
}

also read:

  • Spring Books
  • Introduction to Spring Framework
  • Introduction to Spring MVC Framework

spring-config.xml

<?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>

Category: Spring FrameworkTag: Spring AOP

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to write Interception Around Advice in Spring AOP?
Next Post: a4j:support – Simple Example on onkeyup event »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact