Hello I have started learning spring MVC. Done good enough to write code , but I still find AOP as hard concept to understand, have already checked this post. And gone through Spring documentation. I still find it confusing as the explaintaion is not in layman terms and hard to understand. AOP have common example of logging. Still not understood its soul. Can you please explain in the layman language what are the concepts with sample implementation with and without that. A link to good tutorial is welcome. Please donot post long arguments so that this post becomes useless. This is intended only for explaination. Benifits/drawbacks of using it can be ignored for now.
1 Answers
I think the easiest way to look at Aspecting, in terms of a definition, is a declarative way to define a proxy (or proxies) with a single expression.
Contrived situation:
Say that you want to count how many times a 'getter' method is invoked, any getter method in your application. Well, you find each 'getter' method individually and increment some static counter. That might be acceptable to you, but you will also need to make sure you do the same thing for each 'getter' method that is added to your application from that point on.
Consider an aspect:
The aspect is made up of 2 main things. The proxy, which is the code being executed, and the declarative expression, which tells where the proxy code is applied.
A proxy could be written for a 'getter' that, either before or after method invocation, increments the counter automatically. The issue with this proxy is that it still needs to be applied to each 'getter' method. This is where an aspect shines. You can write a quick definition (via your favorite aspecting libraries expression language) that says, 'do this proxy for all 'getter' methods.
Example Using SpringAOP:
@Aspect
public class MyAspect
{
private static int counter = 0;
@Before("execution(* get*())")
public void incrementCounterProxy()
{
counter++;
}
}
Description of example:
Assuming you have everything setup with your Spring container properly to pick up aspects, this should fulfill our contrived goal.
@Aspect
marks our class as a class that is a candidate for containing aspects.@Before
marks our proxy type. The code in the method will be a proxy that only does things before the target method invocation.execution(* get*())
is our declarative expression that applies this proxy to all executions of methods that start with 'get' and have no parameters.

- 4,004
- 4
- 27
- 40

- 42,483
- 9
- 127
- 120
-
nice simplistic explaination. Understood what is happeining . but Aspect and proxy are merged now, can you please do little seperationn of both. Anyways I am marking your explaination as answer. As per me aspect is the piece of code having the common code to execute. while proxy is design..m I correct?? – ManMohan Vyas May 31 '13 at 16:05
-
2The aspect is made up of 2 main things. The proxy, which is the code being executed, and the declarative expression, which tells where the proxy code is applied. – nicholas.hauschild May 31 '13 at 16:14