Spring的注入方式总结
Spring的注入方式总结**
最近学习Spring,因官网基本是英文,难以理解,故总结Spring的注入方式,以便后续查找引用。
准备工作
两个类,分别是地址和学生
package com.DI.setGet;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.DI.setGet;
import java.util.*;
public class student {
private String name;
private Address address1;
private String[] books;
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
private List<String> hobby;
private Map<String,String> card;
private Set<String> games;
private Properties dedsc;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress1() {
return address1;
}
public void setAddress(Address address1) {
this.address1 = address1;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
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 Properties getDedsc() {
return dedsc;
}
public void setDedsc(Properties dedsc) {
this.dedsc = dedsc;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", address1=" + address1 +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", dedsc=" + dedsc +
", wife='" + wife + '\'' +
'}';
}
}
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">
</beans>
注入方式
1、name-value 注入
<bean id="student_name" class="com.DI.setGet.student" >
<property name="name" value="XXX"></property>
</bean>
2、bean注入
<bean id="address" class="com.DI.setGet.Address">
<property name="address" value="北京市东城区1号"></property>
</bean>
<bean id="student_address" class="com.DI.setGet.student">
<property name="address1" ref="address"></property>
</bean>
3、数组注入
<bean id="student_books" class="com.DI.setGet.student">
<property name="books">
<array>
<value>"《泰坦尼克号》"</value>
<value>"《论演员的自我修养》"</value>
</array>
</property>
</bean>
4、List注入
<bean id="student_list_hobby" class="com.DI.setGet.student">
<property name="hobby">
<list>
<value>"LOL"</value>
<value>"CSGO"</value>
</list>
</property>
</bean>
5、Map注入
<bean id="student_map_card" class="com.DI.setGet.student">
<property name="card">
<map>
<entry key="01" value="20210101"></entry>
<entry key="02" value="20210102"></entry>
<entry key="03" value="20210103"></entry>
</map>
</property>
</bean>
6、Set注入
<bean id="student_games" class="com.DI.setGet.student">
<property name="games">
<set >
<value>"怪物猎人"</value>
<value>"守望屁股"</value>
</set>
</property>
</bean>
7、Properties注入
<bean id="student_dedsc" class="com.DI.setGet.student">
<property name="dedsc">
<props>
<prop key="m" >"1"</prop>
<prop key="n" >"3"</prop>
</props>
</property>
</bean>
8、null注入
<bean id="student_wife" class="com.DI.setGet.student">
<property name="wife">
<null></null>
</property>
</bean>
注意“”和null并不一致,下面的方法注入的不是null
<bean id="student_wife" class="com.DI.setGet.student">
<property name="wife" value=""></property>
</bean>
该文仅用于自我总结,难免有疏忽之处,望不断改进,努力进步!!!
|