HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring AOP / Spring AOP XML Configuration Example

Spring AOP + AspectJ XML Configuration Example

If you are one of rare developers who are still struck on JDK 1.4 OR you are maintaining a legacy spring application where AOP code has been written in XML configuration files, this post is for you. Learn here to define and use spring aop with aspectj using xml based configuration.

Read More : Spring AOP AspectJ Example using Annotation Config

1) How to declare aspects

An aspect is declared using the <aop:aspect> element, and the backing bean is referenced using the ref attribute as follows:

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspectBean">
   ...
   </aop:aspect>
</aop:config>

<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

2) How to define pointcuts

A pointcut helps in determining the join points to be executed with different advices. A pointcut will be defined as follows:

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspectBean">
		
		<aop:pointcut id="loggingOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
			
		<aop:pointcut id="transactionOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
			
   </aop:aspect>
</aop:config>

<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

In above example, two pointcuts (loggingOperation and transactionOperation) will match the methods defined in EmployeeManager class. Where loggingOperation pointcut will match all methods defined inside EmployeeManager, transactionOperation will match only EmployeeManager.getEmployeeById() method execution.

Read More : Spring AOP AspectJ Pointcut Expressions With Examples

3) Defining advises

You can declare any of the five advices inside an <aop:aspect> using the <aop:advise_name> element as given below:

<aop:config>
	
		<!-- Spring AOP Pointcut definitions -->
		<aop:pointcut id="loggingOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
			
		<aop:pointcut id="transactionOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />

		<!-- Spring AOP aspect -->
		<aop:aspect id="loggingAspect" ref="loggingAspectBean">
			
			<!-- Spring AOP advises -->
			<aop:before pointcut-ref="loggingOperation" method="logBefore" />
			<aop:after pointcut-ref="loggingOperation" method="logAfter" />
			
		</aop:aspect>

	</aop:config>

Example application for XML Based Spring AOP Configuration

In this example, I am applying before and after logging advises over all methods defined inside EmployeeManager interface, and transaction advise over EmployeeManager.getEmployeeById() method. Complete configuration file as below:

applicationContext.xml

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

	<!-- We don't need to this; This is required only in annotation based AOP support -->
	<!-- <aop:aspectj-autoproxy /> -->

	<aop:config>
	
		<!-- Spring AOP Pointcut definitions -->
		<aop:pointcut id="loggingOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
			
		<aop:pointcut id="transactionOperation"
			expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />

		<!-- Spring AOP aspect 1 -->
		<aop:aspect id="loggingAspect" ref="loggingAspectBean">
			
			<!-- Spring AOP advises -->
			<aop:before pointcut-ref="loggingOperation" method="logBefore" />
			<aop:after pointcut-ref="loggingOperation" method="logAfter" />
			
		</aop:aspect>

		<!-- Spring AOP aspect 2 -->
		<aop:aspect id="transactionAspect" ref="transactionAspectBean">
			<aop:before pointcut-ref="transactionOperation" method="getEmployeeById" />
		</aop:aspect>

	</aop:config>

	<!-- Spring AOP aspect instances -->
	<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />
	<bean id="transactionAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" />
	
	<!-- Target Object -->
	<bean id="employeeManager" class="com.howtodoinjava.demo.aop.EmployeeManagerImpl" />

</beans>

Other files used in this example are:

EmployeeDTO.java

public class EmployeeDTO {

	private Integer id;
	private String firstName;
	private String lastName;

	//Setters and Getters
}

EmployeeManager.java

public interface EmployeeManager 
{
	public EmployeeDTO getEmployeeById(Integer employeeId);

	public List<EmployeeDTO> getAllEmployee();

	public void createEmployee(EmployeeDTO employee);

	public void deleteEmployee(Integer employeeId);

	public void updateEmployee(EmployeeDTO employee);
}

EmployeeManagerImpl.java

public class EmployeeManagerImpl implements EmployeeManager
{
	public EmployeeDTO getEmployeeById(Integer employeeId) {
		System.out.println("Method getEmployeeById() called");
		return new EmployeeDTO();
	}

	public List<EmployeeDTO> getAllEmployee() {
		System.out.println("Method getAllEmployee() called");
		return new ArrayList<EmployeeDTO>();
	}

	public void createEmployee(EmployeeDTO employee) {
		System.out.println("Method createEmployee() called");
	}

	public void deleteEmployee(Integer employeeId) {
		System.out.println("Method deleteEmployee() called");
	}

	public void updateEmployee(EmployeeDTO employee) {
		System.out.println("Method updateEmployee() called");
	}
}

EmployeeCRUDLoggingAspect.java

public class EmployeeCRUDLoggingAspect
{
	public void logBefore(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());
	}
	
	public void logAfter(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDAspect.logAfter() : " + joinPoint.getSignature().getName());
	}
}

EmployeeCRUDTransactionAspect.java

public class EmployeeCRUDTransactionAspect
{
	public void getEmployeeById(JoinPoint joinPoint)
	{
		System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : "
				+ joinPoint.getSignature().getName());
	}
}

TestAOP.java

public class TestAOP
{
	@SuppressWarnings("resource")
	public static void main(String[] args) 
	{

		ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml");
		EmployeeManager manager = ( EmployeeManager ) context.getBean("employeeManager");

		manager.getEmployeeById(1);
		
		manager.createEmployee(new EmployeeDTO());
		
		manager.deleteEmployee(100);
	}
}

Output:

EmployeeCRUDAspect.logBefore() : getEmployeeById
EmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeById
Method getEmployeeById() called
EmployeeCRUDAspect.logAfter() : getEmployeeById


EmployeeCRUDAspect.logBefore() : createEmployee
Method createEmployee() called
EmployeeCRUDAspect.logAfter() : createEmployee


EmployeeCRUDAspect.logBefore() : deleteEmployee
Method deleteEmployee() called
EmployeeCRUDAspect.logAfter() : deleteEmployee

Drop me any comments/queries in comments section below.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. L S

    December 2, 2019

    Could you please elaborate on JointPoint Interface

  2. Srinivas KN

    August 27, 2019

    Fantastic, thanks a lot for the crystal clear explanation.

  3. harshith

    June 17, 2018

    nice code.. can we have logback.xml in this context , instead of using logging files

    • Krish

      December 12, 2018

      Could you please post download option also for entire code

  4. suresh

    May 19, 2017

    it very nice code.Thank you

  5. Binh Thanh Nguyen

    February 4, 2015

    Thanks, nice example code.

Comments are closed on this article!

Search Tutorials

Spring AOP Tutorial

  • AOP – Introduction
  • AOP – Annotation Config
  • AOP – XML Config
  • AOP – @Before
  • AOP – @After
  • AOP – @Around
  • AOP – @AfterReturning
  • AOP – @AfterThrowing
  • AOP – Before Advice
  • AOP – After Advice
  • AOP – Around Advice
  • AOP – After-Returning Advice
  • AOP – After-Throwing Advice
  • AOP – Pointcut Expressions
  • AOP – Aspects Ordering
  • AOP – Transactions
  • AOP – Interview Questions

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)