Feature encapsulation and Configuration:
Beanfactory of the factory class is responsible for creating instance objects of the target class or proxy class, and implementing switching through the configuration file. The getbean method returns a corresponding instance object based on the parameter string, if the class name corresponding to the parameter string in the configuration file is not proxyfactorybean, the instance object of this class is directly returned, and the object returned by the getproxy method of this object is returned.
Beanfactory constructor accepts the input stream object representing the configuration file. The configuration file format is as follows:
Export xxx=java.util.araylist;xxx=cn.itcast.proxyfactorybean;xxx.tar get = java. util. arraylist; XXX. d
Evice = cn. itcast. mydvice;
The proxybean acts as a dynamic proxy for encapsulation and survival. The configuration information required for the factory class is: Target and notification.
Object OBJ = beanfactory. getbean ("XXX"); it means that there is a factory beanfactory that generates the factory of JavaBean, and there is a getbean (string name) method, create a JavaBean object based on the name, which is configured in the configuration file.# Xxx = Java. util. araylist; input # XXX tells spring to create a Java for me. util. arraylist instance object. If getbean finds that it is not a special class such as proxyfactorybean, it creates such an instance object, Else. Instead of returning its instance object, it calls the method in the proxyfactorybean class, create a proxy.
Dynamically generate internal classCodeAnalysis:
The dynamically generated class implements the collection interface (multiple interfaces can be implemented). The generated class includes all the methods in the collection interface and a constructor that accepts the invocationhandler parameter, the constructor receives an invocationhandler object. Why do I need to define an interface and reference it in one place? I need to use the interface when I don't know the name!
The principle knowledge of Spring framework is the proxy. The core code is implemented as follows: