Knowledge points sorting
reflection
Understand the usage of reflection:
reflection one
reflection two
practice
public class Servant {
private int a;
private String str;
public Servant() {
}
public Servant(String string,int _a) {
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getStr() {
return str;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("-----------------反射基本使用-------------------");
//1获取类对象 3种方式
Servant servant = new Servant();
Class<?> clazz = servant.getClass(); //方式1
Class<?> clazz1 = Servant.class; //方式2
try {
Class<?> clazz2 = Class.forName("Reflection.Servant"); //方式3
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
//创建对象
try {
Class<?> clazz3 = Servant.class;
Servant servant1 = (Servant)clazz3.getDeclaredConstructor().newInstance();
servant1.setA(10);
System.out.println(servant1.getA());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
/*************************************************************************************/
System.out.println("获取构所有构造函数---------------");
Class<?> clazz4 = Servant.class;
Constructor[] constructors = clazz4.getDeclaredConstructors();
for (Constructor constructor:constructors) {
System.out.println(constructor);
}
try {
System.out.println("获取指定构造函数---------------");
Class<?> clazz5 = Servant.class;
Constructor constructor = clazz5.getDeclaredConstructor(String.class,int.class);
System.out.println(constructor);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
/*************************************************************************************/
System.out.println("获取类中所有的方法---------------");
Class<?> clazz6 = Servant.class;
Method[] methods = clazz6.getMethods();
for (Method method:methods) {
System.out.println(method);
}
System.out.println("获取指定方法----------------");
try {
Method method = clazz6.getDeclaredMethod("setA",int.class);
method.setAccessible(true);
System.out.println(method);
method.setAccessible(false);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
/*************************************************************************************/
System.out.println("获取类中所有成员变量------------------");
Class<?> clazz7 = Servant.class;
Field[] fields = clazz7.getDeclaredFields();
for (Field field:fields) {
System.out.println(field);
}
System.out.println("获取类中指定成员变量--------------");
Class<?> clazz8 = Servant.class;
try {
Field field = clazz8.getDeclaredField("str");
System.out.println(field);
Servant servant1 = new Servant();
field.setAccessible(true);
field.set(servant1,"12345678");
System.out.println(field.getName()+" "+servant1.getStr());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Agency model
Definition: The proxy mode is to provide a proxy object to the target object, and the proxy object controls the reference of the target object. In layman's terms: it is a common intermediary in our lives.
Purpose:
(1) Indirect access to the target object by introducing proxy objects to prevent unnecessary complexity caused by direct access to the target object;
(2) Enhance the original business through proxy objects.
The proxy mode is divided into static proxy and dynamic proxy
Static proxy
Abstract role:
define the public external method of agent role and real role.
Real role:
realize abstract role and define the business logic to be implemented by real role for the agent role to call.
Agent role:
Realize the abstract role, which is a real agent. The abstract method is realized through the business logic method of the real role, and you can attach your own operations.
Static proxy: The relationship between the proxy and the proxy is determined during the compilation of the program
接口: Start
Start ->实现 RealStart
Start ->实现 ProxyStart
ProxyStart 是代理类包含RealStart
public interface Start {
public void bookTicket();
public void Config();
public void CollectMoney();
public void sing();
}
public class RealStart implements Start{
@Override
public void bookTicket() {
System.out.println("RealStart bookTicket");
}
@Override
public void Config() {
System.out.println("RealStart Config");
}
@Override
public void CollectMoney() {
System.out.println("RealStart CollectMoney");
}
@Override
public void sing() {
System.out.println("RealStart sing");
}
}
public class ProxyStart implements Start {
private Start start;
public ProxyStart(Start _start) {
start=_start;
}
@Override
public void bookTicket() {
System.out.println("ProxyStart bookTicket");
}
@Override
public void Config() {
System.out.println("ProxyStart Config");
}
@Override
public void CollectMoney() {
System.out.println("ProxyStart CollectMoney");
}
@Override
public void sing() {
start.sing();
}
}
public static void main(String[] args) {
System.out.println("-------------简单代理--------------");
Start realStart = new RealStart();
Start proxyStart = new ProxyStart(realStart);
proxyStart.CollectMoney();
proxyStart.bookTicket();
proxyStart.sing();
}
Dynamic proxy
The advantages of dynamic agents compared to static agents:
only through interfaces to create agent classes, not through classes to create agent classes.
Dynamic agents use reflection mechanisms to be less efficient than static agents. The relationship between the agent and the agent is determined when the program is running.
Implementation of InvocationHandler (processor interface):
steps:
1. Real object interface
2. Create real object to implement interface
3. Create object to implement InvocationHandler interface
4. Pass Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Shop .class},invocationHandler); Realize proxy
Parameter:
ClassLoader class is loaded to
Class<?>[] interfaces The interface of the proxy class
InvocationHandler The class that implements InvocationHandler
public interface Shop {
void buy();
}
public class Real implements Shop {
@Override
public void buy() {
System.out.println("--买东西--");
}
}
public class Invocation implements InvocationHandler {
public Shop shop;
public Invocation(Shop _shop) {
shop =_shop;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始海外代购");
Object object = method.invoke(shop,args);
System.out.println("海外代购完成");
return object;
}
}
public class Main {
public static void main(String[] args) {
Shop shop = new Real();
InvocationHandler invocationHandler = new Invocation(shop);
Shop proxy = (Shop) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),new Class[]{
Shop.class},invocationHandler);
proxy.buy();
}
}
例如2:
public class MarkCompany implements InvocationHandler {
private Object factory;//真实对象
public void setFactory(Object factory) {
this.factory = factory;
}
public Object getFactory() {
return factory;
}
public Object getProxyInstance() {
//参数1 真实对象的类加载器 参数2 真实对象的接口 参数3 被代理的InvocationHandler
return Proxy.newProxyInstance(factory.getClass().getClassLoader(),factory.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object object = null;
Before();
object = method.invoke(factory,args);
After();
return object;
}
public void Before() {
System.out.println("开始行动");
}
public void After() {
System.out.println("行动完成");
}
}
public interface Shop {
public void shopeat();
}
public class Real implements Shop {
@Override
public void shopeat() {
System.out.println("代购");
}
}
public class Main {
public static void main(String[] args) {
Shop shop = new Real(); //真实对象
MarkCompany markCompany = new MarkCompany();
markCompany.setFactory(shop);
//代理对象1
Shop shop1 = (Shop) markCompany.getProxyInstance();
shop1.shopeat();
//代理对象2
//代理对象3
}
}