Dependency injection (DI)
- Dependency injection (Dependency Injection, DI).
- Dependence: Bean refers to create an object depends on the container resource-dependent Bean object.
- Injection: means Bean object is dependent resources, set by the container and assembly.
Constructor injection
Before we 03 have talked in detail
settet injection (focus)
Asked to be injected into the property, you must have a set method, the method name set by the method of capital set + initials property, if the property is boolean type, there is no set method, is is.
pojo Deliverable
public class Address {
private String address;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
}
xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--常量注入-->
<bean id="addr" class="com.cong.pojo.Address">
<property name="address" value="耀华里***"/>
</bean>
<bean id="cong" class="com.cong.pojo.Student">
<property name="name" value="cong"/>
<!--bean注入,用ref-->
<property name="address" ref="addr"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!--list注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>码字</value>
<value>看电影</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="idCard" value="4408*******************"/>
<entry key="countCard" value="6201***************"/>
<entry key="studentCard" value="201*****"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>lol</value>
<value>魔兽</value>
<value>刀塔</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="姓名">rainbow</prop>
<prop key="学号">2014****</prop>
<prop key="性别">female</prop>
</props>
</property>
</bean>
</beans>
Test category
public class TestDI {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student cong = (Student) context.getBean("cong");
System.out.println(cong.toString());
}
}
result
Student{
name='cong',
address=Address{address='耀华里***'},
books=[红楼梦, 水浒传, 西游记, 三国演义], hobbys=[听歌, 码字, 看电影],
card={idCard=4408*******************,
countCard=6201***************,
studentCard=201*****},
games=[lol, 魔兽, 刀塔],
wife='null',
info={学号=2014****, 性别=female, 姓名=rainbow}
}
Extension namespace injection
pojo Deliverable
package com.cong.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
beans.xml
Note To import constraints
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.cong.pojo.User" p:name="cong" p:age="3"/>
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="com.cong.pojo.User" c:name ="cong2" c:age="3"/>
</beans>
Test category
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
System.out.println(user.toString());
User user2 = (User) context.getBean("user2");
System.out.println(user2.toString());
}