1

I was wondering how does AOP actually works and is there a way that we can create our own AOP. what should be the steps taken and best practice for the same?

Varun
  • 5,001
  • 12
  • 54
  • 85
  • For how it works: http://stackoverflow.com/questions/5589319/understanding-spring-aop?rq=1 –  Sep 16 '15 at 05:55
  • @RC. - it tells the working... I am looking forward to see how is it internally implemented – Varun Sep 16 '15 at 07:07
  • @Varun: Read this - http://www.javacodegeeks.com/2015/03/create-your-own-aop-in-java.html – Ankit Jan 01 '16 at 15:27

2 Answers2

0

Essentially there are two different concepts for AOP:

  • Class-weaving: using this concept you create "advices", that can be translated into regular java code while compiling bytecode. (altough there are possibilities to do this in run-time). This approach allows you to write a code once, and (through advices) distribute this code across many classes that might have no relation to each other whatsoever.

  • Proxy-based: this you can imagine as an example usage of the "Decorator" design pattern. Using bytecode instrumentation certain libraries (like cglib) you can intercept function calls and add your own code to be executed beforehand. This is a more "natural" (and surely simpler) approach, so if you are really going to implement your own AOP library then probably this is the better approach to go with.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
0

There are different steps for AOP, but the basics turn around doing automatically another action on specified condition, for example:

  • before calling any method on any class of a dedicated package, log something
  • get the time before executing a specific method and after to time it
  • replace any access to a specific attribut of a class with a direct access to a database element

The first 2 examples are easier to implement because they involve methods. So if the methods are public a proxy mechanism will be enough. The third one really requires bytecode editing.

So the first question you must ask yourself is what is your real need. If you need full AspectJ power, the only way will be bytecode editing. My advice here would be to not try to reinvent the wheel and rely on AspectJ implementation

If the only interception points are public method calls, you could try to mimic the Spring AOP which is based on JDK proxies. It can also use cglib proxies which are slightly more intrusive but a little more powerful (do not need interfaces and can deal with not public methods).

TL/DR: So you have 3 implementation levels from simpler/less powerfull to harder/full power:

  • rely on JDK proxies : can only process public method calls, but certainly the simplest way; another limit, cannot process internal calls (calls originating from the class itself)
  • rely on CGLib proxies : intermediary way: requires limited bytecode editing, and can process private method calls, but still cannot deal with direct attribute access
  • reimplement AspectJ : full power but... good luck on it!
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252