Detailed introduction to Spring configuration files

Table of contents

1. Naming of Spring configuration files

2. What is in the Spring configuration file

3. set injection

4. Construct injection


1. Naming of Spring configuration files

Answer: The Spring configuration file is placed under the resources folder. Generally, we will give it a default name: applicationContext.xml. As shown below:

2. What is in the Spring configuration file

  The above figure is the content of the most basic Spring configuration file. The usage of each label in this configuration file will be introduced in detail below.

  1. In this applicatinContext.xml file, usually the top is a string of URLs, and this string of URLs will be given an alias, which is a kind of constraint information. When you run this file, it will go to this address to call the written things by itself, and abide by the written rules.
  2. Detailed interpretation of the <bean> tag
<bean id=""  class="">
    <property name="" value=""/>  <!--这是set注入-->
</bean>
  •  The <bean> tag is used to create an object, which has the same effect as the new object we learned initially.
  • The id in the <bean> tag represents the name of the object you create in the future, which is unique.
  • class: Indicates what type of class the object you created is. We need to write the full name qualification of this class (this full name qualification is to write the package path where this class is located from the java package). In addition: the class we have here must not be an interface, because the interface cannot be a new object
  • If there is no content between the <bean></bean> tag pair, then <bean></bean> can be abbreviated as <bean/>.

3. set injection

<bean id="" class="">
    <property name="" value="" />
</bean>

Set injection means that when we create this bean object, we use the set method to assign values ​​to the properties of the created object.

Therefore, when we use the set injection method to create an object, we must ensure that the class already has a set method.

The name attribute of the property tag in set injection : it must be exactly the same as the attribute name of the class, because Spring will find the corresponding set method of setName according to the name of the name.

The value attribute of the property tag in the set injection : it means assigning a value to the attribute of the object, and the value of the value is the value of the attribute of the object, but here it should be noted that the value attribute can only assign a value to a simple type of attribute, (simple The type is: the eight basic types and the packaging classes of the basic types).

So how to copy with the properties of the reference type?

At this time, we need to use the ref attribute, which is also an attribute of the property tag. The ref attribute is used to assign values ​​​​to the attributes of the reference type. The function of the ref attribute is the same as that of the value attribute, but the value attribute is for the basic type. Attribute assignment, the ref attribute assigns a value to an attribute of a reference type.

The value of the ref attribute is the id value of the reference type.

Supplement: When a class uses set to inject attribute values, but only writes the set method, but does not write the attribute, then using set injection will not report an error, because according to Spring's design rules, Spring will only judge the value of the attribute. Whether the set method exists, if the method exists, then no error will be reported.

4. Construct injection

<bean id="" class="">
    <constructor-arg name="" value="" />
</bean>

Construction injection uses the parameterized construction method of the class that creates the object, so when using construction injection, ensure the existence of the construction method.

The name attribute in the constructor-arg tag in the construction injection must be exactly the same as the name of the parameter in the construction method.

The value attribute in the constructor-arg tag in the construction injection is also used to assign values ​​to the attributes of the basic type. If you assign a value to a property of a reference type, you must use the ref property. The value of ref is the value of the id of the class where this attribute is located.

Supplement : The name attribute in the constructor-arg tag in the construction injection can be replaced by index, and the value of index is 0, 1, 2. It means assigning a value to the first parameter in the construction method. For example

<bean id="" class="">
    <constructor-arg index="0" value="" /><!--给构造方法的第一个参数赋值-->
    <constructor-arg index="2" value="" /><!--给构造方法的第三个参数赋值-->
    <constructor-arg index="1" value="" /><!--给构造方法的第二个参数赋值-->
</bean>

The above code shows that when using the attribute of index to assign values ​​to the attributes injected by the structure, it is not necessary to assign values ​​​​in the order of 0, 1, and 2. The order can be disrupted, and subscripts of 0, 2, and 1 are also possible.

If we strictly follow the subscripts of 0, 1, and 2 to assign values ​​to the attributes injected by the structure, then we can also abbreviate it into the following style:

<bean id="" class="">
    <constructor-arg  value="" /><!--给构造方法的第一个参数赋值-->
    <constructor-arg  value="" /><!--给构造方法的第二个参数赋值-->
    <constructor-arg  value="" /><!--给构造方法的第三个参数赋值-->
</bean>

That is, the above code omits the index attribute, but this method requires that you must assign values ​​​​to the attributes in the order of the parameters in the construction method.

The power of construction injection: when we use construction injection to assign values ​​to properties, we can directly use the built-in written classes in Java. For example, as shown in the following figure:

The name in the above figure is the attribute of the File class, and the value is the value we want to assign to the object myFile of the File class we defined.

Guess you like

Origin blog.csdn.net/weixin_44362089/article/details/127341643