Set注入:
即创建对象时,需要的属性等由第三方提供的依赖,也就是spring来帮你注入!
- 依赖:bean对象的创建依赖于spring容器
- 注入:bean对象中的所有属性,由容器来注入!
1.实体类Student和Address
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2.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="adress" class="com.fsp.pojo.Address">
<property name="address" value="宜春"/>
</bean>
<bean id="student" class="com.fsp.pojo.Student">
<!--将其他bean注入用ref-->
<property name="address" ref="adress"/>
<property name="name" value="fsp"/>
<!--数组-->
<property name="books">
<array>
<value>三国演义</value>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--list集合-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--map集合-->
<property name="cards">
<map>
<entry key="借书卡" value="2021111"/>
<entry key="银行卡" value="55556666666"/>
</map>
</property>
<!--set集合-->
<property name="games">
<set>
<value>LOL</value>
<value>帝国时代</value>
</set>
</property>
<!--空指针-->
<property name="wife">
<null/>
</property>
<!--Properties配置-->
<property name="info">
<props>
<prop key="driver">jdbc.mysql.driver</prop>
<prop key="url">localhost.</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
3.测试
public class MyTestDi {
@Test
public void test(){
//获取spring容器的上下文,同时将容器中的对象实例化
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//获得容器中的bean
Student student = (Student) context.getBean("student");
System.out.println(student.toString());//输出Student类中的方法
}
}