IoC inversion of control in Spring-2-Spring

table of Contents

1. XML-based DI

1.1 Injection classification: setter method injection + constructor injection

1.2 Injection classification: setter method injection (master)

1.2.1 Simple type setter method injection

1.2.2 Setter method injection of reference types

1.3 Injection classification: construction method injection (understanding)

1.3.1 Example 1: Injection of parameterized construction method of ordinary java object

1.3.2 Example 2: Parameterized constructor injection of java.io.File object

1.4 Automatic injection of reference type attributes

1.4.1 Reference type attributes: automatic injection byName

1.4.2 Reference type attributes: automatic injection byType

1.5 Specify multiple Spring configuration files for the application

1.5.1 Code Structure

1.5.2 Use tags to import other sub-configuration files in the overall Spring configuration file-1

1.5.2 Use tags to import other sub-configuration files in the overall Spring configuration file-2

1.5.3 Test

2. Spring summary, see the end of the blog

3. Annotation-based DI

3.0 spring.xml configuration file demonstration

3.1 When there are multiple packages in the project, three ways to specify multiple scan packages

3.1.1 Method 1: Use multiple specified different package paths

3.1.2 Method 2: Use a separator to specify the value of base-package

3.1.3 Method 3: base-package is assigned to the parent package name

3.2 Define the annotation @Component of java Bean (Master)  

3.2.1 At the same time, it is necessary to configure the component scanner in the spring.xml file. The component scanner will scan the annotated classes and create objects at the same time

3.2.2 Test notes

3.3 Spring also provides 3 other annotations for creating objects: @Repository/@Service/@Controller /@Component

3.4 Simple type attribute injection @Value (master)

3.5 Reference type attribute injection-1: @Autowired is automatically injected by byType (master)

3.6 Reference type attribute injection-2: The combination of @Autowired and @Qualifier is automatically injected by byName (master)

3.7 JDK annotation @Resource automatic injection (master)

3.7.1 byType injects reference type attributes

3.8 Comparison of using annotations to automatically create javaBean and configuring javaBean in XML files

3.8.1 The way to automatically create javaBean using annotations: use the xml way where the properties of the bean object are not often changed

3.8.2 The way to automatically create javaBean using XML: the attributes of bean objects are often changed using XML

4. Spring summary-IOC

1. XML-based DI

1.1 Injection classification: setter method injection + constructor injection

(1) For the bean instance, the Spring container first creates the object by calling the parameterless constructor , and then initializes the properties of the bean object.

(2) Initialization is done automatically by the Spring container, which is called injection .

(3) According to the different injection methods, there are two commonly used types: setter method injection and construction method injection .

1.2 Injection classification: setter method injection (master)

The set method injection is also called set value injection, which refers to passing the callee's instance through the setter method. This injection method is simple and intuitive, so it is widely used in Spring's dependency injection.

1.2.1 Simple type setter method injection

  

1.2.2 Setter method injection of reference types

When the attribute value of a specified bean is an instance of another bean, the reference relationship between them is specified by ref, and the value of ref must be the id value of a certain bean.

Note: For references to other Bean objects, use the ref attribute of the <bean/> tag.

1.3 Injection classification: construction method injection (understanding)

Constructor injection means that the Spring container completes the instantiation of the callee while constructing the caller instance. That is: use the parameterized constructor of the java object to set the dependency.

1.3.1 Example 1: Injection of parameterized construction method of ordinary java object

The attributes used to specify parameters in the <constructor-arg /> tag are:

(1) name: Specify the parameter name.

(2) index: Specify which parameter corresponds to the constructor, starting from 0. However, this attribute is not necessary, but it should be noted that if the parameter types are the same or there is a containment relationship between them, you need to ensure that the order of assignment is consistent with the order of parameters in the constructor.

1.3.2 Example 2: Parameterized constructor injection of java.io.File object

Use construct injection to create a system class File object.

1.4 Automatic injection of reference type attributes

(1) For the injection of reference type attributes, or the injection that is not displayed in the configuration file, you can set the autowire attribute value for the <bean/> tag to automatically inject the reference type attributes (the default is not to automatically inject the reference type Attributes).

(2) According to the different judgment criteria of automatic injection, it can be divided into two types: autowire =  byName: Automatic injection according to the name of the bean. autowire =  byType: Automatic injection according to the type of bean.

1.4.1 Reference type attributes: automatic injection byName

When the id value of the callee bean in the configuration file is the same as the attribute name of the reference type of the caller bean class in the code, the byName method can be used to let the container automatically inject the callee bean into the caller bean.

The container implements automatic injection by comparing the attribute name of the bean class of the caller with the id of the callee bean in the configuration file.

1.4.2 Reference type attributes: automatic injection byType

Use byType mode automatic injector, requires: the configuration file is specified caller bean class class to invoke a bean's class code and a reference type attribute type homologous : i.e. either the same, or there is-a relationship ( Subclass or implementation class).

Note: However, there can only be one called bean of the same origin. If there is more than one, the container will not know which one to match.

1.5 Specify multiple Spring configuration files for the application

In practical applications, with the increase of application scale, the number of Beans in the system has also increased greatly, resulting in the configuration file becoming very large and bloated. In order to avoid this situation and improve the readability and maintainability of the configuration file, the Spring configuration file can be decomposed into multiple configuration files. Containment relationship configuration file: There is a master file among multiple configuration files, and the master configuration file introduces other sub-files through <import />.

In the Java code, you only need to use the overall configuration file to initialize the Spring container.


For example:

1.5.1 Code Structure

1.5.2 Use the <import/> tag to import other sub-configuration files in the general Spring configuration file-1

1.5.2 Use the <import/> tag to import other sub-configuration files in the general Spring configuration file-2

Note: The wildcard character * can also be used to identify matching any character . However, at this time, it is required that the parent configuration file name cannot meet the format that * can match, otherwise, cyclic recursive inclusion will occur.

In this case, the parent configuration file cannot match the format of spring-*.xml, that is, it cannot be named spring-total.xml.

1.5.3 Test

2. Spring summary, see the end of the blog

3. Annotation-based DI

For DI using annotations, it is no longer necessary to declare bean instances in the Spring configuration file. The use of annotations in Spring requires some changes on the basis of the original Spring runtime environment.

The component scanner <context:component-scan base-package=" "/> needs to be configured in the Spring configuration file to scan annotations in the specified base package.

3.0 spring.xml configuration file demonstration

3.1 When there are multiple packages in the project, three ways to specify multiple scan packages

3.1.1 Method 1: Use multiple <context:component-scan base-package=" "/> to specify different package paths

3.1.2 Method 2: Use a separator to specify the value of base-package

The delimiter can use comma (,), semicolon (; ), and spaces, but spaces are not recommended.

3.1.3 Method 3: base-package is assigned to the parent package name

The value table of base-package is the basic package. The container startup will scan the annotations in the package and its sub-packages, and of course the sub-packages below the sub-package will be scanned. So base-package can specify a parent package.

However, it is not recommended to use the top-level parent package. There are many scanning paths, which will slow down the container startup time. Specify to the target package and appropriate, that is, the full path of the package where the annotation is located. For example, the annotated class is in the com.bjpowernode.beans package.

3.2 Define the annotation @Component of java Bean (Master)  

You need to use the annotation @Component on the class, and the value attribute of the annotation is used to specify the id value of the bean.

3.2.1 At the same time, it is necessary to configure the component scanner in the spring.xml file. The component scanner will scan the annotated classes and create objects at the same time

   

3.2.2 Test notes

3.3 Spring also provides 3 other annotations for creating objects:  @Repository/@Service/@Controller /@Component

In addition, Spring also provides 3 annotations for creating objects:

(1) @Repository: used to annotate DAO implementation classes.

(2) @Service: used to annotate the Service implementation class.

(3) @Controller: used to annotate the Controller implementation class.

These three annotations and @Component can create objects, but these three annotations have other meanings:

@Service creates business layer objects, business layer objects can add transaction functions, and objects created by @Controller annotation can be used as processors to receive user requests.

@Repository, @Service, @Controller are the refinement of @Component annotations, marking objects of different layers, namely persistence layer objects, business layer objects, and control layer objects.

@Component does not specify the value attribute, the id of the bean is the lowercase of the first letter of the class name.

3.4 Simple type attribute injection @Value (master)

You need to use the annotation @Value on the attribute, and the value attribute of the annotation is used to specify the value to be annotated. When using this annotation to complete property injection, no setter is required in the class. Of course, if the property has a setter, it can also be added to the setter.

For example:

3.5 Reference type attribute injection-1: @Autowired is automatically injected by byType (master)

You need to use the annotation @Autowired on the referenced attribute, and the annotation uses automatic Bean assembly by type by default  . When using this annotation to complete property injection, no setter is required in the class. Of course, if the property has a setter, it can also be added to the setter.

For example:

3.6 Reference type attribute injection-2: The combination of @Autowired and @Qualifier is automatically injected by byName (master)

The annotations @Autowired and @Qualifier need to be combined on the reference attribute. The value attribute of @Qualifier is used to specify the id value of the bean to be matched. There is no need for a set method in the class, it can also be added to the set method.

For example:

@Autowired also has an attribute required, the default value is true, which means that when the match fails, the program will be terminated. If the value is set to false, the matching fails and will be ignored, and the value of the unmatched attribute is null.

3.7 JDK annotation @Resource automatic injection (master)

Spring provides support for @Resource annotation in jdk. @Resource annotation can match Bean by name byName , or match Bean by type byType ,

The default is to inject by name byName. Beans cannot be injected by name . Bean matching injection will be performed in the manner of type byType .

To use this annotation, the JDK must be version 6 and above. @Resource can be on attributes or set methods.

3.7.1 byType injects reference type attributes

If the @Resource annotation does not take any parameters, it will be injected by name byName by default. Beans cannot be injected by name. Bean matching injection will be performed in accordance with type byType.

For example:

3.8 Comparison of using annotations to automatically create javaBean and configuring javaBean in XML files

3.8.1 The way to automatically create javaBean using annotations: use the xml way where the properties of the bean object are not often changed

The advantages are: convenient; intuitive; efficient (less code, not as complicated as writing configuration files).

Disadvantages: It is written into the Java code in a hard-coded manner, which is code intrusive. Modifications require recompilation of the code.

3.8.2 The way to automatically create javaBean using XML: the attributes of bean objects are often changed using XML

The advantage is: the configuration and the code are separated; to make changes in xml, no need to compile the code, just restart the server to load the new configuration.

The disadvantages are: troublesome writing, low efficiency, and too complicated for large projects.

4. Spring summary-IOC

 

 

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/112257139