Loading
 
What's Next: View All Tutorial →

AOP (Aspect Oriented Programming) using Spring

A simple program to demonstrate how the AOP works using the point cut expression An analogy used is "Singer perfoms sing", Before or after sings, the Audience action will be injected thro...

Published Date: 03/09/2018  Last Modified Date: 03/09/2018    New Post



A simple program to demonstrate how the AOP works using the point cut expression

An analogy used is "Singer perfoms sing", Before or after sings, the Audience action will be injected through pointcut expression


Steps in implementation

1. Create a pointcut expression @PointCut
2. Inject the @PointCut method to the @Before,@AfterReturning method
 

Maven Dependency

 
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.demo</groupId>
	<artifactId>spring-aop1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-aop</name>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>
	</dependencies>
</project>


AOPConfig.class

 
package com.demo.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages = "com.demo.spring")
@EnableAspectJAutoProxy
public class AOPConfig {

	public static void main(String args[]) {
		ApplicationContext ctx = new AnnotationConfigApplicationContext(AOPConfig.class);

		Singer singer = (Singer) ctx.getBean("singer");

		singer.perform();

//If proxy is inject, the class name changed with proxy name, please check output
		System.out.println(singer.getClass().getName());
	}

}

Singer.java

 
package com.demo.spring;

import org.springframework.stereotype.Service;

@Service
public class Singer {

	public void perform() {

		System.out.println("Singer is now signing...");

	}
}

Audience.java using @Pointcut & @Before @AfterRerturning

 
package com.demo.spring;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Audience {

	@Pointcut("execution (* com..spring.Singer*.perf*(..))")
	private void pcut() {

	}

	@Before("pcut()")
	public void switchOffMobile() {
		System.out.println("Audience Switched off mobile..");
	}

	@Before("pcut()")
	public void takeSeat() {
		System.out.println("Audience taking seats..");
	}

	@AfterReturning("pcut()")
	public void applaud() {
		System.out.println("Well Done! Clap! Clap! Clap!..");
	}
	
}

@Pointcut can be implememented using wildcard 

Option #1
execution (void com.demo.spring.Performer.perform(..))
 
Or
Option #2
execution (* com..spring.Perf*.perf*(..))
 
 

Audience.java using @Around

 
package com.demo.spring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Audience {

	@Pointcut("execution (* com..spring.Singer*.perf*(..))")
	private void pcut() {

	}

//	@Before("pcut()")
	public void switchOffMobile() {
		System.out.println("Audience Switched off mobile..");
	}

//	@Before("pcut()")
	public void takeSeat() {
		System.out.println("Audience taking seats..");
	}

//	@AfterReturning("pcut()")
	public void applaud() {
		System.out.println("Well Done! Clap! Clap! Clap!..");
	}

	@Around("pcut()")
	public void invoke(ProceedingJoinPoint pjp) throws Throwable {

		takeSeat();
		switchOffMobile();
		pjp.proceed();
		applaud();
	}

}


 

Output:


16:51:05.150 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'audience'
Audience Switched off mobile..
Audience taking seats..
Singer is now signing...
Well Done! Clap! Clap! Clap!..
com.demo.spring.Singer$$EnhancerBySpringCGLIB$$d9907a23





 
Awaiting for Administrator approval





Tags: Spring Core

← Back to list


Related Post




×