AspectJ Expression Pointcut : Spring Aspect « Spring « Java Tutorial






File: Main.java

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import bean.MyClass;
import bean.SimpleAfterAdvice;

public class Main {

  public static void main(String[] args) {
    MyClass target = new MyClass();

    AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
    pc.setExpression("execution(* bean..*.get*(..))");

    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice());

    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);

    MyClass proxy = (MyClass) pf.getProxy();
    System.out.println(proxy.getName());
    proxy.setName("New Name");
    System.out.println(proxy.getHeight());

  }

}

File: MyClass.java

package bean;


public class MyClass {

    public String getName() {
        return "AAA";
    }

    public void setName(String name) {

    }

    public int getHeight() {
        return 201;
    }
}

File: SimpleAfterAdvice.java

package bean;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;


public class SimpleAfterAdvice implements AfterReturningAdvice{
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("After method: " + method);
    }
}
  Download:  Spring-AspectJExpressionPointcut.zip( 4,746 k)








28.50.Spring Aspect
28.50.1.Spring Tracing Aspect
28.50.2.Spring Aspect Logging
28.50.3.AspectJ Expression Pointcut
28.50.4.AspectJ AutoProxy
28.50.5.Aspect Filter
28.50.6.Aspect Annotation Pointcut Around After
28.50.7.Aspect Annotation
28.50.8.AOP Annotation
28.50.9.Annotation Scope
28.50.10.Spring AOP Examples