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
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
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
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");
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");
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);
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)!