Initial Spring
Spring applies many excellent design patterns, and provides excellent solutions for project implementation; Spring is a lightweight (low-intrusion) MVC and logic code framework. Decoupling IOCs between classes
IOC (Inverse Of Control)
The control of the traditionally created object new is declared and implemented in the spring container (configuration file)
In the MVC framework, the big housekeeper of object management realizes the decoupling between classes
Benefits of IOC: decoupling
IOC implementation principle: factory design pattern-reflection (no parameter construction method) configuration file + factory class
Spring-IOC implementation:
1. Create a java project
2. Guide package
3. Restore a Product example under the MyBatis framework
4. Copy a Spring main configuration file
bean tag to represent an object;
id is the unique identifier of an object;
class is the full path of the class;
5. Object creation by Spring
Load Spring main configuration file
According to the Bean ID, the object is created by the factory
Call the method of the object normally
The DI (Dependence the I njection) assigns them dependency injection
Assign values to object properties
1. Set value injection into the underlying implementation of set method assignment
ref refers to other objects, nesting of objects
2. Construction injection The underlying implementation construction method injection
According to the parameter type and number of parameters under the bean, find the corresponding construction method
If not, report an error
3. Automatic assembly of MVC
Restrictions: Cannot automatically assemble so-called simple types including basic types, strings and collection classes
Usually used to automatically assemble objects
1 Automatic assembly according to the name
Annotate autowire = ”byName”, indicating automatic assembly by name
Scan the attribute values of all objects under the current class, and then use this attribute value to find whether there is a corresponding bean-id in the spring container (configuration file)
If found, the object is automatically assembled; if not found, an error is reported
The underlying implementation is the set method
2 Automatic assembly according to type
Annotate autowire = ”byType”, which means automatic assembly by type
Scan the type of all objects under the current class, and then use the type to find the corresponding type in the spring container (configuration file)
If found, the object is automatically assembled; otherwise, an error is reported
The underlying implementation is the set method