日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > Java的動態代理機制及Spring的實現方式

Java的動態代理機制及Spring的實現方式

來源:程序員人生   發布時間:2017-01-23 20:17:39 閱讀次數:2697次

學習Java的同學注意了!!! 
學習進程中遇到甚么問題或想獲得學習資源的話,歡迎加入Java學習交換群,群號碼:183993990  我們1起學Java!


JAVA 代理實現

代理的實現分動態代理和靜態代理,靜態代理的實現是對已生成了的JAVA類進行封裝。

動態代理則是在運行時生成了相干代理累,在JAVA中生成動態代理1般有兩種方式。

JDK自帶實現方法

JDK實現代理生成,是用類 java.lang.reflect.Proxy, 實現方式以下

EX:

public class JDKProxy {

      public static Object getPoxyObject(final Object c) {

            return Proxy.newProxyInstance(c.getClass().getClassLoader(), c.getClass().getInterfaces(),// JDK實現動態代理,但JDK實現必須需要接口

                        new InvocationHandler() {

                             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                    // TODO Auto-generated method stub

                                    Object reObj = null;

                                    System.out.print("you say: ");

                                    reObj = method.invoke(c, args);

                                    System.out.println(" [" + Calendar.getInstance().get(Calendar.HOUR) + ":"

                                                + Calendar.getInstance().get(Calendar.MINUTE) + " "

                                                + Calendar.getInstance().get(Calendar.SECOND) + "]");

                                    return reObj;

                              }

                        });

      }

}

測試代理類方法

public class TestForPoxy {

      public static void main(String[] args) {

            ServiceTest service = new ServiceTestImpl();

            System.out.println(service.getClass().getSimpleName());

            ServiceTest poxyService = (ServiceTest) JDKProxy.getPoxyObject(service);

            System.out.println(poxyService.getClass().getSuperclass());

            poxyService.saySomething("hello,My QQ code is 107966750.");

            poxyService.saySomething("what 's your name?");

            poxyService.saySomething("only for test,hehe.");

      }

}

1, Proxy實現代理的目標類必須有實現接口

2, 生成出來的代理類為接口實現類,和目標類不能進行轉換,只能轉為接口實現類進行調用

明顯特點:通過此方法生成出來的類名叫做 $Proxy0

用CGLIB包實現

CGLIB是1個開源項目,官方網址是:http://cglib.sourceforge.net/,可以去上面下載最新JAR包,

本項目用的是cglib⑶.0.jar

本項目還加入了依賴JAR包asm⑷.0.jar,asm-util⑷.0.jar

實現方式以下

EX:

public class CGLIBProxy {

      public static Object getPoxyObject(Object c) {

            Enhancer enhancer = new Enhancer();

            enhancer.setSuperclass(c.getClass());

            enhancer.setCallback(new MethodInterceptor() {

                  public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy proxy) throws Throwable {

                        System.out.print("you say: ");

                        proxy.invokeSuper(arg0, arg2);

                        System.out.println(" [" + Calendar.getInstance().get(Calendar.HOUR) + ":"

                                    + Calendar.getInstance().get(Calendar.MINUTE) + " " + Calendar.getInstance().get(Calendar.SECOND)

                                    + "]");

                        return null;

                  }

            });

            return enhancer.create();

      }

}

測試代理類方法

public class TestForPoxy {

      public static void main(String[] args) {

            ServiceTest service = new ServiceTestImpl();

            System.out.println(service.getClass().getSimpleName());

//          ServiceTest poxyService = (ServiceTest) JDKProxy.getPoxyObject(service);

            ServiceTest poxyService = (ServiceTest) CGLIBProxy.getPoxyObject(service);

            System.out.println(poxyService.getClass().getSuperclass());

            poxyService.saySomething("hello,My QQ code is 107966750.");

            poxyService.saySomething("what 's your name?");

            poxyService.saySomething("only for test,hehe.");

      }

}

 

1, CGLIB實現方式是對代理的目標類進行繼承

2, 生成出了的代理類可以沒方法,生成出來的類可以直接轉換成目標類或目標類實現接口的實現類,因JAVA向上轉換

明顯特點:通過輸出看出,看誕生成出的代理類的parent類為代理的目標類

 

Spring  AOP的代理類機制分析

 

在spring中,bean都是由動態代理生成出來的,那末究竟是用JDK的Proxy類實現呢,還是用CGLIB方式實現呢。

AOP  Spring需要的依賴JAR包有:

spring-asm⑶.2.0.M1.jar

spring-beans⑶.2.0.M1.jar

spring-context⑶.2.0.M1.jar

spring-core⑶.2.0.M1.jar

spring-expression⑶.2.0.M1.jar

spring-aop⑶.2.0.M1.jar

spring-aspects⑶.2.0.M1.jar

commons\commons-logging⑴.1.1\commons-logging⑴.1.1.jar

aopalliance\aopalliance.jar

lib\aspectjweaver.jar

 

實現AOP

 

先簡單的實現AOP

 

配置以下

 

<?xml version="1.0" encoding="utf⑻"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd

      http://www.springframework.org/schema/aop 

    http://www.springframework.org/schema/aop/spring-aop⑶.2.xsd">

      <bean id="test" class="org.ben.spring.service.Test" />

      <bean id="aspectBean" class="org.ben.spring.TestAspect" />

      <!-- 對Test類進行AOP攔截 -->

      <aop:config>

            <aop:aspect id="TestAspect" ref="aspectBean">

                  <!--配置切面-->

                  <aop:pointcut id="businessService"

                        expression="execution(* org.ben.spring.service.Test.say(..))" />

                  <aop:before pointcut-ref="businessService" method="doBefore" />

                  <aop:after pointcut-ref="businessService" method="doAfter" />

            </aop:aspect>

      </aop:config>

</beans>

 

 

然落后行運行結果以下,表示AOP攔截成功

AOP測試類

public class TestBeans {

      public static void main(String[] args) {

            ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");

            Test test=(Test) ctx.getBean("test");

            System.out.println(test.getClass().getSimpleName());

            test.say();

      }

}

輸出:

do something in befor

welcome for test

do something in after

打印代理類的生成方式

 

第1種情況, Test不實現任何接口,代碼以下

public class Test {

      public void say() {

            System.out.println("welcome for test,My QQ is 107966750");

      }

}

 

在TestBeans中加入打印當前對象的名稱

以下:

ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");

Test test=(Test) ctx.getBean("test");

System.out.println(test.getClass().getSimpleName());

test.say();

輸出:

Test$$EnhancerByCGLIB$$4791b36c

super class is class org.ben.spring.service.Test

do something in befor

welcome for test

do something in after

 

明顯看到用了AOP以后,輸出的是代理類對象Test$$EnhancerByCGLIB$$bb9b6a7c.而且它的父類是我們的代理目標類。說明是有CGLIB生成的

 

 

 

 

 

第2種情況

 

XML的配置不變,改變代理目標類Test的實現方法,以下

public class Test implements TestInter{

      public void say() {

            System.out.println("welcome for test,My QQ is 107966750");

      }

}

和原來不同的是多繼承了1個接口,接口中定義了say()方法

在TestBeans中加入打印當前對象的名稱

以下:

ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");

TestInter test=(TestInter) ctx.getBean("test");

System.out.println(test.getClass().getSimpleName());

System.out.println("super class is "+test.getClass().getSuperclass());

test.say();

 

輸出:

$Proxy0

super class is class java.lang.reflect.Proxy

do something in befor

welcome for test,My QQ is 107966750

do something in after

結論

Spring AOP中,當攔截對象實現了接口時,生成方式是用JDK的Proxy類。當沒有實現任何接口時用的是GCLIB開源項陌生成的攔截類的子類.

 附上本文測試的源碼內容  源碼下載

學習Java的同學注意了!!! 
學習進程中遇到甚么問題或想獲得學習資源的話,歡迎加入Java學習交換群,群號碼:183993990  我們1起學Java!

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产成人av一区二区三区 | av在线中文| 色婷婷粉嫩av | 天天插日日干 | 夜夜春精品视频高清69式 | 黄色毛片小视频 | 18成人在线观看 | 日韩欧美片 | 欧美性猛交xxxx乱大交退制版 | 亚洲乱码精品 | 欧美一区二区三区大片 | 国产福利小视频 | 日韩在线观看中文字幕 | 高清一区二区三区 | 99久久精品毛片免费播放高清 | 欧美天堂视频 | 成人国产| 717影视三级理论电影在线播放 | 亚洲成人免费在线观看 | 欧美视频二区 | 日本久久久一区二区三区 | 国产欧美久久一区二区三区 | 麻豆传媒在线播放 | 国产日韩视频 | 欧美中文在线观看 | 日韩av在线免费看 | 欧美成人手机在线 | 国产精品久久久久久久久久久久午夜片 | 亚洲一级免费视频 | 日韩欧美不卡视频 | 欧美色综合天天久久综合精品 | 日本精品在线视频 | 玖玖国产| 国产黄色一级电影 | 久久久噜噜噜久久中文字幕色伊伊 | 亚洲精品视频二区 | 欧美国产免费 | 亚洲欧美激情视频 | 国产欧美日韩综合 | 免费国产视频 | 欧美日韩另类一区 |