2008年9月16日星期二

Spring代理 之 ProxyFactory

  1. package home.dong.aop;
  2. import java.lang.reflect.Method;
  3. import home.dong.advice.MyMethodBeforeAdvice;
  4. import home.dong.impl.Tiger;
  5. import home.dong.inter.Animal;
  6. import org.aopalliance.aop.Advice;
  7. import org.springframework.aop.MethodBeforeAdvice;
  8. import org.springframework.aop.framework.ProxyFactory;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;
  11. import org.springframework.aop.support.DefaultPointcutAdvisor;
  12. public class Spring_Base_AOP {
  13.     public void proxy_A_Animal_1() {
  14.         Tiger animal_Tiger = new Tiger();
  15.         ProxyFactory factory = new ProxyFactory();
  16.         factory.addAdvice(new MyMethodBeforeAdvice());
  17.         // 如果设置了接口
  18.         // factory.setInterfaces(new Class[]{Animal.class});
  19.         // 或
  20.         // factory.addInterface(Animal.class);
  21.         // 那么 ProxyFactory会 使用java 代理来实现 (使用java 代理必须设置接口---既代理目标对象所实现的接口 )
  22.         // 如果没有设置或添加接口 ProxyFactory 则会使用 CGLIB来实现代理
  23.         factory.setTarget(animal_Tiger);
  24.         Animal tiger = (Animal) factory.getProxy();
  25.         System.out.println(tiger._Fight("Lion"));
  26.     }
  27.     public static void main(String[] args) {
  28.         Spring_Base_AOP aop = new Spring_Base_AOP();
  29.         aop.proxy_A_Animal_1();
  30.     }
  31. }

标签:

0 条评论:

发表评论

订阅 博文评论 [Atom]

<< 主页