Spring AOP Logging.

Error Logging for a particular class that is a Spring Bean can be logged as follow :

<bean id="FooBeanProxy" class="foo.FooBeanImpl">
     <property name="studentMasterDao" ref="StudentMasterDao" /> </bean>
<!--Now above proxy bean is to be used in Logging.-->
<bean id="FooBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
        <property name="target">
            <ref local="FooBeanProxy"/>
        <property name="interceptorNames">
    <bean id="loggingInterceptor" class="foo.LoggingInterceptor"/>

Interceptor class

package foo;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoggingInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        Log log = LogFactory.getLog(invocation.getMethod().getDeclaringClass());
        log.info("before method invocation : " + invocation.getMethod().getName());
        Object retVal = null;
        try {
            retVal = invocation.proceed();
        } catch (Exception e) {
            log.error("error while invoking method : " + invocation.getMethod().getName(), e);
            throw e;
        log.info("after method invocation : " + invocation.getMethod().getName());
        return retVal;

This entry was posted in Log4j, Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s