Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring AOP

DZone's Guide to

Spring AOP

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

 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>
</property>
</bean>

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

// 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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}