Spring AOP demo 和获取被CGLIB代理的对象

本文分为两部分:1)给出Spring AOP的一个例子(会使用CGLIB代理);2)给出获取被CGLIB代理的原始对象。

1、Spring AOP Demo

这部分参考了博文(http://www.voidcn.com/blog/daguanjia11/article/p-4628090.html)

1.1添加maven依赖


    org.springframework
    spring-core
    4.0.5.RELEASE


    org.springframework
    spring-beans
    4.0.5.RELEASE


    org.springframework
    spring-context
    4.0.5.RELEASE


    org.springframework
    spring-aop
    4.0.5.RELEASE


    org.aspectj
    aspectjweaver
    1.8.1


1.2添加测试类

public class SpringAopTest {

    public static class Car {
        public void go() {
            System.out.println("go go go!");
        }
    }

    public static class CarLogger {

        public void beforeRun() {
            System.out.println("car is going to run");
        }

        public void afterRun() {
            System.out.println("car is running");
        }
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop-test.xml");
        Car car = (Car) context.getBean("car");
        System.out.println(car.getClass());
        car.go();
    }
}


1.3添加spring配置文件

放置在classpath的根目录下,额,放在resources目录下也就行了。




    
        
        
        
            
                

                
                
            
        
    


1.4运行main函数

得到如下结果:

car is going to run
go go go!
car is running


呐,你看go go go!前后都有后添加的动作吧。


2、获取被CGLIB代理的对象

这部分内容参考了博文(http://blog.csdn.net/liufeng520/article/details/43563161)

我们先通过debug看一下被代理对象被藏在哪里了

Spring AOP demo 和获取被CGLIB代理的对象_第1张图片


好,看完了,我们来写一下代码,如下:


import java.lang.reflect.Field;

import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by qm on 17/2/15.
 */
public class SpringAopTest {

    public static class Car {
        public void go() {
            System.out.println("go go go!");
        }
    }

    public static class CarLogger {

        public void beforeRun() {
            System.out.println("car is going to run");
        }

        public void afterRun() {
            System.out.println("car is running");
        }
    }

    public static Object getTarget(Object beanInstance) {
        if (!AopUtils.isAopProxy(beanInstance)) {
            return beanInstance;
        } else if (AopUtils.isCglibProxy(beanInstance)) {
            try {
                Field h = beanInstance.getClass().getDeclaredField("CGLIB$CALLBACK_0");
                h.setAccessible(true);
                Object dynamicAdvisedInterceptor = h.get(beanInstance);

                Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
                advised.setAccessible(true);

                Object target = ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
                return target;
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;

    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop-test.xml");
        Car car = (Car) context.getBean("car");
        Object target = getTarget(car);
        System.out.println(target == car);
        System.out.println(car.getClass());
        System.out.println(target.getClass());
        car.go();
    }
}






你可能感兴趣的:(JAVA,spring,反射,代理)