一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产91在线精品 | ady久久 | 欧美成人免费观看bbb | 天天久久综合 | 亚洲欧美精品一区二区 | 4444kk在线看片 | 国产a免费 | 亚洲国产麻豆 | 女人与d0gxxx| 免费亚洲视频在线观看 | 精品国产区一区二区三区在线观看 | 日韩欧美一区二区三区视频 | 九九大香尹人视频免费 | 16男男gaygays | 超强台风免费观看完整版视频 | 4hc44四虎www在线影院男同 | 高h短篇辣肉各种姿势bl | 国产一区二区三区福利 | 亚洲高清一区二区三区久久 | 国产日产韩产麻豆1区 | 日本老师动漫 | 俄罗斯图书馆无打码久久 | 色综合合久久天天综合绕视看 | 丝袜兔女郎被啪在线观看91 | 扒开老女人 | 亚洲欧美日韩综合一区久久 | 亚洲福利一区二区 | 亚洲国产美女精品久久久久 | 天堂欧美 | 欧美性野久久久久久久久 | 香蕉视频在线观看网址 | 91视频国产自拍 | 性欧美sexovideotv | 污污的动态图合集 | sex5·性屋娱乐 | 青青草成人在线 | 久久99r66热这里有精品 | 精品欧美一区二区三区久久久 | 久久久无码精品无码国产人妻丝瓜 | 大陆黄色片| 亚洲尿尿 |