Skip to content

SR – Chapter 6

April 27, 2010
tags:

Spring AOP interprets the pointcut expressions at runtime by using a library provided by AspectJ.Spring AOP only supports method execution join points for the beans in its IoC container. By default it uses J2SE dynamic proxies and if no interface existing for the class uses CGLIB. Use LTW/CTW if :::
(1) additional pointcut types / or/ joinpoint on constructor/field
(2) Apply your aspects to objects created outside the Spring IoC container

Steps to now have AOP in spring are:
1. modify the configuration file to have the aop ns and tag aop:aspectj-autoproxy
2. Create a class for the aspect and mark it as @Aspect with the respective method in it as either of the following aspect: @Before, @After, @AfterReturning, @AfterThrowing, @Around. Specify the pointcut along with it – which class/method to apply it to.
3. Voila! Aop is running in your app.

Example”: @Before(“execution(* *.*(..))”)
No more need of the classical way to do it – implementing the interfaces or specifying interceptors/interfaces in the configuration file.

@Order(number) to be used OR impl the Ordered interface

MyAspect implements Ordered {
public int getOrder() {
        return 0;
}

To use the same pointcut definition for multiple aspects:

@Pointcut("execution(* *.*(..))")
private void loggingOperation() {}

@Before("pointcut = loggingOperation()")
public void logBefore(JoinPoint joinPoint) {
 }

—————————————————
Pointcut definition in spring
execution (modifier(Needed public etc.) Returntype(Optional) packageName+ClassName.MethodName(Parameter Types)
execution(* *.*(..))”)
=========================
Custom annotation and pointcut definitions

This annotation can be used at class or method level.

@Target( { ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggingRequired {
}

Then use this as marker

@LoggingRequired
public double add(double a, double b) {

And then final REAL usage is for pointcut definition:
@annotation(com.spring.recipe.LoggingRequired) //exec this aspect for all methods with this annotation

Match within package, subpackages, or all methods of class: within(com.spring.recipe..*)
To match all classes implementing interface within(IntefaceName+ )

Also bean name matching pointcut:

bean(idOrNameOfBean)
————————————-
AspectJ, pointcut expressions can be combined with the operators && (and), || (or), and !
(not). Can get real parameters instead of Joinpoint: @Pointcut(“execution(* *.*(..)) && target(target) && args(a,b)”) :::: public void parameterPointcut(Object target, double a, double b) {}

Introduction
Introduction is a special type of advice in AOP. It allows your objects to implement an interface
dynamically by providing an implementation class for that interface. It seems as if your
objects had extended the implementation class at runtime.

Load Time Weaving
———————-
This happens when classes loaded into JVM by class loader. Class loader behavior modified by aj classloader.
Steps to do it:

1. Define the aspect implementing class
@Aspect
public class ComplexCachingAspect {
@Around(“call(public Complex.new(int, int)) && args(a,b)”)
public Object cacheAround(ProceedingJoinPoint joinPoint, int a, int b)
}

2. Create aop.xml file with the name of the aspect class and point cut defintion:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN"
"http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
      <weaver>
             <include within="com.apress.springrecipes.calculator.*" />
      </weaver>
      <aspects>
           <aspect name="com.apress.springrecipes.calculator.ComplexCachingAspect" />
       </aspects>
</aspectj>

(3) Add the classloader on classpath before launching
java -javaagent:c:/spring-framework-2.5/lib/aspectj/aspectjweaver.jar
com.spring.recipe.Main

To use spring add path in java agent : c:/spring-framework-2.5/dist/weaving/spring-agent.jar
and in config file xmlns:context=”http://www.springframework.org/schema/context and si as well as tag named context:load-time-weaver.
~~~~~One instance aop/spring~~~~~
Conflict is AJ creates own instance and spring creates own instance of Aspect – to get one use the factory-method attribute that defines static class method aspectOf inside the newly created bean instance – which is used to be spring to achieve ONE common instance.

~~~~ DI for Non-Spring Obj’s ~~~~~~~
1. Mark NonSpring Class as @Configurable
2. Configuration : context:spring-configured tag
3. spring AOP needs to be enabled so follow all steps above – including spring-agent for java agent.

~~Set @Autowired for methods and also configure [context:annotation-config] tag

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: