4
votes

From Oracle:

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

and from wikipedia:

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development.

My question is: If I use a annotation like @EJB , can I say that I am using AOP? Because it seems like @EJB is a cross-cutting concern to me (e.g. resource looking up). Please explain if yes or no, or sometimes an annotation is AOP.

3
EJB manages dependence injection, AOP is a different thing. Anyway annotations can be used by AOP framework.holap

3 Answers

8
votes

Annotations are not themselves AOP, they just decorate a class, property or method with metadata. In AOP terms, you could say annotations describe "Aspects", but by themselves they do not provide any extra functionality.

AOP usually requires information to determine where to inject or provide interception. Some AOP libraries use XML to describe the places of the program in which to weave their functionality, others use annotations for this same purpose.

So, Annotations are not AOP, but rather a way of marking locations in the code where AOP can intervene and add cross-cutting concerns.

1
votes

The whole cross cutting concern mumbo jumbo I find is very confusing with AOP. The gist is that with AOP, at compile time, something inserts code into your code, creating a new munged bytecode. So imagine you have an aspect that is (peudocode)

class Aspect{
   public void OnMethodBoundaryStart(some stuff){
       System.out.println("Hey the method is starting!")
   } 
}

class AspectUser{
    @OnMethodBoundaryStart
    public void SomeMethod(){
       System.out.println("Something");
    }
}

Then the OnMethodBoundaryStart code is actually literally inserted into your bytecode, the annotation just decorates the method so that the infrastructure that does the bytecode insertion knows what to do.

So, annotations are not AOP, and AOP are not necessarily annotations. You can probably do AOP however you want, if you write your own AOP framework. All AOP means is that it decouples where you write your code and how its put together. You can have aspects that do stuff on return statements, or during exception throws, or at the end of a method. You can also use aspects to implement hidden interfaces, or classes, or all sorts of stuff.

0
votes

Aspect Oriented Programming its a concrete technique (i do not believe that AOP can be considered a paradigm), you create "Aspects", and aspect encapsulate the execution of some behavior and the places (point-cuts in AOP terminology) where this behavior are executed.

You can view annotations like a mechanism for defining "point-cuts", and you need another thing like a runtime environment (an application server for example) or perhaps a compile-time routine (think in annotations for generating some form of documentation) to execute the behavior associated with that annotation.

Sometimes are used for similar purposes, for example and annotation for transactions its used to implement this cross-cutting concern, with AOP can be implemented very similar. Simply are different techniques that something are used for solving similar problems, but annotations are not a form of AOP in my opinion.