3 DI
3.1 setter injection of dependency injection
Note: Dependency injection is actually a process of assigning values to properties in the current bean; it is actually very simple to implement this process in Spring, you only need to create a sub-label property for the corresponding bean tag in the configuration file, and specify the name attribute The attribute name and value attribute in the bean can be assigned to the attribute in the bean; the above method actually assigns the attribute through the set method, so the class corresponding to the bean must contain the set method.
Step demonstration:
1. Create a Student class
package di; public class Student { private Integer id; private String name; private Integer age; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
Second, configure the bean in the configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Student" class="di.Student"> <property name="id" value="001"></property> <property name="name" value="ArimaMisaki"></property> <property name="age" value="18"></property> <property name="sex" value="male"></property> </bean> </beans>
Third, write test class
import di.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class locTest { @Test public void locTestMethod() { ClassPathXmlApplicationContext loc = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = loc.getBean(Student.class); System.out.println(student); } }
3.2 Constructor Injection of Dependency Injection
Note: In addition to implementing dependency injection through the set method, it is also possible to inject through the parameterized constructor; the detailed steps are to write the constructor-arg sub-tag in the corresponding bean tag, the order of the sub-tags corresponds to the order of the constructor, and the value of The properties in the constructor can be assigned in order.
Tip: If you don't want to assign properties in the order of the constructors, you can use the name attribute in the constructor-arg tag to specify the properties corresponding to the bean.
Step demonstration:
1. Create the Student class
This class must contain a parameterized constructor, because the dependency injection method is injected through the parameterized constructor
package di; public class Student { private Integer id; private String name; private Integer age; private String sex; public Student(Integer id, String name, Integer age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
Second, write the configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Student" class="di.Student"> <constructor-arg value="001"></constructor-arg> <constructor-arg value="ArimaMisaki"></constructor-arg> <constructor-arg value="18"></constructor-arg> <constructor-arg value="male"></constructor-arg> </bean> </beans>
3. Write test classes
import di.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class locTest { @Test public void locTestMethod() { ClassPathXmlApplicationContext loc = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = loc.getBean(Student.class); System.out.println(student); } }
3.3 Special value handling
Empty value: If a property is empty, either do not assign a value to the property, or use the null subtag to indicate that the property is empty.
Special symbols: If you want to add special symbols to attribute values, you must use xml character entities.
CDATA section: The fields in CDATA will be parsed into plain text data by XML, so the CDATA byte can be written with special symbols at will; if you want to write the CDATA section in the Spring configuration file, you need to wrap it with the value tag, and then enter the uppercase CD A CDATA section can be generated.
3.4 Assigning values to class type properties
Reference method: It is common to use another class in one class and instantiate the object. When we hand over the object to the container for management, if we declare another class in the object, we need to use the bean tag. the ref attribute too.
Cascading method: ref is not used for reference. It is also possible to locate directly through the name attribute in the form of ., but this method is not easy to use, because it requires that the class must have been instantiated before it can pass through the name attribute. Assignment.
Inner bean way: We can also achieve our goal by using the bean tag again in the property tag.
Step demonstration:
1. Write a student class
package di; public class Student { private Integer id; private String name; private Integer age; private String sex; private Clazz clazz; public Student(Integer id, String name, Integer age, String sex, Clazz clazz) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.clazz = clazz; } public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Clazz getClazz() { return clazz; } public void setClazz(Clazz clazz) { this.clazz = clazz; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", clazz=" + clazz + '}'; } }
Second, write the class
package di; public class Clazz { private Integer cid; private String cname; public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "Clazz{" + "cid=" + cid + ", cname='" + cname + '\'' + '}'; } }
3. Configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Student" class="di.Student"> <property name="id" value="001"></property> <property name="name" value="ArimaMisaki"></property> <property name="age" value="18"></property> <property name="sex" value="male"></property> <property name="clazz" ref="Class"></property> </bean> <bean id="Class" class="di.Clazz"> <property name="cid" value="201916"></property> <property name="cname" value="Class A"></property> </bean> </beans>
Fourth, write test classes
import di.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class locTest { @Test public void locTestMethod() { ClassPathXmlApplicationContext loc = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = loc.getBean(Student.class); System.out.println(student); } }
3.5 Assigning values to container type properties
Array: To assign an attribute of an array type, you only need to use the value or ref tag in the subtag array of property to assign a value to the attribute.
list:
- To assign a value to a property of type list, you only need to use the value or ref tag in the sub-tag list of property to assign value to the attribute.
- If you want to configure a bean of list collection type, you need to use the constraint of util, the member of the list is the ref tag, and the ref tag uses the bean attribute to select the corresponding bean
map:
- To assign a value to a property of type map, you only need to pass the sub-label map of property, use the enrty label in the map label, and specify the corresponding key and value.
- Of course, the constraint writing method of utill can also be used.
3.6 P namespace
Note: The previous dependency injection seems too cumbersome, we can use the p: namespace through the p: constraint, so that the namespace has all the operable properties.
3.7 Data source object management
Note: Some third-party libraries are not written by us, so when they are handed over to container management, the class path cannot be written in the same way as the traditional one. At this time, we need to find the storage path of their classes in Maven.
step:
-
Import druid and mysql coordinates; there is a pit here, don't import druid wrong, and import Alibaba's instead of apache.
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
-
Create a new spring-datasource configuration file; it's best to use setter injection here, because we don't know the parameter order of the constructor; after everything is over, you can try to run it
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> </beans>
-
Create a new DataSourceTest test class
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException; public class DataSourceTest { public void testDataSource() throws SQLException { ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml"); DruidDataSource druidDataSource = ioc.getBean(DruidDataSource.class); System.out.println(druidDataSource.getConnection()); } }
-
You can also combine the usage of Mybatis to write the connection properties into the configuration file; use the Resource Bundle to create a jdbc configuration file, and write the content after pressing Enter.
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/ username=root password=123456
-
Modify the configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- To access the configuration file you need to use context constraint --> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>