3
votes

Im working with the Spring Framework 3.0.5 and Spring Security 3.0.5 and Ive got questions to the aspect orientated programming. At the moment Im trying to figure out the disadvantages and advantages of aspect orientated programming. Of course, I know them in theory: I can avoid redundant code, I only have to make changes in the aspect, not everywhere in the code and so on. But I still got some questions:

The disadvantage I found out:

I wrote a sample application using aspects with Spring AOP. I configured an Aspect with Annotations (@Pointcut, @Before, @Aspect and so one). The methods that triggered the aspect (which were part of a Pointcut of course) were of course part of a different class and not annotated with anything.

=> I really think that one big disadvantage is that when watching those methods of the other class it was not clear that they trigger an aspect. They needed no annotations or anything else, they were just mentioned in the pointcut of the aspect. (I really hope you understand what I mean). So thats why I think that AOP makes code also less understandable!

a) Is there a solution to this problem? (Can this maybe be solved when I put the whole configuration in an XML File? I dont think so.)

b) Would this problem still exist when I would use AspectJ instead of Spring AOP?

Springs Features using Spring AOP: they dont have this disadvantage?

As Spring AOP is part of many Spring Features (just like declarative Transaction Management or (maybe) Spring Security(?)) I took a closer look at those Features. I was not able to find any disadvantage at all.

c) Lets take the declarative transaction management as an example: managing transactions is so easy with those annotations (@transactional) and I dont really find the disadvantage I mentioned above. I can see the methods that trigger specific behaviour. (all @transactional methods trigger transactional behaviour) Maybe I misunderstood something and this isnt where AOP is used? But if I did not misunderstood this, why is it here possible to see which methods trigger aspects and why isnt it possible to see in my example above? I would really like to know this!

Thank you for answering! :-)

EDIT: a) and b) are answered (use an IDE which marks those methods), c) is still missing :-)

3

3 Answers

3
votes

For Point b) If you use an Eclipse with Spring IDE and AspectJ plugin, or STS, the IDE will show you where Aspects are woven in.


For Point b) If you use AspectJ and an IDE that supports AspectJ (Eclipse with AspectJ Plugin, or STS), then you will see markers in the souce-code where the Aspect is woven in.

An disadvantaged of Compile time AspectJ is, that you are not able to wove aspects in libraries. (without advanced techniques).


For Point c) There is only one disadvantage of declarative Aspects like @Transactional. -- You can forget to put the annotation on the method.

But if you have for example a rule like: every public method in a Class annoteted by @Service (or if you like to build you own @TransactionalService), is transactional, then you do not need to specifiy the @Transactional annotation on each method. -- So in Summary: declarative Aspects are very good for reading (you will not overlook them), and they are good if your code is very (lets say) "individual" (instead of the term "not consistent") . But If you work in an Environment with Strong Architecural Rules (like every public method in a @Service class...), then you can Write this rules down in a Point Cut Definition, instead of using declarative Aspects.

2
votes

Actually for point a) the same answer holds that Ralph gave: use Eclipse with either AspectJ plugin or if you are using Spring anyway, use STS. That way you will see in your IDE if a certain method matches a pointcut on the left side of your editor, represented by small red arrows:

enter image description here

0
votes

Actually, Spring AOP supports creating custom annotations.

I defined a annotation named Loggable binding with Advice.The Loggabel could be applied to any method you want.

public @interface Loggable {

}

@Aspect
public class EmployeeAnnotationAspect {

    @Before("@annotation(com.ben.seal.spring.aspect.Loggable)")
    public void myAdvice(){
        System.out.println("Executing myAdvice!!");
    }
}