This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programming Diversions and the fly likes What is AOP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "What is AOP? " Watch "What is AOP? " New topic

What is AOP?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Can someone help me in understanding AOP. I read in wikipedia but was not able to get a good grasp. Is there something in java that i can try practicing.
I know that it is being used in Spring and some other frameworks...

Thanks for the help in advance.

My Blog SCJP 5 SCWCD 5
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41632
Check out AspectJ. It is one of the most commonly used AOP libraries, and its web site has lots of introductory material.

Ping & DNS - my free Android networking tools app
abhishek paul

Joined: Sep 26, 2006
Posts: 25

I am sure you might have gone thru wikis and other online stuff to get yourself acquainted with AOP. Heres my 2 cents..

While Object Oriented Programming concepts revolves around Classes,Objects and Methods, Aspect Oriented Programming talks primarily about Aspects which according to the Spring Reference Docs can be defined as -

Aspect : A modularization of a concern that cuts across multiple objects.

Lets have a example, that of Logging in the Application. So in a plain vanilla Java Application one would achieve Logging in the following way:

1. Prepare a log4j file (.properties/.xml).
2. Make sure the Application is able to access this file. By loading it using PropertyConfigurator/DOMConfigurator or using a Servlet.
3. Then in the Classes where logging in intended put the following lines of code :

Logger logger = Logger.getLogger(this.getClass());

And then use the appropriate methods DEBUG, INFO, ERROR etc as needed.
In order to implement logging throughout the application step (3) needs to be repeated wherever logging is required. And we end up having redundant lines of code achieving a common goal – Logging. Here we find that Logging is definitely a general feature of the Application. In AOP jargon this general feature is termed as a concern, one that cuts through the entire application – a crosscutting concern

Now let’s again take a look at the definition (see above).So AOP suggests that Aspects be defined which in turn would work on a particular concern (i.e. logging in the example above).These Aspects can then be plugged into an application in a flexible and modular way.

Following points can be summed up at this point:

1. Aspects separate the concern from the rest of the code into its own structure. The crosscutting concern is encapsulated.
2. Invocation of an Aspect is Implicit.

Some AOP terminology -

Aspect : A modularization of a concern that cuts across multiple objects .Apart from that seen above Transaction Management is also a good example of a crosscutting concern in J2EE applications.So since Aspects are plain Java Objects(POJOs), these can replace EJB.

JoinPoint : Point during execution of program such as a execution of a method or handling of an exception.

Advice : Code that is invoked during program execution, and is the logic for implementing an Aspect.

Pointcut : A set of joinpoints specifying when an Advice should be fired. The pointcut concept is the key to AOP. This enables Advice to be targeted independently of OO hierarchy.

A comparison between OOP and AOP -
Similarities - both use Objects. The behavior and data of a concern is combined into a single physical entity using objects.
Differences - OOPs tries to push the scattered code for the concerns up in the inheritance tree. This is often not possible and results in tangled code.AOP collects scattered concerns into a single class structure and inserts them horizontally into the code.

Advantages of AOP -
Separation of Concerns - AOP makes it possible to encapsulate even cross cutting concerns.
Simpler System Evolution - Join points allow changes to programs to be incorporated simply with aspects.
Reuse - Aspects can often be reused in other programs with only slight modifications.
Adaptability - Easy to quickly adapt a change in a concern.
More Comprehensive Systems - Separation of concerns makes it easier to quickly see and understand concerns.

To repeat what others have said, requires education, to challenge it , requires brains.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks a ton abhishek paul for this informative reply.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: What is AOP?