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

來源:互聯網
上載者:User

標籤:ret   over   tin   ace   tps   XML   proxy   final   targe   

靜態代理

第一種實現(基於介面):

1》介面

public interface Hello {
    void say(String msg);
}

2》目標類,至少實現一個介面

public class HelloImpl implements Hello {
public void say(String msg) {
System.out.println("Hi,"+msg);
}
}

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

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》測試

/**
* @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>目標類

public class HelloTarget {
public void sayHello(String name){
System.out.println("Hi,"+name);
}
}

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

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>測試

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動態代理(基於介面實現,與目標類實現相同介面,從而保證功能一致)

/**
* @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

<dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>

</dependencies>

1)目標類

public class Hi {
public void sayHi(String msg){
System.out.println("Hi,"+msg);
}
}

2)測試

/**
* @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靜態代理和動態代理總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.