The core of the Spring framework is the inversion of control and dependency injection.
The core of the Spring framework is the inversion of control and dependency injection.
When IOC is used, the object passively accepts the dependency class, rather than actively looking for it. The container actively Injects its dependent classes into it during instantiation. It can be understood as follows: Control reversal transfers the initiative of the class to the interface, and dependency injection injects the dependent class during class instantiation through the xml configuration file. Gradually understand through the following examples:
assume that class business needs to call the dependency method F () according to the daily practice, the following Code is obtained:
// ** class dependency **
public class dependency {
Public void F (){};
}< br> // ** business **
public class business {
dependency D;
Public Business () {
d = New dependency ();
}< br> Public void dosomething () {
D. F ();
}< BR >}
Make the following changes to the above implementation:
First, set the setter method for obtaining the dependency instance in business, and then change the dependency class to an interface implementation. The following code is available:
// ** Interface idependency **
Public interface idependency {
Void F ();
}
// ** Class dependency **
Public class dependency {
Public void F (){};
}
// ** Business **
Public class business {
Idependency D;
Public Business (){}
Public void dosomething (){
D. F ();
}
Public void setdependency (idependency d ){
This. d = D;
}
}
in the new code, the Business variable D can receive any idependency instance. In addition, the dependency instance is not obtained through business, instead, it is transmitted externally through setter (or the constructor. This seems to be no different from our usual code, but it is already a good design. The key is how dependency instances are injected to business from the outside?
This is implemented through XML.
Create a springfirst. XML for simple configuration:
id =" business "
class =" aopfirst. business. business "
In this configuration file, add the dependency class and business class, the dependency parameter is used as a business parameter.
This XML file is not enough. A test class is required to load the XML file. Spring provides an existing API to load the XML file as follows: instantiate the dependency class, instantiate the business class, and assign the dependency instance as a parameter to the business instance
Setdependency () method. Below is the testProgram:
Public class startserver {
Public static void main (string [] ARGs ){
Classpathresource Cr = new classpathresource ("springfirst. xml ");
Beanfactory factory = new xmlbeanfactory (CR );
Business B = (business) Factory. getbean ("business ");
B. dosomething ();
}
}
After the above program loads XML, it obtains the bean with ID "business", that is, the business class instance, and calls its dosomething () method. It can be seen that the dependency class dependency of business is injected through XML, and business receives dependency instances through the idependency interface. Therefore, when we have a new implementation of idependency, we only need to modify the XML file. The test program only needs to obtain the required parameters according to the ID value in XML.
To sum up the above example, we can understand control inversion and dependency injection. Dependency class is injected by external (XML), rather than by its own business. This is dependency injection. On the other hand, the dependency of business on class dependency is transferred to the dependency on the idependency interface, and the control is transferred from class to interface, that is, from "Implementation" to "abstract. This is the control reversal.
In fact, dependency injection andControl reversal(Inversion of control) the two are the same concept. Specifically, when a role (possibly a Java instance, caller) needs the assistance of another role (another Java instance, called ),DesignIn the process, the caller usually creates the called instance. HoweverSpring/structThe creation of the called instance is no longer completed by the caller. Therefore, it is called "control inversion ".Spring/structContainer to complete and then inject the caller, which is also called "dependency injection ".
Spring mainly adopts the Java reflection mechanism,
Java reflection mechanism means that Java programs can load, explore, and use completely unknown classes during the execution period. This mechanism is very effective for the development of software frameworks. It is very similar to the callbyname method I used in VB.
JavaThe reflection mechanism mainly provides the following functions:
1. Determine the class to which any object belongs during running;
2. Construct the object of any class at runtime;
3. Determine the member variables and methods of any class at runtime;
4. Call methods of any object at runtime;
5. Generate a dynamic proxy
In JDK, classes that implement reflection mechanisms areJava. Lang. Reflect package.
1. Class class: represents a class
It has the following methods:
(1) getname ():
Obtain the complete name of the class.
(2) getfields ():
Obtain the public type attribute of the class.
(3) getdeclaredfields ():
Obtain all attributes of the class, including public, protected, default, and private access-level attributes.
(4) getmethods ():
Obtain the public type of the class.
(5) getdeclaredmethods ():
Obtain all methods of the class.
(6) * getmethod (string name, class [] parametertypes ):
Obtain the specific method of the class. The name parameter specifies the name of the method, and the parametertypes parameter specifies the parameter type of the method.
(7) * getconstrutors ():
Obtain the public Type constructor of the class.
(8) * getconstrutor (class [] parametertypes ):
Obtain the specific constructor of the class. The parametertypes parameter specifies the parameter type of the constructor.
(9) newinstance ():
Creates an object of this class through the class constructor without parameters.
2. Field Class: represents the member variables of the class (attributes of the class ).
3. method class: indicates the method of the class.
The invoke (Object OBJ, object ARGs []) method of the method class is used to dynamically execute a specific method of an object,
Its first OBJ parameter specifies the object with this method, and the second ARGs parameter specifies the parameter passed to this method.
4. constructor class: constructor class.
5. array class: provides static methods for dynamically creating arrays and accessing array elements.
1. Spring does not provide interface-oriented programming. In fact, it is not very useful and should be improved gradually, rather than spring
2. Spring is also a container, and the container is the object that can load objects.
3. Spring AOP-Aspect-oriented programming that can intercept methods and attributes
5. Spring lightweight model and dependency Injection
Seamless integration of spring and Hibernate
Sessionbean and hibernate XML definition files can be directly configured.