Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

June 11, 2010

Spring AOP

Spring does not create a proxied object until that proxied bean is needed by the
application. If you are using an ApplicationContext, the proxied objects will be
created when it loads all of the beans from the BeanFactory. Because Spring creates
proxies at runtime, you do not need a special compiler to use Spring’s AOP.
Spring generates proxied classes in two ways. If your target object implements
an interface(s) that exposes the required methods, Spring will use the JDK’s
java.lang.reflect.Proxy class. This class allows Spring to dynamically generate
a new class that implements the necessary interfaces, weave in any advice, and
proxy any calls to these interfaces to your target class.
If your target class does not implement an interface, Spring uses the CGLIB1
library to generate a subclass to your target class. When creating this subclass,
Spring weaves in advice and delegates calls to the subclass to your target class.
When using this type of proxy generation, you need to deploy all of the JAR files
in the lib/cglib directory of your Spring distribution with your application.
There are two important things to take note of when using this approach:
■ Creating a proxy with interfaces is favored over proxying classes, since this
leads to a more loosely coupled application. The ability to proxy classes is
provided so that legacy or third-party classes that do not implement interfaces
can still be advised. This approach should be taken as the exception,
not the rule.
■ Methods marked as final cannot be advised. Remember, Spring generates
a subclass to your target class. Any method that needs to be advised is overridden
and advice is woven in. This is not possible with final methods.

Spring implements AOP Alliance interfaces
The AOP Alliance is a joint project between several parties interested in implementing
AOP in Java. The AOP Alliance shares the same belief as Spring that AOP
can provide cleaner and easier solutions for Java enterprise applications than what
is currently offered by EJB. Their goal is to standardize Java’s AOP interface to provide
interoperability between various Java AOP implementations. This means that
AOP advice that implements their interfaces (as do some of Spring’s implementations)
will be reusable in any other AOP Alliance–compatible framework.
Spring only supports method joinpoints
As mentioned earlier, multiple joinpoint models are available through various
AOP implementations. Spring only supports method joinpoints. This is in contrast
to some other AOP frameworks, such as AspectJ and JBoss, which provide
field joinpoints as well. This prevents you from creating very fine-grained advice,
such as intercepting updates to an object’s field.
However, as Spring focuses on providing a framework for implementing J2EE
services, method interception should suit most, if not all, of your needs. Plus,
Spring’s philosophy is that field interception violates encapsulation. A fundamental
object-oriented concept is that objects initiate operations on themselves
and other objects through method calls. Having advice fired on field modification
as opposed to method invocation arguably violates this concept.

No comments: