spring框架学习(6)
依赖注入(重点)
6.1构造器注入
6.2set注入(重点)
实体类
package com.yms.entity;
/**
-
@Author 杨明书
-
@PackageName: com.yms.entity
-
@ClassName: Address
-
@Description:
-
@Date: 2021/12/29 15:06
*/
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 + ''' +
'}';
}
}
package com.yms.entity;
import java.time.Period;
import java.util.*;
/**
-
@Author 杨明书
-
@PackageName: com.yms.entity
-
@ClassName: Student
-
@Description:
-
@Date: 2021/12/29 15:05
*/
public class Student {
private String name;
private Address address;
private String[] books;
private Listhobby;
private Map<String, String> card;
private Set grams;
private Properties info;
private String wife;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
getHobby() {
return hobby;
}public void setHobby(List
hobby) {
this.hobby = hobby;
}public Map<String, String> getCard() {
return card;
}public void setCard(Map<String, String> card) {
this.card = card;
}public Set getGrams() {
return grams;
}public void setGrams(Set grams) {
this.grams = grams;
}public Properties getInfo() {
return info;
}public void setInfo(Properties info) {
this.info = info;
}public String getWife() {
return wife;
}public void setWife(String wife) {
this.wife = wife;
}@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", grams=" + grams +
", info=" + info +
", wife='" + wife + ''' +
'}';
}
}
配置注入
<!-- address引用类型的注入-->
<bean id="address" class="com.yms.entity.Address">
<property name="address" value="西安"></property>
</bean>
<!-- 单一的string注入-->
<bean id="student" class="com.yms.entity.Student">
<property name="name" value="李白"></property>
<!-- address引用类型的注入-->
<property name="address" ref="address"></property>
<!-- 数组类型的注入-->
<property name="books">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</array>
</property>
<!-- List类型的注入-->
<property name="hobby">
<list>
<value>一</value>
<value>二</value>
<value>三</value>
<value>四</value>
</list>
</property>
<!-- Map类型的注入-->
<property name="card">
<map>
<entry key="身份证" value="122222222222222222222"></entry>
<entry key="银行卡" value="7633333333333333333333"></entry>
</map>
</property>
<!-- Set类型的注入-->
<property name="grams">
<set>
<value>lol</value>
<value>bob</value>
</set>
</property>
<!-- null类型的注入-->
<property name="wife">
<null></null>
</property>
<!-- Properties类型的注入-->
<property name="info">
<props>
<prop key="学号">T12309</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>