依赖注入(Dependency Injection ,DI)
依赖:指Bean对象的创建依赖于容器,Bean对象的依赖资源。
注入:指Bean对象所依赖的资源,由容器来设置和装配
1、构造器注入:见上篇博客
2、Set注入
要求被注入的属性必须有set方法,set()方法的方法名由set+属性首写字母大写,如果属性是boolean类型,没有set方法,是is
Address.java
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 + '\'' +
'}';
}
}
Student.java
package Pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String>card;
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> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
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 .toString()+
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
1、常量注入
<bean id="student" class="Pojo.Student">
<property name="name" value="花泽生菜"/>
</bean>
测试:
import Pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Student student=(Student)context.getBean("student");
System.out.println(student.getName());
}
}
2、Bean注入
注意:引用数据类型用ref
<bean id="address" class="Pojo.Address"/>
<property name="address" ref="address"/>
<bean id="student" class="Pojo.Student">
<property name="name" value="花泽生菜"/>
3、数组注入
<property name="books">
<array>
<value> 红楼梦</value>
<value> 西游记</value>
<value> 三国演义</value>
</array>
</property>
4、List注入
<property name="hobbys">
<list>
<value>听歌</value>
<value>看书</value>
<value>写代码</value>
</list>
</property>
5、Map注入
<property name="card">
<map>
<entry key="中国银行" value="1216545465"/>
<entry key="中国移动" value="546665556"/>
</map>
</property>
6、set注入
<property name="games">
<set>
<value>LOL</value>
<value>LOL</value>
</set>
</property>
7、Null注入
<property name="wife"><null></null></property>
8、Properties注入
<property name="info">
<props >
<prop key="学号">2020120537</prop>
<prop key="姓名">花泽生菜</prop>
</props>
</property>