Spring Framework
Spring is the container framework for managing multiple Java classes, and note that the class does not manage interfaces.
The main functions of Spring are the Ioc inversion control and DI dependency injection .
The method of injection can be either a constructor assignment or a set method assignment, which is recommended by the set method.
Multiple Java classes in the spring configuration file are instantiated, and initialized (injected values) in the spring load phase. Waits for a user's call to use the server-side cache consumption in exchange for a quick response from the client.
Ioc: inversion control. When instantiating a bean class, the traditional way of operation is instantiated by the caller. In spring, the caller is no longer instantiated, and the instantiation is performed by the spring container instead.
DI: is a further description of the IOC. called Dependency Injection. It is the responsibility of spring to perform the assignment of variables in the Bean class while the bean's instantiation operation is performed.
Both IOC and Di refer to the same thing: Bean management is entirely up to spring.
Prerequisites for Spring Set injection:
Leave the default parameterless constructor
Variable privatization
There is a corresponding public set method for the variable
The bean's configuration in spring
Each bean class, when loaded into spring, will have its own configuration information.
<bean id= "Bean unique alias" class= "Class Physical Address" >
<property name= "variable name" >
<value> Specific Values </value>
</property>
</bean>
Inject another bean
Using ref or IDREF for injection
The bean to be injected needs to be configured first in spring and injected with a reference injection by an ID alias
When an injection is performed, only the explicit unique value (value) is given or another bean (ref IDREF) is injected. When a collection is injected, it can only be injected in the form of child elements.
at the same time, in addition to the child element of the way to implement the value of the variable injection, you can also use the namespace of the way to inject. And. The data type cannot be changed by itself when using namespaces for injection, all of which are default string types
P: Variable name = "Value"
Spring is a container framework that configures beans in spring's configuration file Applicationcontext.xml
and initialization < injection > (you can either initialize the variable by the constructor or assign a value to the parameter through the Set method) the Java class configured in the spring configuration file is instantiated and initialized during the server load phase, waiting for the user's call request.
Spring uses a single-state mode of operation (construction method Privatization, which returns an instance of an object through a public static construction method) to reuse an instantiated Java class.
In spring, a configuration file allows any number of XML files to be created anywhere in any name.
The default, in the SRC directory, exists applicationcontext.xml as the Master profile.
Instantiation of the Pplicationcontext
It is implemented by two classes in two instances.
Classpathxmlapplicationcontext
Filesystemxmlapplicationcontext
Each Java class, when placed in spring for unified management, is matched by a unique <bean> tag.
Spring has two default modes of operation: Single-state and factory-mode
Factory mode:< polymorphism >
Unified interface manages multiple Java classes
The engineering model simply says: What type of object does the user pass in and return.
When multiple objects have the same or similar behavior, these similar behaviors are abstracted out to form a common interface, which is implemented from this interface to form a unified parent class management.
Also, create a factory object. When a specific object is invoked, it is no longer done by the instantiation of the concrete object, but by the instantiation of the factory class implementation object.
In this way, all of the different objects, whether they are parent or instantiated, are managed uniformly.
When spring starts, all beans configured in spring are loaded and instantiated first. At the same time, the assignment of all variables that are injected with good values is automatically done to prepare for the call by the client. In this way, when the user wants to invoke the bean through spring, it simply makes the call, without the process of instantiation.
★ ATTENTION POINT ★! : A Bean class that, when not injected with the value of a variable through spring, can be instantiated either in the traditional new way or by spring. But once a variable is injected through spring, it can only be called through Spring . (Cause: When new is instantiated without injection and is obtained through the spring context, the object is instantiated by the default parameterless constructor, if the spring configuration file is injected with values (child elements or namespaces) and then new instantiation or the default parameterless constructor, The Java class that the spring context obtains is the instantiation of the injection parameter, which results in a different two invocations.
The bean configured in Spring can be either a Java class that actually exists or a bean without a class body.
In spring, a bean has four advanced properties
(1) Scope property
How spring generates beans and the scope of the bean
A:default
By default, it is a single state
B:singleton
A single state. Instantiate a bean in a single-state thought, implementing all calls to the current bean to reuse the same object and share the same address.
Evaluation:
Advantages: Implementing Object Reuse
Cons: Data conflicts may occur
C:prototype
Each user request generates an instance of a new bean
Advantages: Avoid data data, solve the problem of single State
Cons: Too many bean classes are generated.
Workaround: The bean's management is now no longer owned by the caller, but is entirely up to spring.
D:request
The same bean is shared within the scope of a request sent by the user.
E:session
Within the scope of a user session, the same bean is common
F:globalsession
Represents the same bean for the entire application scope
Recommended: Default or Prototype
Use singleton for the model layer and business layer. For the control layer, use prototype.
(2) Lazy init
Lazy Loading
A: Basic Ideas
In spring, by default, all beans configured in spring are automatically instantiated and injected when spring starts.
Lazy load: Refers to whether the bean is instantiated and injected when spring starts.
B:false
Default
Default, does not delay loading. Spring is started and instantiated immediately
In essence: The server startup speed decreases, in exchange for user call performance improvement.
Generates: Server cache usage is increased.
C:true
Lazy Loading
does not instantiate and inject operations at spring startup. The instantiation occurs only if the user is waiting for a call.
(3) Autowire
Automatic loading
When one property needs to inject a value that is another bean, it can be implemented automatically without XML configuration injection.
A:no
Default
Injection must be implemented through XML configuration
B:byname
Automatically search for matching beans in spring based on the variable name for injection
C:bytype
Automatically search for matching beans for injection based on the data type of the variable
D:constructor
Injected according to the parameters specified in the Bean's constructor
E:autodetect
Automate the previous three auto-injection methods
Because, in a bean, the variable name or data type may not accurately represent the information of the bean to be injected. Also, in a class, there may be multiple data types that are variables of a bean, so that automatic matching is done one at a time and is too resource-intensive. It is recommended that you explicitly inject XML with precision.
(4) Check dependencies
Verify that all variables in the bean have a set method, and that they are injected in spring.
In practice, not all variables need to be injected to assign values.
A:none
Default
Do not check
Set method setting and injection operation according to the user's own needs completely.
B:simple
Only basic data types and collection type variables are checked.
C:objects
Checks only for variables of the object type (which need to reference another bean)
D:all
All variables are checked
(5) Life cycle
In spring, the Bean's management is fully operational by spring. Colleagues, you can add your own initialization and resource-freeing operations to the bean. Further management of the life cycle.
Initialization: It is not the initialization assignment of a variable, but the lock needs to obtain the initialization of the resource, for example, the acquisition of database information.
When spring starts, the default is to instantiate the bean, perform the injection, and perform the initialization operation.
Only the destroy operation is automatically invoked when the container stops serving the live server, freeing up the resources that are consumed.
How Spring works core components and applications