• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

What is AOP?

Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Posts: 43016
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out AspectJ. It is one of the most commonly used AOP libraries, and its web site has lots of introductory material.
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton abhishek paul for this informative reply.
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic