Java improvement class (VI) reflection and dynamic proxy (JDK proxy and cglib)
•
Java
1、 Reflection
1. Get class object
2. Common methods of class
// 此段代码为公共代码
interface People {
int parentAge = 18;
public void sayHi(String name);
}
class PeopleImpl implements People {
private String privSex = "男";
public String race = "汉族";
@Override
public void sayHi(String name) {
System.out.println("hello," + name);
}
private void prvSayHi() {
System.out.println("prvSayHi~");
}
public static void getSex() {
System.out.println("18岁");
}
}
3.1 static method call
// 核心代码(省略了抛出异常的声明)
public static void main(String[] args) {
Class myClass = Class.forName("example.PeopleImpl");
// 调用静态(static)方法
Method getSex = myClass.getmethod("getSex");
getSex.invoke(myClass);
}
3.2 general method call
Class myClass = Class.forName("example.PeopleImpl");
Object object = myClass.newInstance();
Method method = myClass.getmethod("sayHi",String.class);
method.invoke(object,"老王");
3.3 calling private methods
Class myClass = Class.forName("example.PeopleImpl");
Object object = myClass.newInstance();
Method privSayHi = myClass.getDeclaredMethod("privSayHi");
privSayHi.setAccessible(true); // 修改访问限制
privSayHi.invoke(object);
4. Summary
2、 Dynamic agent
1. JDK proxy dynamic proxy
interface Animal {
void eat();
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("The dog is eating");
}
}
class Cat implements Animal {
@Override
public void eat() {
System.out.println("The cat is eating");
}
}
// JDK 代理类
class AnimalProxy implements InvocationHandler {
private Object target; // 代理对象
public Object getInstance(Object target) {
this.target = target;
// 取得代理对象
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {
System.out.println("调用前");
Object result = method.invoke(target,args); // 方法调用
System.out.println("调用后");
return result;
}
}
public static void main(String[] args) {
// JDK 动态代理调用
AnimalProxy proxy = new AnimalProxy();
Animal dogProxy = (Animal) proxy.getInstance(new Dog());
dogProxy.eat();
}
@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)
throws IllegalArgumentException
{
// 省略其他代码
* @param loader the class loader to define the proxy class
* @param interfaces the list of interfaces for the proxy class to implement
2. Cglib dynamic proxy
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.9</version>
</dependency>
class Panda {
public void eat() {
System.out.println("The panda is eating");
}
}
class cglibProxy implements MethodInterceptor {
private Object target; // 代理对象
public Object getInstance(Object target) {
this.target = target;
Enhancer enhancer = new Enhancer();
// 设置父类为实例类
enhancer.setSuperclass(this.target.getClass());
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}
@Override
public Object intercept(Object o,Object[] objects,MethodProxy methodProxy) throws Throwable {
System.out.println("调用前");
Object result = methodProxy.invokeSuper(o,objects); // 执行方法调用
System.out.println("调用后");
return result;
}
}
public static void main(String[] args) {
// cglib 动态代理调用
cglibProxy proxy = new cglibProxy();
Panda panda = (Panda)proxy.getInstance(new Panda());
panda.eat();
}
3、 JDK proxy vs cglib
4、 Reference documents
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码