闽公网安备 35020302035485号
3.可以实现延迟加载,只有在需要时才创建实际对象。
2.可能会降低系统的性能,因为代理对象需要执行额外的操作。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 堆代码 duidaima.com
interface UserService {
void addUser(String username);
}
class UserServiceImpl implements UserService {
public void addUser(String username) {
System.out.println("Adding user: " + username);
}
}
class UserServiceProxy implements InvocationHandler {
private Object target;
public Object bind(Object target) {
this.target = target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Before method call");
Object result = method.invoke(target, args);
System.out.println("After method call");
return result;
}
}
public class Main {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
UserServiceProxy userServiceProxy = new UserServiceProxy();
UserService proxy = (UserService) userServiceProxy.bind(userService);
proxy.addUser("堆代码");
}
}
在上面的示例中,我们定义了一个UserService接口和一个实现类UserServiceImpl。然后,我们创建了一个UserServiceProxy类,实现了InvocationHandler接口,并在invoke方法中添加了额外的逻辑。最后用Proxy.newProxyInstance方法创建了代理对象,并将其绑定到实际对象上。import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
class UserService {
public void addUser(String username) {
System.out.println("Adding user: " + username);
}
}
class UserServiceInterceptor implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Before method call");
Object result = proxy.invokeSuper(obj, args);
System.out.println("After method call");
return result;
}
}
public class Main {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
enhancer.setCallback(new UserServiceInterceptor());
UserService proxy = (UserService) enhancer.create();
proxy.addUser("堆代码 ");
}
}
在上面的示例中,我们定义了一个UserService类,没有实现任何接口。然后,我们创建了一个UserServiceInterceptor类,实现了MethodInterceptor接口,并在intercept方法中添加了额外的逻辑。最后使用Enhancer类创建了代理对象,并设置了回调对象为UserServiceInterceptor。实现方式:Java提供了两种动态代理的实现方式,即基于接口的动态代理(JDK动态代理)和基于类的动态代理(CGLIB动态代理)。
实现方式:Java的反射机制主要通过Class、Field、Method等类来实现。
Class<?> clazz = Class.forName("com.example.MyClass");
2.通过类名的.class语法获取Class对象Class<?> clazz = MyClass.class;3.通过类加载器的loadClass方法获取Class对象
ClassLoader classLoader = MyClass.class.getClassLoader();
Class<?> clazz = classLoader.loadClass("com.example.MyClass");
4.通过对象的getClass方法获取Class对象MyClass obj = new MyClass();
Class<?> clazz = obj.getClass();
class MyClass {
public void sayHello(String name) {
System.out.println("Hello, " + name);
}
}
public class ReflectionExample {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("MyClass");
Object obj = clazz.newInstance();
Method method = clazz.getMethod("sayHello", String.class);
// Method method = clazz.getDeclaredMethod("sayHello", String.class);
method.invoke(obj, "堆代码");
} catch (Exception e) {
e.printStackTrace();
}
}
}
在上面的示例中,我们使用Class.forName方法通过类名获取Class对象。然后,使用newInstance方法创建类的实例。接下来,获取sayHello方法的Method对象,并使用invoke方法调用该方法,传递参数"堆代码"。