Java/Spring/AfterReturningAdvice

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

AfterReturningAdvice Demo

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);
    }
}





Check Logic In AfterReturningAdvice

       
File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class Main {
  public static void main(String[] args) {
    KeyGenerator target = new KeyGenerator();
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(target);
    factory.addAdvice(new WeakKeyCheckAdvice());
    KeyGenerator keyGen = (KeyGenerator) factory.getProxy();
    System.out.println("Key: " + keyGen.getKey());
  }
}
class KeyGenerator {
  public static final long WEAK_KEY = 1L;
  public static final long STRONG_KEY = 2L;
  public long getKey() {
    return WEAK_KEY;
    // return STRONG_KEY;
  }
}
class WeakKeyCheckAdvice implements AfterReturningAdvice {
  public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
      throws Throwable {
    if ((target instanceof KeyGenerator) && ("getKey".equals(method.getName()))) {
      long key = (Long) returnValue;
      if (key == KeyGenerator.WEAK_KEY) {
        System.out.println("a weak key");
      }
    }
  }
}





DefaultPointcutAdvisor and AfterReturningAdvice

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);
    }
}





implements AfterReturningAdvice

       
File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class Main {
  public static void main(String[] args) throws Exception {
    MessageWriter target = new MessageWriter();
    // create the proxy
    ProxyFactory pf = new ProxyFactory();
    pf.addAdvice(new SimpleAfterReturningAdvice());
    pf.setTarget(target);
    MessageWriter proxy = (MessageWriter) pf.getProxy();
    // write the messages
    proxy.writeMessage();
  }
}
class MessageWriter {
  public void writeMessage() {
      System.out.println("A");
  }
}
class SimpleAfterReturningAdvice implements AfterReturningAdvice {
  public void afterReturning(Object returnValue, Method method, Object[] args,
          Object target) throws Throwable {
      System.out.println("After method: " + method.getName());
  }
}