Skip to content

AspectJ

Understanding AspectJ in Spring

The execution points matched by a pointcut are called join points. In this term, a pointcut is an expression to match a set of join points, while an advice is the action to take at a particular pointcut.

An after advice is executed after a join point finishes, whenever it returns a result or throws an
exception abnormally. (Does that mean runtime exception is thrown????)

An after advice is executed regardless of whether a join point returns normally or throws an
exception – all checked exceptions thrown also this gets called?

@AfterReturning(
pointcut = “execution(* *.*(..))”,
returning = “result”)

Note the returning param – this allows that the method call return void – while logging result info.
AfterReturning and AfterThrowing (can specify exception type by throwing=”e” and e as parameter of specific exception.

** Use the least powerful advice.
Target object: The original object that was wrapped. – getTarget
Proxy object: The proxy receiving the call  – getThis

If you two aspects for same method the order can be specified by implementing the Ordered interface or the @Order annotation with value in paramter.
Eg: @Order(1) . Lower value implies higher precedence.

If you need to define multiple aspects for the same aspect – the using @Pointcut define the expression Once – and revolve all other aspects around or after it using the sig:   @Before(“loggingOperation()”)

within : package, sub packages, class,  classes implementing interfaces (intfc+)
bean : (*Operator) (all bean whose names end with operator)

Operands: or ||  && or ! can be used for defining point cut.

Also you can define parameter datatypes to your aspect methods instead of using JointPoint directly.
@Pointcut(“execution(* *.*(..)) && target(target) && args(a,b)”)
public void parameterPointcut(Object target, double a, double b) {}

and caller also needs to state args: @Before(“CalculatorPointcuts.parameterPointcut(target, a, b)”)
public void logParameter(Object target, double a, double b)

::::::::::IMPORTANT::::::::::::::::::::::::::::::::::::
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.
You can also restrict the method return type OR the parameters , fix it or first param eg: .add(double, …) or .add(double,double)
XMl aop limitation that it does not using apsectj internally but only spring fwk.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
=====================================================================================================================
CUSTOM ANNOTATION – Neat.
——————
@Target( { ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggingRequired {
}

Introducing behavior to spring.

@DeclareParents(
value = “com.apress.springrecipes.calculator.ArithmeticCalculatorImpl”,
defaultImpl = MaxCalculatorImpl.class)
public MaxCalculator maxCalculator;

Can cast: MaxCalculator maxCalculator = (MaxCalculator) arithmeticCalculator;

Introducte STATE to beans
@DeclareParents(
value = “com.apress.springrecipes.calculator.*CalculatorImpl”,
defaultImpl = CounterImpl.class)
public Counter counter;

AND @after method call (Use this instead of target – since this refers to proxy having the state notification listener method)

@After(“execution(* com.apress.springrecipes.calculator.*Calculator.*(..))”
+ ” && this(counter)”)
public void increaseCount(Counter counter) {
counter.increase();
}

LTW and CTW – Using aop.xml and AspectJ.
====================================================
Eg: call to capture the join points of calling the Complex(int, int) constructor. (not supported by Spring)
Weaving is the process of applying aspects to your target objects
LTW: java -javaagent:c:/spring-framework-2.5/lib/aspectj/aspectjweaver.jar
Namespace: xmlns:context=”http://www.springframework.org/schema/context”
and <context:load-time-weaver />

Each AspectJ aspect provides a static factory method called aspectOf(). This method used by Spring to get DI instance created by Aj, <beanaspectOf”>
————————
Domain objects: Created with new/hibernate
To inject spring beans into domain objects

AnnotationBeanConfigurerAspect : @Configurable: <context:spring-configured>: Jar file -> spring-aspects.jar
<context:load-time-weaver /> -javaagent:c:/spring-framework-2.5/dist/weaving/spring-agent.jar
OR
Match the id as param @Configurable(“complex”) and bean id is complex
, also,
@Autowired for setters, and xml <context:annotation-config />

————————–
AOP system related (bytecode-based or proxy-based AOP), syntax related (traditional AspectJ, @AspectJ, or XML syntax) or weaving options (build time or  load-time weaver), register AnnotationAwareAspectJAutoProxyCreator behind the scenes.

Advertisements
2 Comments leave one →
  1. anonym permalink
    January 21, 2010 10:17 pm

    I believe a small error is in your start sentence (The execution points matched by a pointcut are called join points. In this term, a pointcut is an expression to match a set of join points, while an advice is the action to take at a particular join point.)

    should be (…, while an advice is the action to take at a particular point cut)

  2. techzen permalink*
    April 27, 2010 4:20 pm

    Corrected that. thanks.

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: