[SSM framework] Spring notes - Spring overview; Detailed explanation of IOC control inversion

1. What is the Spring framework

It is a container that integrates other frameworks. Its core is IOC and AOP. Spring framework consists of more than 20 modules, which provides excellent solutions in many fields.

 

2. Features of Spring

1) Lightweight

It is composed of more than 20 modules. Each jar package is very small, less than 1M, and the core package is about 3M;

No pollution to code;     

2) Interface oriented programming

Using interfaces is flexible. The scalability and maintainability of the project are very high. When in use, the interface points to the implementation class, and the whole function can be switched by switching the implementation class.

3)AOP -- aspect oriented programming

It is to develop the public, general and repeated code separately and weave it back when necessary. The underlying principle is dynamic agent.

4) Integrate other frameworks

Big fish eat small fish. Spring integrates many excellent frameworks.

 

3. What is IOC

  • Inversion of control (IOC) is a concept and an idea. The Spring container creates objects and injects dependencies, which programmers can use directly when using.

Forward conversion: the creation of objects and dependency injection by programmers is called forward conversion The programmer has the the final say.

//Programmers create objects
Student stu = new Student();   
//The programmer assigns values
stu.setName("Zhang San");           
stu.setAge(22);

 

Inversion: the creation of objects and dependency injection by the Spring container is called inversion, which takes control from the programmer and gives it to the Spring container, which is called inversion. The container has the say.

<!--Spring The container is responsible for the creation of objects-->
<bean id="stu" class="com.bjpowernode.pojo.Student">     
    <!--Spring Container dependency injection value-->
    <property name="name" value="Zhang San"/>                    
    <property name="age" value="22"/> 
</bean>

Remember: when the Spring container starts, all objects (stu,,) are created

 

4. IOC based on xml

1) Create object

<bean id="stu" class="com.bjpowernode.pojo.Student"></bean>

 

2) Assign a value to the created object

A. setter injection is used. The injection is divided into simple type injection and reference type injection:

  • Simple type injection value uses value attribute; (simple type refers to [8 basic types + String])
  • ref attribute is used for reference type injection value

 

<!--Must pay attention to:use setter Injection must provide a parameterless construction method, which must be provided setXXX()method.-->
<!--Create student object-->
<bean id="stu" class="com.bjpowernode.pojo2.Student">
    <!--Simple type injection-->
    <property name="name" value="Li Si"></property>    
    <property name="age" value="22"></property>
    <!--Reference type injection-->
    <property name="school" ref="school"></property>  
</bean>

<!--Create school object-->
<bean id="school" class="com.bjpowernode.pojo2.School">
    <property name="name" value="Tsinghua University"></property>
    <property name="address" value="Haidian District"></property>
</bean>

 

 

B. Injection using construction method

a. Use the parameter name of the construction method to inject values

<bean id="school" class="com.bjpowernode.pojo3.School">
    <constructor-arg name="name1" value="Tsinghua University"></constructor-arg>
    <constructor-arg name="address1" value="Haidian District"></constructor-arg>
</bean>


b. Subscript injection value using construction method parameters

<bean id="stu" class="com.bjpowernode.pojo3.Student">
    <constructor-arg index="0" value="Qian Qi"></constructor-arg>
    <constructor-arg index="1" value="22"></constructor-arg>
    <constructor-arg index="2" ref="school"></constructor-arg>
</bean>

 

c. The sequential injection values of the parameters of the default construction method are used

<bean id="stuSequence" class="com.bjpowernode.pojo3.Student">
    <constructor-arg value="Chen Shi"></constructor-arg>
    <constructor-arg value="22"></constructor-arg>
    <constructor-arg ref="school"></constructor-arg>
</bean>

 

 

5. Annotation based IOC

  • Annotation based IOC, also known as DI(Dependency Injection), is the technology of IOC implementation

 

For annotation - based IOC, package scanning must be added to Spring's core configuration file

<context:component-scan base-package="com.bjpowernode.s01"></context:component-scan>

1) notes for creating objects:

@Component: any object can be created The default name of the created object is the hump nomenclature of the class name. You can also specify the name of the object @ Component("specify name")

@Controller: an object (Servlet) specially used to create a controller. This object can receive user requests and return processing results to the client

@Service: an object specially used to create the business logic layer, which is responsible for accessing the data access layer downward, and returning the processed results to the interface layer

@Repository: an object specially used to create the data access layer, which is responsible for all operations of adding, deleting, modifying and querying in the database

Case:

@Component("stu")  //Leave it to Spring to create the object, which is created when the container starts
public class Student {
    //Simple type value injection
    @Value("Zhang San")  
    private String name;
    @Value("22")
    private int age;
}


    
2) Annotation of dependency injection

Used to inject values into simple types:

@Value:

 

Injection of reference type

A.@Autowired: use the type injection value to search for objects of the same type from the whole Bean factory for injection

  • What are homologous types:

a. The injected type (school in Student) is exactly the same as the injected type

b. The injected type (school parent in Student) and the injected type (child) are parent-child classes

c. The injected type (school interface in Student) and the injected type (implementation class) are the types of interface and implementation class

Note: in the case of parent and child classes, there may be multiple injectable objects using type injection At this time, perform secondary filtering according to the name, and select the object with the same name as the injected object for injection
     

B. Use the name injection value to search for objects with the same name from the whole Bean factory for injection

@Autowired

@Qualifier("name")

Note: if there are parent and child classes, inject values directly by name; Both annotations are useful for injecting values by name

 

6. How to add package scanning

1) Single package scanning (recommended)

<context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
<context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
<context:component-scan base-package="com.bjpowernode.dao"></context:component-scan>

 

2) Multiple packages are scanned and separated by commas or spaces or semicolons

<context:component-scan base-package="com.bjpowernode.controller com.bjpowernode.service ,com.bjpowernode.dao"></context:component-scan>


3) Scan root package (not recommended)

<context:component-scan base-package="com.bjpowernode"></context:component-scan>
  • It will reduce the starting speed of the container and lead to more useless work

 

7. Specify multiple Spring configuration files for the application

When the project becomes larger and larger, it needs to be developed by multiple people, and there is a great hidden danger with only one configuration file

1) Policy for splitting profiles

A. Demolition by layer

applicationContext_controller.xml (write operations about the interface layer)

<bean id="uController" class="com.bjpowernode.controller.UsersController">
<bean id="bController" class="com.bjpowernode.controller.BookController">

 

applicationContext_service.xml (write operations about the business logic layer)

<bean id="uService" class="com.bjpowernode.controller.UsersService">
<bean id="bService" class="com.bjpowernode.controller.BookService">

 

applicationContext_mapper.xml (write operations about the data access layer)

<bean id="uMapper" class="com.bjpowernode.controller.UsersMapper">
<bean id="bMapper" class="com.bjpowernode.controller.BookMapper">

 

B. Remove by function

applicationContext_users.xml        

<bean id="uController" class="com.bjpowernode.controller.UsersController">
<bean id="uService" class="com.bjpowernode.controller.UsersService">
<bean id="uMapper" class="com.bjpowernode.controller.UsersMapper">

 

applicationContext_book.xml      

<bean id="bController" class="com.bjpowernode.controller.BookController">
<bean id="bService" class="com.bjpowernode.controller.BookService">
<bean id="bMapper" class="com.bjpowernode.controller.BookMapper">

 

8. Integration of spring configuration files

1) Single file import

<import resource="applicatoinContext_mapper.xml"></import>
<import resource="applicatoinContext_service.xml"></import>
<import resource="applicatoinContext_controller.xml"></import>

 

2) Batch import

<import resource="applicatoinContext_*.xml"></import>

 

Tags: Spring SSM

Posted by F1Fan on Wed, 25 May 2022 21:29:24 +0300