1. Introduction to spring IOC
IoC: inversion of control. In the traditional development mode, developers create and manage objects by themselves. When the Spring framework is used, these things are handed over to the framework. It can be understood that "the management right of developers over objects to the framework". Therefore, there is a transfer of "management right", It is called "control reversal".
DI: dependency injection, which is embodied in assigning values to the attributes of objects through the framework.
IoC and DI: the Spring framework implements IoC through DI. DI is the means of implementation, and IoC is the goal we want to achieve.
About Spring IoC, we need to master how to confirm the attribute value of an object of a class through the Spring framework.
2. Inject the value of attribute through SET
Suppose there is a User class with a String password attribute. If you want Spring to inject value (assignment) into the password attribute when creating the object of the User class, prepare the User class first, for example:
package cn.tedu.spring; public class User { private String password; public String getPassword() { return password; } public void setPassword(String password) { System.out.println("User.setPassword()"); this.password = password; } }
Then, configure in the Spring configuration file:
<bean id="user" class="cn.tedu.spring.User"> <!-- property Nodes: values used to configure attributes --> <!-- name Attribute: the name of the attribute whose value needs to be configured, which is actually set Name corresponding to method --> <!-- value Attribute: the value of the attribute that needs to be configured --> <property name="password" value="000111"/> </bean>
Finally, at run time, after obtaining the object, you can directly output the attribute value. You can see that the configured value of the attribute already exists.
Note: in the < property > attribute, the name attribute is actually the name corresponding to the set method. For example, if the above configuration is name="password", the Spring framework will capitalize the initial letter based on this configuration value and spell set on the left to get setPassword. Taking this as the method name and combined with the value attribute value, setPassword("000111") is called to make the password attribute assigned. Therefore, the essence is that the value of the name attribute corresponds to the set method of the class. However, the standard set method name is exactly the same as the way Spring organizes set methods. Therefore, it can also be simply understood that the name attribute refers to the attribute name.
2.1 inject values of List set type
If you need to inject a value of the List collection type, for example:
// David, Lucy, Tom, Lily, Alex private List<String> names;
It needs to be configured as:
<bean id="sampleBean" class="cn.tedu.spring.SampleBean"> <property name="names"> <list> <value>David</value> <value>Lucy</value> <value>Tom</value> <value>Lily</value> <value>Alex</value> </list> </property> </bean>
2.2 inject values of array type
If the type to be injected is array, for example:
// 9, 5, 2, 7 private int[] numbers;
The configuration is:
<property name="numbers"> <array> <value>9</value> <value>5</value> <value>2</value> <value>7</value> </array> </property>
2.3 inject values of Set type
If the data to be injected is Set collection type, if it exists:
// Beijing, Shanghai, Guangzhou, Shenzhen private Set<String> cities;
It needs to be configured as:
<property name="cities"> <set> <value>Beijing</value> <value>Shanghai</value> <value>Guangzhou</value> <value>Shenzhen</value> </set> </property>
2.4 inject Map type values
If the type of data to be injected is Map:
// {username:root, password:1234, from:Beijing} private Map<String, String> session;
It needs to be configured as:
<property name="session"> <map> <entry key="username" value="root"/> <entry key="password" value="1234"/> <entry key="from" value="Beijing"/> </map> </property>
2.5 inject values of type Properties
If you need to inject Java util. Data of type properties, if any:
// name:Jack, age:25, gender:Male private Properties profile;
The configuration is:
<property name="profile"> <props> <prop key="name">Jack</prop> <prop key="age">25</prop> <prop key="gender">Male</prop> </props> </property>
Of course, data of type properties can also be obtained from Read from a file of type properties. You can first create JDBC under src/main/resources Properties file and configure properties and values in it:
url=jdbc:mysql://localhost:3306/database_name driver=com.mysql.jdbc.Driver username=root password=root initialSize=2 maxActive=10
In the Spring configuration file, you need to use the < util: Properties > node to read this file. This node is the same level as the < bean > node. Do not write this node in the child of < bean >.
When configuring:
<!-- util:properties Nodes: for reading.properties Configuration information in file --> <!-- location Attribute: the location of the file to be read --> <!-- classpath:It means resources folder --> <util:properties id="jdbc" location="classpath:jdbc.properties"></util:properties>
If the data needs to be read into the Properties jdbc property of the SampleBean, add the following to the SampleBean first:
// The data comes from JDBC Properties file private Properties jdbc;
In addition, in the Spring configuration file, add the following in the < bean > node child of the configuration SampleBean:
<property name="jdbc" ref="jdbc"/>
When injecting a value into an attribute, if the attribute value is another Bean, it should be referenced to the id of another Bean through the ref attribute.
The < util: Properties > used is also a < bean >.
Summary: if an attribute in a class needs to be injected with value, first, you need to add a matching SET method for this attribute. In the Spring configuration file, add a < property > node in the child of the < Bean > node corresponding to this class to inject value for the attribute. If multiple attributes need to be injected with value, Then the child of the same < Bean > node should have multiple < property > nodes. In each < property > node, the name of the attribute to be injected is specified through the name attribute. Assuming that the attribute to be injected is called password, it should be configured as name="password". If the value of the attribute can be written directly (such as numeric value, string, Boolean value), Then add the value attribute in the < property > node to configure the value. If the value of this attribute is another Bean object, add the ref attribute in the < property > node to reference the id value of another < Bean > node or the equivalent node. If the value of this attribute is of collection type, add < list > / < SET > / < array > / < Map > / < props > in the children of the < property > node for configuration, Refer to the example code in the above notes for the specific configuration method.
3. Inject the value of attribute through construction method (less used)
If a class does not have a parameterless constructor, and you want to inject values into some attributes through the constructor, for example:
package cn.tedu.spring; // Inject the value of the attribute using the constructor // When using constructor injection, it is not required that each property has a SET/GET method public class Person { // Zoe private String username; // 26 private int age; public Person(String username, int age) { super(); this.username = username; this.age = age; } @Override public String toString() { return "Person [username=" + username + ", age=" + age + "]"; } }
In the Spring configuration file, it needs to be configured as:
<bean id="person" class="cn.tedu.spring.Person"> <!-- constructor-arg Nodes: configuring parameters of construction methods --> <!-- index Attribute: the position of the parameter, that is, the number of parameters --> <constructor-arg index="0" value="Zoe"/> <constructor-arg index="1" value="26"/> </bean>
Therefore, if there are multiple parameters in the construction method, multiple < constructor Arg > nodes need to be added to the children of the < Bean > node for configuration. In the < constructor Arg > node, the index attribute represents the position of the parameters and is numbered sequentially from 0, that is, the index value of the first parameter is 0, the index value of the second parameter is 1, and so on. If the corresponding parameter value is a numerical value String and Boolean values are configured through the value attribute. If the corresponding parameter value is another Bean object, you need to refer to the id value of another < Bean > node through the ref attribute. If the corresponding parameter value is a collection type, you need to add a collection node to the child of < constructor ARG > for configuration.