国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring AOP demo 和獲取被CGLIB代理的對象

2019-11-08 18:22:59
字體:
來源:轉載
供稿:網友

本文分為兩部分:1)給出SPRing AOP的一個例子(會使用CGLIB代理);2)給出獲取被CGLIB代理的原始對象。

1、Spring AOP Demo

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

1.1添加maven依賴

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>4.0.5.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-beans</artifactId>    <version>4.0.5.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>4.0.5.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>    <version>4.0.5.RELEASE</version></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjweaver</artifactId>    <version>1.8.1</version></dependency>

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目錄下也就行了。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >        <bean id="car" class="com.SpringAopTest.Car"/>        <bean id="logger" class="com.SpringAopTest.CarLogger" />        <aop:config>            <aop:aspect ref="logger">                <aop:pointcut expression="execution(* com.SpringAopTest.Car.go(..))" id="go"/>                <aop:before pointcut-ref="go" method="beforeRun" />                <aop:after pointcut-ref="go" method="afterRun" />            </aop:aspect>        </aop:config>    </beans></beans>1.4運行main函數

得到如下結果:

car is going to rungo go go!car is running

吶,你看go go go!前后都有后添加的動作吧。

2、獲取被CGLIB代理的對象

這部分內容參考了博文(http://blog.csdn.net/liufeng520/article/details/43563161)

我們先通過debug看一下被代理對象被藏在哪里了抓狂

好,看完了,我們來寫一下代碼,如下:

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石河子市| 建平县| 自贡市| 永登县| 永靖县| 武宣县| 永宁县| 崇文区| 浦县| 石城县| 西藏| 长沙市| 即墨市| 南昌县| 墨玉县| 新兴县| 杭锦旗| 久治县| 民丰县| 嘉鱼县| 凉山| 昆山市| 鄱阳县| 韶关市| 吉安县| 卫辉市| 吴堡县| 右玉县| 黄山市| 五台县| 阿巴嘎旗| 湘西| 肥东县| 诸城市| 顺义区| 合山市| 疏勒县| 衡阳市| 北票市| 玉林市| 遵义市|