Sunday, March 20, 2011

Binding parameters to advice

A joinpoint object provides all the details required by the advice to perform any kind of operation.

Suppose we have to validate an order, and so we are writing our own aspect to get argument from pointcut - expression and perform validation. Example:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import service.Order;

public class OrderValidator {

@Before("execution(* placeOrder(..)) && args(order)")
public void validateOrder(Order order) {
//some order validation logic here
System.out.println("Just checking it out!");

In the config file we write:

<aop:aspectj-autoproxy />

<bean class="ex3.OrderValidator" />

<bean class="service.OrderServiceImpl" />

No comments:

Post a Comment