package com.ietree.basicskill.designpattern.dynamicproxy.jdk;
/**
* 接口类
*
* @author Root
*/
public interface ISomeService {
String doFirst();
void doSecond();
}
package com.ietree.basicskill.designpattern.dynamicproxy.jdk;
/**
* 实现类
*
* @author Root
*/
public class SomeServiceImpl implements ISomeService {
@Override
public String doFirst() {
System.out.println("执行doFirst()...");
String result = "abcde";
return result;
}
@Override
public void doSecond() {
System.out.println("执行doSecond()...");
}
}
package com.ietree.basicskill.designpattern.dynamicproxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Main {
public static void main(String[] args) {
final ISomeService target = new SomeServiceImpl();
// 使用JDK的Proxy动态代理,要求目标类和代理类必须实现相同的接口,因为其底层的执行原理与静态代理的相同
ISomeService service = (ISomeService) Proxy.newProxyInstance(
// 目标类的类加载器
target.getClass().getClassLoader(),
// 目标类所实现的所有接口
target.getClass().getInterfaces(),
new InvocationHandler() {
// proxy:代理对象
// method:目标方法
// args:目标方法的参数列表
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 调用目标方法
Object result = method.invoke(target, args);
if (result != null) {
result = ((String) result).toUpperCase();
}
return result;
}
});
String result = service.doFirst();
System.out.println(result);
service.doSecond();
}
}
package com.ietree.basicskill.designpattern.dynamicproxy.cglib;
/**
* 实现类
*
* @author Root
*/
public class SomeService {
public String doFirst() {
System.out.println("执行doFirst()...");
String result = "abcde";
return result;
}
public void doSecond() {
System.out.println("执行doSecond()...");
}
}
package com.ietree.basicskill.designpattern.dynamicproxy.cglib;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class MyCglibFactory implements MethodInterceptor {
private SomeService target;
public MyCglibFactory() {
super();
target = new SomeService();
}
public SomeService myCglibCreator() {
// 创建增强器对象
Enhancer enhancer = new Enhancer();
// 指定目标类,即父类
enhancer.setSuperclass(SomeService.class);
// 设置回调接口对象
enhancer.setCallback(this);
return (SomeService) enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// 调用目标方法
Object result = method.invoke(target, args);
if (result != null) {
result = ((String) result).toUpperCase();
}
return result;
}
}
package com.ietree.basicskill.designpattern.dynamicproxy.cglib;
public class Main {
public static void main(String[] args) {
SomeService service = new MyCglibFactory().myCglibCreator();
String result = service.doFirst();
System.out.println("result = " + result);
service.doSecond();
}
}
执行doFirst()... result = ABCDE 执行doSecond()...
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有