Over a million developers have joined DZone.

Spring AOP

DZone's Guide to

Spring AOP

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

 Let's define some well known problems

  • Some programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the code
  • Examples:
    • Logging (tracking program behavior to a file)
    • Profiling (determining where a program spends its time)
    • Tracing (determining what methods are called when)
    • Transaction Management
    • Security Management
  • The result is crosscuting code--the necessary code cuts across many different classes and methods

 Above-mentioned problems are easey solvable via AOP.

 Now let's define some AOP concepts

  • Aspect-oriented programming (AOP) provides for simplified application of cross-cutting concerns
  • Joinpoint - Execution point to target
    • Typically, methods
  • Advice - Action taken at a particular joinpoint.
  • Pointcut - A set of joinpoints specifying where advice should be applied (e.g. Regular expression)

 Now it's time to move to Spring AOP

  • Generally, Spring AOP uses the following techniques.
    • Uses Dynamic Proxies if interface available otherwise CGLIB
    • CGLIB creates derived class which proxies requests
  • Less capable than AspectJ
    • does not have field interception
    • only runtime weaving solution is available

 Let's see real Logger example

// Biz Interface
public interface MyBiz {
public void doBizMethod();

//Biz Interface Impl
public class MyBizImpl implements MyBiz {
public void doBizMethod() {
System.out.println("Business Method");

//Around Advice Class
public class MethodLogger implements MethodInterceptor {

public MethodLogger() {}
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// same with MethodBeforeAdvice
System.out.println("AOP: Before method call!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("AOP: After method call!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("AOP: Throw exception!");
throw e;


 Spring 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-2.0.xsd">

<!-- business beans -->
<bean id="myBiz" class="com.attask.seminar.aop.spring.MyBizImpl"/>
<!-- aop -->
<bean id="methodLogger" class="com.attask.seminar.aop.spring.MethodLogger" />
<bean id="myBizProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="myBiz" />
<property name="interceptorNames">
<list> <value>methodLogger</value></list>

// Simple Usage
public static void main(String[] args) {
ClassPathXmlApplicationContext ctxt = new ClassPathXmlApplicationContext("SpringBeans.xml");
MyBiz bizInstance = (MyBiz)ctxt.getBean("myBizProxy");

// Result will be
AOP: Before method call!
Business Method
AOP: After method call!

 Note Try to use advice which is appropriate for your specific case (Before, After, Throws)

 Find attached example under Resources Section.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}