Home > Java > javaTutorial > body text

spring framework learning (4) automatic assembly

黄舟
Release: 2016-12-29 13:15:30
Original
1332 people have browsed it

Set injection and construction injection are sometimes more troublesome when doing configuration. Therefore, in order to improve development efficiency, the framework provides automatic assembly functions and simplifies configuration. The Spring framework does not support automatic assembly by default. If you want to use automatic assembly, you need to modify the autowire attribute of the tag in the spring configuration file.
The automatic assembly attribute has 6 optional values, each representing different meanings.

1, byName
When obtaining the target object from the Spring environment, the properties in the target object will search for the id attribute value of the tag in the entire Spring environment based on the name. If there are the same, then obtain this object and implement the association.
Entire Spring environment: means searching in all spring configuration files, then the ID cannot be repeated.

2, byType
When obtaining the target object from the Spring environment, the attributes in the target object will search the class attribute value of the tag in the entire spring environment according to the type. If there are the same, then obtain this object and implement the association.

Disadvantages: If there are multiple bean objects of the same type, an error will occur.
If the attribute is a single type of data, an error will occur if multiple related objects are found.
If the attribute is an array or collection (generic) type, no exception will occur if multiple associated objects are found.

3, constructor
uses the constructor method to complete object injection. In fact, it also performs object search based on the parameter type of the constructor method, which is equivalent to using byType.

4, autodetect
Automatic selection: If the object does not have a parameterless construction method, then the automatic assembly method of the constructor is automatically selected for construction injection. If the object contains a parameterless constructor, the byType automatic assembly method is automatically selected for setter injection.
5, no
does not support the automatic assembly function

6, default
means that the value of the automatic assembly of the upper-level label is used by default. If there are multiple configuration files, the automatic assembly method of each configuration file is independent.

If there are multiple configuration files, the way to load the configuration file:
1) You can specify the overall configuration file to include the sub-configuration files, and then only load the overall configuration file. Use the import tag in the overall configuration file applicationContext.xml to load the configuration file in the sub-file package




code:

ApplicationContextac= newClassPathXmlApplicationContext("applicationContext.xml");
Copy after login


2) Use asterisks to match multiple files for loading, and the file names must comply with the rules. (Recommended)

//配置文件的名称
applicationContext.xml
applicationContext-action.xml
applicationContext-service.xml
applicationContext-dao.xml
 
ApplicationContextac =newClassPathXmlApplicationContext("applicationContext*.xml");
Copy after login

3) You can use arrays as parameters to load multiple configuration files at one time

String[]files={"applicationContext.xml","applicationContext-test.xml"};               
ApplicationContextac = newClassPathXmlApplicationContext(files);
Copy after login


Note: If the automatic assembly function and manual assembly are used at the same time, the automatic assembly will not work.

The above is the content of spring framework learning (4) automatic assembly. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template