Package us.codecraft.tinyioc.aop

Source Code of us.codecraft.tinyioc.aop.Cglib2AopProxyTest

package us.codecraft.tinyioc.aop;

import org.junit.Test;
import us.codecraft.tinyioc.HelloWorldService;
import us.codecraft.tinyioc.HelloWorldServiceImpl;
import us.codecraft.tinyioc.context.ApplicationContext;
import us.codecraft.tinyioc.context.ClassPathXmlApplicationContext;

/**
* @author yihua.huang@dianping.com
*/
public class Cglib2AopProxyTest {

  @Test
  public void testInterceptor() throws Exception {
    // --------- helloWorldService without AOP
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("tinyioc.xml");
    HelloWorldService helloWorldService = (HelloWorldService) applicationContext.getBean("helloWorldService");
    helloWorldService.helloWorld();

    // --------- helloWorldService with AOP
    // 1. 设置被代理对象(Joinpoint)
    AdvisedSupport advisedSupport = new AdvisedSupport();
    TargetSource targetSource = new TargetSource(helloWorldService, HelloWorldServiceImpl.class,
        HelloWorldService.class);
    advisedSupport.setTargetSource(targetSource);

    // 2. 设置拦截器(Advice)
    TimerInterceptor timerInterceptor = new TimerInterceptor();
    advisedSupport.setMethodInterceptor(timerInterceptor);

    // 3. 创建代理(Proxy)
        Cglib2AopProxy cglib2AopProxy = new Cglib2AopProxy(advisedSupport);
    HelloWorldService helloWorldServiceProxy = (HelloWorldService) cglib2AopProxy.getProxy();

    // 4. 基于AOP的调用
    helloWorldServiceProxy.helloWorld();

  }
}
TOP

Related Classes of us.codecraft.tinyioc.aop.Cglib2AopProxyTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.