This is the 8th day of my participation in Gwen Challenge
This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details
The challenge has lasted to the eighth day, today I feel a little tired, but I still continue to finish today’s learning task, today let’s talk about the agent mode
Proxy Pattern Proxy Pattern
Intent: Provide a proxy for other objects to control access to this object.
How do you understand the proxy pattern?
Generally speaking, the agency model is similar to the intermediary in our real life. For example, when we are looking for a job, some job agencies can help us find suitable positions. For example, we want to rent a house, but we don’t know where we are satisfied with the house, then we turn to the rental agent, they will provide us with many houses on hand for us to choose; From the perspective of the example of rent, mediation convenient we find houses, for the landlord, the landlord don’t want to stick his housing information through broader way and would lead to a lot of people looking for him, and sometimes to find he is not in order to rent a house, he wanted to expose information for people interested in renting, so through rental intermediary agent, can be more accurate to put housing information to specify the crowd, The landlord will not be disturbed as much
implementation
public class ProxyPatternDemo {
public static void main(String[] args) {
Tenement tenement = new ProxyTenement("Zhongguancun Room 1");
// For the first time, you need to obtain the housing information from the landlord
tenement.display();
System.out.println("");
// The listing information does not need to be obtained through the landlordtenement.display(); }}// Create a listing interface
interface Tenement {
void display(a);
}
/ / create
class RealTenement implements Tenement {
private String tenementName;
public RealTenement(String tenementName){
this.tenementName = tenementName;
loadFromDisk(tenementName);
}
@Override
public void display(a) {
System.out.println("Display housing information:" + tenementName);
}
private void loadFromDisk(String tenementName){
System.out.println("Obtain listing information from landlord:"+ tenementName); }}class ProxyTenement implements Tenement{
private RealTenement realTenement;
private String tenementName;
public ProxyTenement(String tenementName){
this.tenementName = tenementName;
}
@Override
public void display(a) {
// Judge whether the agent has mastered the housing information. If so, there is no need to seek the landlord
if(realTenement == null){
realTenement = newRealTenement(tenementName); } realTenement.display(); }}Copy the code
The results
Application in Spring
The proxy pattern is mainly embodied in AOP, one of the core features of the Spring framework, through which aspect notifications are woven into the specified pointcut location, which can be simplified by adding specified functionality before and after a method is run. The following is a simple implementation that weaves the interceptor onto the specified method. When using Spring AOP, ProxyFactoryBean needs to be configured, and then ac.getBean(bean ID) is used to get the object of ProxyFactoryBean proxy. While using ProxyFactoryBean ProxyFactoryBean class. GetObject () method to obtain the object returned, namely proxy objects
Here is the implementation of the core method getObject() of the ProxyFactoryBean class
@Nullable
public Object getObject(a) throws BeansException {
this.initializeAdvisorChain();
if (this.isSingleton()) {
return this.getSingletonInstance();
} else {
if (this.targetName == null) {
this.logger.info("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
}
return this.newPrototypeInstance(); }}Copy the code
In the getObject() method, getSingletonInstance() and newPrototypeInstance() are primarily called;
In the Spring configuration, beans generated by the Spring proxy are singleton objects if nothing is done. If you modify scope, create new prototype objects one at a time.
Spring uses dynamic proxies to implement AOP with two very important classes, the JdkDynamicAopProxy class and the CglibAopProxy class
Today’s summary
Looking at the proxy pattern today, it’s simple from the case, but when it’s applied to Spring, it’s pretty confusing at first, but keep a few key things in mind, Such as proxy mode ->Spring AOP -> getObject() and JdkDynamicAopProxy class and CglibAopProxy class, comprehensive knowledge in the actual practice again step by step to go down, well, today again Get a new design pattern, bye.