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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java靜態(tài)代理和動(dòng)態(tài)代理總結(jié)

Java靜態(tài)代理和動(dòng)態(tài)代理總結(jié)

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

這篇文章主要介紹了Java靜態(tài)代理和動(dòng)態(tài)代理總結(jié),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

靜態(tài)代理

第一種實(shí)現(xiàn)(基于接口):

1》接口

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

2》目標(biāo)類,至少實(shí)現(xiàn)一個(gè)接口

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

3》代理類(與目標(biāo)類實(shí)現(xiàn)相同接口,從而保證功能一致)

?
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》測(cè)試

?
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");
 }
}

第二種實(shí)現(xiàn)(基于目標(biāo)類):

1>目標(biāo)類

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

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

?
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>測(cè)試

?
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");
 }
}

動(dòng)態(tài)代理

動(dòng)態(tài)代理的代理類是在程序運(yùn)行期間動(dòng)態(tài)生成的,也有兩種實(shí)現(xiàn),一種是JDK動(dòng)態(tài)代理,一種是CGLib動(dòng)態(tài)代理

1》JDK動(dòng)態(tài)代理(基于接口實(shí)現(xiàn),與目標(biāo)類實(shí)現(xiàn)相同接口,從而保證功能一致)

?
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: 代理對(duì)象
    * method: 目標(biāo)對(duì)象的方法對(duì)象
    * args: 目標(biāo)對(duì)象方法的參數(shù)
    * return: 目標(biāo)對(duì)象方法的返回值
    */
   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提取出來(lái),單獨(dú)寫一個(gè)類,為了方便大家看,這里我用內(nèi)部類的形式
  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動(dòng)態(tài)代理(基于目標(biāo)類,通過繼承目標(biāo)類,從而保證功能一致),需要導(dǎo)入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)目標(biāo)類

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

2)測(cè)試

?
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();
  //設(shè)置父類
  enhancer.setSuperclass(Hi.class);
  //設(shè)置回調(diào)函數(shù)
  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提取出來(lái),單獨(dú)寫一個(gè)類,為了方便大家看,這里我用內(nèi)部類的形式
  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靜態(tài)代理和動(dòng)態(tài)代理總結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 韩国漂亮美女三级在线观看 | 四虎影院在线免费 | 亚洲系列国产精品制服丝袜第 | 大香焦在线 | 玩乳h文奶水和尚 | 精品一区二区三区五区六区七区 | 精品国语对白精品自拍视 | 久久永久影院免费 | 欧美日韩在线观看区一二 | 亚洲精品色婷婷在线影院麻豆 | 好大好猛好深好爽视频 | 91传媒制片厂果冻有限公司 | 免费一级欧美片在线观免看 | 亚洲国产99 | 国产午夜精品一区二区三区 | 99免费精品视频 | 男女男精品视频网站 | swag最新正在播放 | 香蕉视频在线观看网址 | 好大好长好紧爽免费 | 欧美日韩综合一区 | 男人的天堂久久精品激情 | 国产一级网站 | 魔法满屋免费观看完整版中文 | 99久久久久国产精品免费 | 草莓视频丝瓜 | 亚洲免费一 | 欧美另类杂交a | 日本护士handjob | 激情偷拍网| 性xxxx18学生第一次出血 | 国产精品四虎在线观看免费 | 国产成人精品曰本亚洲78 | 香蕉 在线播放 | 好爽好紧小雪别夹小说 | gay帅老头毛都白了 gayxxx视频 | 精品伊人 | 国产在线观看色 | 久草在在线免视频在线观看 | 亚洲高清一区二区三区四区 | 国精视频一区二区视频 |