10. Agency mode
Why learn the agency model?
Because this is the bottom layer of SpringAOP, [server-side development SpringAOP and SpringMVC interview must ask]
Classification of agent mode:
- Static proxy
- Dynamic proxy
10.1 Static proxy
Role analysis:
- Abstract role: generally use interfaces or abstract classes to solve
- Real role: the role of the agent
- Acting role: Acting for the real role, after acting for the real role, we usually do some subsidiary operations
- Client: The person who visits the proxy object!
step:
-
interface
public interface Rent { public void rent(); }
-
Real character (landlord)
public class Host implements Rent{ public void rent() { System.out.println("房东有房子出租"); } }
-
Agent role
public class Proxy implements Rent{ private Host host; public Proxy() { } public Proxy(Host host) { this.host = host; } public void rent() { host.rent(); viewHouse(); contract(); fee(); } public void viewHouse(){ System.out.println("中介带租客去看房子"); } public void contract(){ System.out.println("中介负责打印合同签合同"); } public void fee(){ System.out.println("收取中介费"); } }
-
Customer access agent role
public class Client { public static void main(String[] args) { Host host = new Host(); Proxy proxy = new Proxy(host); proxy.rent(); } }
Benefits of the agency model:
- Can make the operation of real characters more pure! No need to pay attention to some public business
- Public business is left to the agent role! Realize the division of labor
- When public business expands, it is convenient for centralized management.
Disadvantages:
- A real role will produce an agent role; the amount of code will double and the development efficiency will be lower
10.2 Deepen understanding
Talk about AOP:
10.3, dynamic proxy
-
Dynamic agents have the same role as static agents
-
The proxy class of dynamic proxy is dynamically generated, not written directly by us.
-
Dynamic agents are divided into two categories: interface-based dynamic agents and class-based dynamic agents
- Based on interface: JDK dynamic proxy
- Based on class: cglib
- Java bytecode implementation: javasist
-
public interface InvocationHandler
InvocationHandler
It is an interface implemented by the call handler of the proxy instance .Each agent instance has an associated call handler. When a method is called on a proxy instance, the method call will be coded and dispatched to the
invoke
method of its call handler . -
Proxy
Provides static methods for creating dynamic proxy classes and instances. It is also the superclass of all dynamic proxy classes created by these methods.
Agency case:
1. Interface
package com.kuber.dynamicdemo1;
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
2. Real characters
package com.kuber.dynamicdemo1;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("执行add方法");
}
public void delete() {
System.out.println("执行delete方法");
}
public void update() {
System.out.println("执行update方法");
}
public void query() {
System.out.println("执行了query方法");
}
}
3. Acting role
package com.kuber.dynamicdemo1;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyInvocationHandler implements InvocationHandler {
/*被代理的接口*/
private Object target;
public void setTarget(Object target) {
this.target = target;
}
/*生成得到代理类*/
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
}
/*处理代理实例,并返回结果*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
log(method.getName());
Object result = method.invoke(target, args);
return result;
}
public void log(String msg){
System.out.println("【日志】"+msg+"方法执行");
}
}
4. Client access agent role
package com.kuber.dynamicdemo1;
public class Client {
public static void main(String[] args) {
/*真实角色*/
UserServiceImpl userService = new UserServiceImpl();
/*代理角色*/
ProxyInvocationHandler pih = new ProxyInvocationHandler();
/*设置要代理的对象*/
pih.setTarget(userService);
/*动态代理生成类*/
UserService proxy = (UserService)pih.getProxy();
proxy.query();
}
}
The benefits of dynamic agents:
- Can make the operation of real characters more pure! No need to pay attention to some public business
- Public business is left to the agent role! Realize the division of labor
- When public business expands, it is convenient for centralized management.
- A dynamic proxy class proxy is an interface, generally a corresponding type of business
- A dynamic proxy class can proxy multiple classes, as long as the same interface is implemented