import java.util.concurrent.TimeUnit;
public interface CacheService {
String get(final String key);
void set(String key, String value);
void set(String key, String value, long timeout, TimeUnit timeUnit);
void del(String key);
}
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class CacheServiceImpl implements CacheService{
private RedisUtils redisUtils = new RedisUtils();
@Override
public String get(String key) {
return redisUtils.get(key);
}
@Override
public void set(String key, String value) {
redisUtils.set(key, value);
}
@Override
public void set(String key, String value, long timeout, TimeUnit
timeUnit) {
redisUtils.set(key, value, timeout, timeUnit);
}
@Override
public void del(String key) {
redisUtils.del(key);
}
}
import java.util.concurrent.TimeUnit;
public interface ICacheAdapter {
String get(String key);
void set(String key, String value);
void set(String key, String value, long timeout, TimeUnit timeUnit);
void del(String key);
}
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class EGMCacheAdapter implements ICacheAdapter {
private EGM egm = new EGM();
@Override
public String get(String key) {
return egm.gain(key);
}
@Override
public void set(String key, String value) {
egm.set(key, value);
}
@Override
public void set(String key, String value, long timeout, TimeUnit
timeUnit) {
egm.setEx(key, value, timeout, timeUnit);
}
@Override
public void del(String key) {
egm.delete(key);
}
}
import java.util.concurrent.TimeUnit;
public class EGM {
public String gain(String key) {
return "redis设置值";
}
public void set(String key, String value) {
System.out.println("设置成功!");
}
public void setEx(String key, String value, long timeout, TimeUnit timeUnit) {
}
public void delete(String key) {
}
}
import java.util.concurrent.TimeUnit;
public class RedisUtils {
public String get(String key) {
return null;
}
public void set(String key, String value) {
}
public void set(String key, String value, long timeout, TimeUnit timeUnit) {
}
public void del(String key) {
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class JDKProxy {
public static <T> T getProxy(Class<T> interfaceClass, ICacheAdapter cacheAdapter) throws Exception {
InvocationHandler handler = new JDKInvocationHandler(cacheAdapter);
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
Class<?>[] classes = interfaceClass.getInterfaces();
return (T) Proxy.newProxyInstance(classLoader, new Class[]{classes[0]},
handler);
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class JDKInvocationHandler implements InvocationHandler {
private ICacheAdapter cacheAdapter;
public JDKInvocationHandler(ICacheAdapter cacheAdapter) {
this.cacheAdapter = cacheAdapter;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return ICacheAdapter.class.getMethod(method.getName(),
getClazzByArgs(args)).invoke(cacheAdapter, args);
}
public static Class<?>[] getClazzByArgs(Object[] args) {
Class<?>[] classes = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
classes[i] = args[i].getClass();
}
return classes;
}
}
public class AbstractFactoryTest {
public static void main(String[] args) throws Exception {
CacheService proxy_EGM = JDKProxy.getProxy(CacheServiceImpl.class, new
EGMCacheAdapter());
proxy_EGM.set("user_name_01","?傅哥");
String val01 = proxy_EGM.get("user_name_01");
System.out.println(val01);
}
}
我在debug的时候发现会出现 Method threw ‘java.lang.NullPointerException’ exception. Cannot evaluate com.sun.proxy.$Proxy0.toString()但是不影响程序的正常运行,应该是由于动态代理的时候生成的对象可能并没有toString()方法,而debug模式下是通过toString来打印信息的。所以也解释了为什么在invoke方法中可以返回真实的代理对象
可参考
https://blog.csdn.net/qq_21040559/article/details/118541395
https://blog.csdn.net/zixiao_YING/article/details/79358708
|