java代理模式
代理模式分为:静态代理、动态代理(jdk代理)、Cglib代理
静态代理跟动态代理:需要目标对象有实现接口
cglib代理:适用目标对象没有实现接口的代理
直接上例子:
静态代理:
public interface ISinger {
public void sing();
}
public class Singer implements ISinger {
@Override
public void sing() {
System.out.println("我是歌手,我要唱歌");
}
}
/**
* 静态代理
*/
public class SingerProxy implements ISinger{
private ISinger target;
public SingerProxy(ISinger singer){
this.target=singer;
}
@Override
public void sing() {
System.out.println("演唱开始....");
target.sing();//静态代理的关键,调用目标对象的方法
System.out.println("演唱结束....");
}
}
/**
* 静态代理测试
*/
public class TestSinger {
public static void main(String[] args) {
ISinger targer=new Singer();
SingerProxy proxy=new SingerProxy(targer);
proxy.sing();
}
}
动态代理:
public interface Disinger {
public void sing();
}
public class DSinger implements Disinger {
@Override
public void sing() {
System.out.println("动态代理实现,我是歌手,我要歌唱");
}
}
package com.design.proxy.dynamic;
import com.design.proxy.motionless.ISinger;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 动态代理类-适用于有接口的类
*/
public class DynamicProxySinger implements InvocationHandler {
private Object target;
public DynamicProxySinger(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("start..");
Object object = method.invoke(target, args);
System.out.println("end...");
return object;
}
/**
* 获得代理对象
* @return
*/
public Object getProxy(){
Object object = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
return object;
}
}
package com.design.proxy.dynamic;
import com.design.proxy.dynamic.impl.DSinger;
import com.design.proxy.dynamic.impl.UImpl;
import org.junit.Test;
/**
* 动态代理测试
*/
public class DynamicTest {
@Test
public void sing(){
Disinger disinger = new DSinger();
DynamicProxySinger dynamicProxySinger=new DynamicProxySinger(disinger);
Disinger disingerProxy = (Disinger) dynamicProxySinger.getProxy();
disingerProxy.sing();
}
@Test
public void insert(){
U disinger = new UImpl();
DynamicProxySinger dynamicProxySinger=new DynamicProxySinger(disinger);
U uProxy = (U) dynamicProxySinger.getProxy();
uProxy.insert();
}
}
cglib代理:依赖spring-core
public class CglibSinger {
public void sing(){
System.out.println("中国好声音.....");
}
}
package com.design.proxy.cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;
/**
* cglib实现动态代理依赖spring-core
* 适用目标对象没有实现接口的情况
*/
public class CglibProxy implements MethodInterceptor {
private Object target;
public CglibProxy(Object target){
this.target=target;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("开始时间:"+new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date()));
Object object = method.invoke(target, objects);
Thread.sleep(2000);
System.out.println("结束时间:"+new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date()));
return object;
}
//获取代理对象
public Object getProxy(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this);
Object object = enhancer.create();
return object;
}
}
package com.design.proxy.cglib;
import org.junit.Test;
public class CglibTest {
@Test
public void sing(){
CglibSinger target=new CglibSinger();
CglibSinger proxySinger = (CglibSinger) new CglibProxy(target).getProxy();
proxySinger.sing();
}
}