Spring AOP
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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:





Comments
Marco Tedone replied on Wed, 2011/05/18 - 3:25am
Hi,I wrote a very similar article yesterday: http://java.dzone.com/articles/aop-made-easy-aspectj-and
Regards,
Marco
Artur Mkrtchyan replied on Wed, 2011/05/18 - 5:17am
in response to:
Marco Tedone
Philopator Ptolemy replied on Wed, 2011/05/18 - 8:21am
Marco Tedone replied on Wed, 2011/05/18 - 9:43am
in response to:
Philopator Ptolemy
Mitch Pronschinske replied on Wed, 2011/05/18 - 11:43am
Artur Mkrtchyan replied on Wed, 2011/05/18 - 12:27pm
in response to:
Mitch Pronschinske
Aamir Iqbal replied on Thu, 2011/05/19 - 8:31am
Well first of all thanks alot for such a good article. I really appreciate your effort.
But on the other hand, i had a bad experience with Aspect Orientation. As i used it in my previous project.
I am not an expert in its usage, but what i understand it is, Aspect Orientation creates a layer over native Java Compiler. Means firstly code compiles into Aspect orientation, then it is given to Java Compiler. Due to this, i faced some issues in Eclipse IDE. eg. Eclipse highlights a variable or a statement underlined RED as an error. But when i click on Run, it works perfectly fine.
While in some cases, the issue was totally reverse. Means the Eclipse IDE does not show any error being highlighted. But when i Click on Run then it prints a huge list of errors on console.
These things frustrated me alot. and eventually i started disliking Aspect Orientation. I dont know whether it was due to Aspect Nature or it was something else, as i was new to Aspect Orientation.
Regards,
Amir Iqbal
Mehdi Heidarzadeh replied on Fri, 2011/05/27 - 2:51am