Spring AOP + AspectJ XML Configuration Example

Lokesh Gupta

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

Comments

Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.