- 依赖注入
依赖:值bean对象的创建依赖于容器。
注入:Bean对象依赖的资源由容器来设置和装配。
2.Spirng 注入 – 构造注入
3.Spirng 注入 – setter 注入
要求被注入的属性必须有set方法。set 的方法由 set+首字符大写构成。
3.1 常量注入
3.2 Bean注入
3.3 数组注入
3.4 List注入
3.5 Map注入
3.6 Set注入
3.7 Properties注入
<bean id="student" class="com.duoduo.Student">
<!-- 常量注入 -->
<property name="name" value="constant test"/>
<!-- Bean 注入 -->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books">
<array>
<value>yuwen</value>
<value>shuxue</value>
<value>yingyu</value>
</array>
</property>
<!-- List 注入 -->
<property name="hobbies">
<list>
<value>basketball</value>
<value>football</value>
</list>
</property>
<!-- Map 注入 -->
<property name="cards">
<map>
<entry key="ICBC" value="1234567890"/>
<entry key="CMBC" value="12121212121"/>
</map>
</property>
<!-- Set 注入 -->
<property name="games">
<set>
<value>dota</value>
<value>QQ game</value>
</set>
</property>
<!-- Properties 注入 -->
<property name="properties">
<props>
<prop key="no">2012011111</prop>
<prop key="name">name</prop>
<prop key="aget">20</prop>
</props>
</property>
</bean>
<bean id="address" class="com.duoduo.Address">
<property name="addressDetail" value="student address information"/>
</bean>
实体类
package com.duoduo;
import java.util.*;
/**
* @author Duoduo
* @version 1.0
* @date 2017/5/13 14:13
*/
public class Student {
private String name;
private Address address;
private String books[];
private List<String> hobbies;
private Map<String,String> cards;
private Set<String> games;
private Properties properties;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", cards=" + cards +
", games=" + games +
", properties=" + properties +
'}';
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试函数
@Test
public void testSetName() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
Student student = (Student)context.getBean("student");
System.out.println(student.toString());
}
测试结果
Student{name='constant test', address=Address{addressDetail='student address information'}, books=[yuwen, shuxue, yingyu], hobbies=[basketball, football], cards={ICBC=1234567890, CMBC=12121212121}, games=[dota, QQ game], properties={name=name, no=2012011111, aget=20}}