Detailed explanation of IOC and Di of spring

Here is a brief introduction to the difference between IOC and di:

IOC: the translation is control inversion. Spring manages the creation right of the object. Helloservice does not need to create it by itself. Spring can help you create it.

Di: dependency injection. In the process of creating an object, we inject the attribute of the object dependency into our class.

The class we are writing now has no other properties. If you have learned the design of UML, there are several relationships in object-oriented

In a simple book, IOC is more like an idea and Di is a behavior.

Another argument is that IOC is the end and Di is the means. IOC means that the method of generating classes is reversed from the traditional method (New), that is, programmers do not call new, and when classes are needed, they are injected by the framework (DI). This is the same interpretation at different levels.

IOC: reverse of control:

The spring framework manages the control of manually creating objects in the program.

Spring is the first spring test program

1. Prepare jar package

spring-beans-4.1. 2.RELEASE. jar spring-core-4.1. 2.RELEASE. jar com. springsource. org. apache. commons. logging-

Configure application ion XML file

Use the import element to import other configuration files:

Use the import element. Note:

1. By default, it is found from the following path of the classpath. 2. You can use the prefix to locate the base location of the file:

① : [classpath:]: find the following files from the classpath path (recommended); [note the problem with classloader.] ② : [file:]: find the following files using the path of the file system;

Note: the above prefix identifier can be recognized only when the resource interface is implemented in the framework.

Testing in spring

Spring test environment preparation:

Dependent jar:

1.spring-test-4.1. 2.RELEASE. jar 2. spring-context-4.1. 2.RELEASE. jar 3. spring-aop-4.1. 2.RELEASE. jar 4. spring-expression-4.1. 2.RELEASE. jar

Spring4. X needs to rely on the latest JUnit 4 12. Junit4.0 comes with eclipse 8 is not supported, and from spring 4 X starts with AOP package support.

junit-4.12. jar hamcrest-core-1.3. jar

Write @ contextconfiguration ("classpath: ApplicationContext. XML") as the current test class name found by @ contextconfiguration by default - context XML configuration file, such as helloworldtest context xml

Spring container

Beanfactory: it is the lowest level interface in spring and only provides the simplest IOC function (creating and managing beans).

In applications, beanfactory is generally not used, but ApplicationContext (application context) is recommended for the following reasons.

Members managed by spring are called beans (classes, objects, bean elements)

The difference between beanfactory and ApplicationContext

1. ApplicationContext inherits beanfactory and has basic IOC functions; 2. In addition, ApplicationContext provides the following functions:

① Support internationalization; ② Support message mechanism; ③ . support unified resource loading; ④ Support AOP function;

Bean creation time:

1. ApplicationContext will create all beans when it is loaded (recommended for Web Applications). 2. Beanfactory will not create beans (desktop programs) until it gets the beans, delaying initialization

The scope of the bean and how long the bean object can survive

Di: dependency injection: introduction to dependency injection and automatic assembly of XML refers to the method of injecting object dependency properties into XML through configuration during the process of creating objects by spring - automatic assembly (generally not recommended):

Autowire attribute: let spring find the appropriate object and complete Di in a certain way

-Default: do not inject automatically - No: do not inject automatically - byname: inject by name (find beans in spring according to the name of attributes) factory GetBean ("name of attribute") - bytype: inject according to the type of dependent object (factory. GetBean (type of attribute)) - constructor: inject according to the parameter type above the constructor of the object

be careful:

1. If byname is automatically injected, all attribute names and ID names must ensure a standardized naming method; 2. If bytype is injected, if there are multiple instances of the same type in spring -- > the bean is not the only type error;

Manual assembly:

Attribute injection: inject the dependent object through the setter method of the object

Use setter injection: 1. Use the sub element setting of the bean element; 1. Simple type value, directly assign value; 2. Reference type, assignment with ref; 3. Set type. You can directly use the corresponding set type element. 2. Spring injects values through the setter method of the property; 3. The values configured in the configuration file are all strings. Spring can automatically complete the type conversion. 4. The setting value of the attribute is completed before the init method is executed. 5. Improve the spring test and directly inject the objects to be tested into the test class

Constructor injection: through the constructor, dependent objects are injected when creating objects

Constructor Arg: constructor parameters

1, when spring instantiate an object, if the object is not configured with constructor-arg, the object constructor is instantiated by using the default constructor. If there is constructor-arg, then spring uses these constructor-arg to uniquely determine a constructor 1. By default, the order of constructor-arg is the constructor parameter in the order 2,3 adjusts the constructor order: 1., 1. Index: parameter position 2 in constructor Type: type of parameter in constructor 3 Name: set the value in the constructor according to the parameter name of the constructor

Which injection method is better (setter? Constructor?)?

1. If a class must depend on another class to run normally, use the constructor; 2. However, if there are too many parameters of the constructor, the constructor will be ugly; 3. Setter injection is more often used; 4. You can use the @ required tag to require that an attribute must be injected

Property place holder

Create connection pool object for spring property placeholder

Dependent jar: > > MySQL driver package > > Druid package

Application profile

db. Properties file

summary

The above is all about the detailed explanation of IOC and Di of spring in this article. I hope it will be helpful to you. Welcome to: Java exploration thread + IO file encryption and decryption code example, spring configuration scanning multiple package problem analysis, talking about the advantages of springboot to spring, etc. you can leave a message at any time to point out any problems. Thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>