I work with Spring Framework 3.0.5 and Id like to understand the basic principals of Spring. One of it is AOP.
One basic idea of the spring framework is to keep the objects itself simple and to have clear code.
DI follows this basic idea. It provides clear code and the objects itself can be very simple. The dont have to look up their dependencys.
Now what about AOP: I mean, code is for sure clearer with AOP, but does AOP also have the basic idea of keeping the objects as simple as possible? Im not sure of that, thats why Id like to know some other opinions 🙂
Thanks in advance!
Take the following code snippets as an example. The easy way:
The traditional way (you can manage transactions using
EntityManagerinterface, but that is not the point):What does it have to do with AOP? Transaction management is one of the most widely used examples of AOP and Spring is not an exception. The former code snippet uses
@Transactionalannotation to apply transaction demarcation using aspect, while the latter manages transactions manually. See the benefit?The key thing: use aspects for cross-cutting concerns like logging, profiling, etc. Don’t build your business logic in aspects. This way your beans remain clear while irrelevant code from business perspective is hidden in the aspect.
Also AOP allows you to do all sorts of sophisticated stuff with ease. Take for instance HTTP session handling – with
sessionbean scope and AOP proxying you can access session without even realizing it.To summarize – it is great tool when used for the right job.