激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java靜態代理和動態代理總結

Java靜態代理和動態代理總結

2020-08-14 15:52LZHL Java教程

這篇文章主要介紹了Java靜態代理和動態代理總結,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

靜態代理

第一種實現(基于接口):

1》接口

?
1
2
3
public interface Hello {
 void say(String msg);
}

2》目標類,至少實現一個接口

?
1
2
3
4
5
public class HelloImpl implements Hello {
 public void say(String msg) {
  System.out.println("Hi,"+msg);
 }
}

3》代理類(與目標類實現相同接口,從而保證功能一致)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class HelloProxy implements Hello{
 private Hello hello;
 public HelloProxy(Hello hello){
  this.hello = hello;
 }
 public void say(String msg){
  before();
  hello.say(msg);
  after();
 }
 private void before(){
  System.out.println("Before");
 }
 private void after(){
  System.out.println("After");
 }
}

3》測試

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * @Author LZHL
 * @Create 2017-02-19 10:26
 * @Description
 */
public class Main {
 public static void main(String[] args) throws Exception {
  HelloImpl target = new HelloImpl();
  HelloProxy proxy = new HelloProxy(target);
  proxy.say("LZHL");
 }
}

第二種實現(基于目標類):

1>目標類

?
1
2
3
4
5
public class HelloTarget {
 public void sayHello(String name){
  System.out.println("Hi,"+name);
 }
}

2>代理類(通過繼承目標類,保證功能一致)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class HelloProxy extends HelloTarget{
  private HelloTarget target;
  public HelloProxy(HelloTarget target){
    this.target = target;
  }
  @Override
 public void sayHello(String name) {
  this.before();
  target.sayHello(name);
  this.after();
 }
 private void before(){
  System.out.println("Before");
 }
 private void after(){
  System.out.println("After");
 }
}

3>測試

?
1
2
3
4
5
6
7
public class Main {
 public static void main(String[] args) throws Exception {
  HelloTarget target = new HelloTarget();
    HelloProxy proxy= new HelloProxy(target);
  proxy.sayHello("LZHL");
 }
}

動態代理

動態代理的代理類是在程序運行期間動態生成的,也有兩種實現,一種是JDK動態代理,一種是CGLib動態代理

1》JDK動態代理(基于接口實現,與目標類實現相同接口,從而保證功能一致)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * @Author LZHL
 * @Create 2017-02-19 12:46
 * @Description
 */
public class Main {
 public static void main(String[] args){
  final HelloImpl target = new HelloImpl();
  Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
   /*
    * proxy: 代理對象
    * method: 目標對象的方法對象
    * args: 目標對象方法的參數
    * return: 目標對象方法的返回值
    */
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before");
    Object retValue = method.invoke(target, args);
    System.out.println("after");
    return retValue;
   }
  });
  Hello proxy = (Hello) proxyInstance;
  proxy.say("LYX");
  //可以把InvocationHandler提取出來,單獨寫一個類,為了方便大家看,這里我用內部類的形式
  class JDKProxy implements InvocationHandler {
   private Object target;
   public JDKProxy(Object target){
    this.target = target;
   }
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    before();
    Object result = method.invoke(target, args);
    after();
    return result;
   }
   private void before(){
    System.out.println("Before");
   }
   private void after(){
    System.out.println("After");
   }
  }
  InvocationHandler ih = new JDKProxy(target);
  Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih);
  Hello proxy2 = (Hello) proxyInstance2;
  proxy2.say("LZHL");
 }
}

2》CGLib動態代理(基于目標類,通過繼承目標類,從而保證功能一致),需要導入cglib-3.2.4.jar包

pom.xml

?
1
2
3
4
5
6
7
8
<dependencies>
 <!-- https://mvnrepository.com/artifact/cglib/cglib -->
 <dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>3.2.4</version>
 </dependency>
</dependencies>

1)目標類

?
1
2
3
4
5
public class Hi {
 public void sayHi(String msg){
  System.out.println("Hi,"+msg);
 }
}

2)測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * @Author LZHL
 * @Create 2017-02-19 13:19
 * @Description
 */
public class Main {
 public static void main(String[] args) {
  Enhancer enhancer = new Enhancer();
  //設置父類
  enhancer.setSuperclass(Hi.class);
  //設置回調函數
  enhancer.setCallback(new MethodInterceptor() {
   public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    System.out.println("before");
    Object retValue = methodProxy.invokeSuper(target, args);
    System.out.println("after");
    return retValue;
   }
  });
  Object proxy = enhancer.create();
  Hi hi = (Hi) proxy;
  hi.sayHi("LXY");
  //可以把MethodInterceptor提取出來,單獨寫一個類,為了方便大家看,這里我用內部類的形式
  class CGLibProxy implements MethodInterceptor {
   public <T> T getProxy(Class<T> clazz){
    return (T) Enhancer.create(clazz, this);
   }
   public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    before();
    Object result = proxy.invokeSuper(target, args);
    after();
    return result;
   }
   private void before(){
    System.out.println("Before");
   }
   private void after(){
    System.out.println("After");
   }
  }
  CGLibProxy cgLibProxy = new CGLibProxy();
  Hi hi2 = cgLibProxy.getProxy(Hi.class);
  hi2.sayHi("LZHL");
 }
}

以上所述是小編給大家介紹的Java靜態代理和動態代理總結,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

原文鏈接:http://www.cnblogs.com/lzhl/p/6416063.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产va在线观看 | 成年免费网站 | 操操电影| 最新中文字幕第一页视频 | 一级网站 | 成人午夜免费在线观看 | 国产麻豆交换夫妇 | 色淫影院| 性明星video另类hd | 一区二区免费看 | xx53xx| 91欧美视频| 久久精品国产99久久6动漫亮点 | 日本一级黄色大片 | 一区二区久久久久草草 | 国产午夜亚洲精品 | 成人爽a毛片免费啪啪红桃视频 | 毛片在线免费播放 | 黄色a级片视频 | 91色琪琪电影亚洲精品久久 | 精品国产一区二区三区在线观看 | 污片在线观看视频 | 国产午夜精品久久久久久久蜜臀 | 国产成人强伦免费视频网站 | 久草在线视频福利 | 性高跟鞋xxxxhd4kvideos | 国产一级不卡毛片 | 二区三区在线观看 | 羞羞的视频 | 国产精品久久久久久久久久10秀 | 久久美女色视频 | 欧洲精品久久久 | 国产精品中文在线 | 欧美日本一区二区 | 欧美成人免费tv在线播放 | av成人免费看 | 美女黄页网站免费进入 | 欧美精品免费一区二区三区 | 久久精品99北条麻妃 | 天天干天天透 | 1314成人网 |