• 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)

Simple example for Before advice and After Advice in Spring Framework

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

BeforeAdvice and AfterReturningAdvice

This tips presents a very simple program for invoking the Before Advice and After Returning Advice in the Spring Framework. Thsese two methods are part of Spring’s AOP implementation and used as interceptor methods. the following program create spring beans using the standalone java program and invokes the business logic method.
Business logic method is surrounded by Before Advice and After Returning Advice method. It is configured in thespring’s configuration xml file. When ever any client invokes that particular method, the advices will be called to perform a certain operations. Normally these kind of techniques used for printing the debug messages while invoking the methods.

also read:

  • Spring Tutorials
  • Spring 4 Tutorials
  • Spring Interview Questions

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

BeforeAdviceExample.java

package javabeat.net.spring.aop;
import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

/**
 * source : www.javabeat.net
 */
public class BeforeAdviceExample implements MethodBeforeAdvice {
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("Before Advice Called");
    }
}

AfterAdviceExample.java

package javabeat.net.spring.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/**
 * source : www.javabeat.net
 */
public class AfterAdviceExample implements AfterReturningAdvice {
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("After Advice Called");
    }
}

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>theTracingBeforeAdvisor</value>
                <value>theTracingAfterAdvisor</value>
            </list>
        </property>
    </bean>

    <!-- Bean Classes -->
    <bean id="beanTarget" class="javabeat.net.spring.aop.BusinessInterfaceImpl" />

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingBeforeAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="theTracingBeforeAdvice" />
        </property>
        <property name="pattern">
            <value>.*</value>
        </property>
    </bean>

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingAfterAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="theTracingAfterAdvice" />
        </property>
        <property name="pattern">
            <value>.*</value>
        </property>
    </bean>

    <!-- Advice classes -->
    <bean id="theTracingBeforeAdvice"
        class="javabeat.net.spring.aop.BeforeAdviceExample" />
    <bean id="theTracingAfterAdvice"
        class="javabeat.net.spring.aop.AfterAdviceExample" />

</beans>

also read:

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

Category: Spring FrameworkTag: Spring IOC

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: « Editing eclipse classpath file
Next Post: How to write Interception Around Advice in Spring AOP? »

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

EJB 3.0 Timer Services

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