Spring AOP + AspectJ in XML configuration example

In this tutorial, we show you how to convert last Spring AOP + AspectJ annotation into XML based configuration.

For those don’t like annotation or using JDK 1.4, you can use AspectJ in XML based instead.

Review last customerBo interface again, with few methods, later you will learn how to intercept it via AspectJ in XML file.


package com.mkyong.customer.bo;

public interface CustomerBo {

	void addCustomer();
	
	String addCustomerReturnValue();
	
	void addCustomerThrowException() throws Exception;
	
	void addCustomerAround(String name);
}

1. AspectJ <aop:before> = @Before

AspectJ @Before example.


package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

	@Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
	public void logBefore(JoinPoint joinPoint) {
		//...
	}

}

Equivalent functionality in XML, with <aop:before>.


<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

     <!-- @Before -->
     <aop:pointcut id="pointCutBefore"
	expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

     <aop:before method="logBefore" pointcut-ref="pointCutBefore" />
			
  </aop:aspect>

</aop:config>

2. AspectJ <aop:after> = @After

AspectJ @After example.


package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;

@Aspect
public class LoggingAspect {

	@After("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
	public void logAfter(JoinPoint joinPoint) {
		//...
	}

}

Equivalent functionality in XML, with <aop:after>.


<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

     <!-- @After -->
     <aop:pointcut id="pointCutAfter"
	expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

     <aop:after method="logAfter" pointcut-ref="pointCutAfter" />
			
  </aop:aspect>

</aop:config>

3. AspectJ <aop:after-returning> = @AfterReturning

AspectJ @AfterReturning example.


package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
public class LoggingAspect {

  @AfterReturning(
   pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))",
   returning= "result")
   public void logAfterReturning(JoinPoint joinPoint, Object result) {
	//...
   }

}

Equivalent functionality in XML, with <aop:after-returning>.


<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning" returning="result" 
      pointcut-ref="pointCutAfterReturning" />
			
  </aop:aspect>

</aop:config>

4. AspectJ <aop:after-throwing> = @AfterReturning

AspectJ @AfterReturning example.


package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
public class LoggingAspect {

  @AfterThrowing(
   pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))",
   throwing= "error")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
	//...
  }
}

Equivalent functionality in XML, with <aop:after-throwing>.


<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" />
			
    <aop:after-throwing method="logAfterThrowing" throwing="error" 
      pointcut-ref="pointCutAfterThrowing"  />
			
  </aop:aspect>

</aop:config>

5. AspectJ <aop:after-around> = @Around

AspectJ @Around example.


package com.mkyong.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;

@Aspect
public class LoggingAspect {

	@Around("execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))")
	public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
		//...
	}
	
}

Equivalent functionality in XML, with <aop:after-around>.


<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

   <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @Around -->
   <aop:pointcut id="pointCutAround"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" />
			
   <aop:around method="logAround" pointcut-ref="pointCutAround"  />
			
  </aop:aspect>

</aop:config>

Full XML example

See complete AspectJ XML based configuration file.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect">

    <!-- @Before -->
    <aop:pointcut id="pointCutBefore"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:before method="logBefore" pointcut-ref="pointCutBefore" />

    <!-- @After -->
    <aop:pointcut id="pointCutAfter"
       expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:after method="logAfter" pointcut-ref="pointCutAfter" />

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
       expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning"
      returning="result" pointcut-ref="pointCutAfterReturning" />

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" />

    <aop:after-throwing method="logAfterThrowing"
      throwing="error" pointcut-ref="pointCutAfterThrowing" />

    <!-- @Around -->
    <aop:pointcut id="pointCutAround"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" />

    <aop:around method="logAround" pointcut-ref="pointCutAround" />

  </aop:aspect>

</aop:config>

</beans>

Download Source Code

Download it – Spring3-AOP-AspectJ-XML-Example.zip (8 KB)

References

  1. Spring AOP + AspectJ annotation example
  2. AspectJ programming guide
  3. Spring AOP + AspectJ reference

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Jun Lew
Guest
Jun Lew

Hi all,
I got an exception when I covert it like above example.
The error msg:

“`
Parser configuration exception parsing XML from class path resource [Spring-aspectj-xml.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@15e83f9] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
“`
then I fix it by stackoverflow, first, add xerces in pom.xml, and add xml-apis in pom.xml, at last, I still got a ex `AnnotationAwareAspectJAutoProxyCreator is only available on Java 1.5 and higher`

env:
JDK 1.4

Anil Mishra
Guest
Anil Mishra

I’m getting below Exception while trying to implement it in my sample application.

Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [Spring-Cofiguration.xml]; nested exception is java.lang.NoClassDefFoundError: org.aspectj.lang.JoinPoint

Ashwani Rawat
Guest
Ashwani Rawat

Anil,

please add aspectjrt-1.0.5.jar (http://mirrors.ibiblio.org/pub/mirrors/maven2/aspectj/aspectjrt/1.0.5/aspectjrt-1.0.5.jar) dependency in your project.

Regards
Ashwani Rawat

trackback
Programación Orientada a Aspectos (AOP) en Spring | csilgoortiz

[…] diferentes formas de trabajar con aspectos en Spring AOP  (mediante XML, mediante anotaciones AspectJ, utilizando configuración por Java, utilizando configuración por […]

surya
Guest
surya

Hello,

Thanks for the article. I have a question. In case a method is a init-method on spring application context, how do I make the advise work on the same?

Thanks & Regards
Surya

Chinthu
Guest
Chinthu

Very nice (Not only this, all ur posts). Like u very much.

trackback
Spring AOP + AspectJ annotation example

[…] It’s always recommended to apply the least power AsjectJ annotation. It’s rather long article about AspectJ in Spring. for further explanations and examples, please visit the reference links below. Anti annotation or using JDK 1.4 ? No worry, AspectJ supported XML configuration also, read this Spring AOP + AspectJ XML example. […]

krunal
Guest
krunal

Very help full example and good post..

Deepak
Guest
Deepak

Very Good example

Simple and clear….Great

Matthew
Guest
Matthew

BTW, do you have a FB publishing page?

Matthew
Guest
Matthew
Your example are very detailed and even have Compilable source code download. That is good, it also provide other external information into. Perhaps adding these would make them great: 1) When some of the methods should be use and not use as advise would really help in understanding the purpose of certain feature. 2) Advantages and also disadvantages of certain coding approaches. I am using this page as an example: Like Annotation vs XML config. Annotation = targeted to specific class during compile time but not runtime. Better during code refactoring. XML Config = Configurable during runtime but perhaps code… Read more »
Chaithanya
Guest
Chaithanya
Hi Mk Young, I’ve always been a good follower of your examples in my projects.. thanks for great work, i follwed this example using aspectj and xml configuring of aop, i dont know why my logging aspect is calling twice for instance, first time it is showing $Proxy186 and second time it is giving fine, please see below log lines…i need only once of calling advice, can you advise of what may be the issue here..I stuck up with this in my dev project…please help [2011-12-19 10:08:32,458] [INFO] [xyz…LoggingAspect] [ Before Method: getApprovableEmployeeUserProfilesInfo of Class: $Proxy186] [2011-12-19 10:08:32,458] [INFO] [xyz…LoggingAspect]… Read more »