Spring5入門-03-依賴注入和配置的一些細節(bean、陣列、list、map、set、properties)
阿新 • • 發佈:2020-09-23
一、前言
- 參考視訊:遇見狂神說-Spring5
- 參考文件:官方文件
- 使用:
- IDEA 2019.3
- Maven
- Spring 5.2.9.RELEASE
- Junit 4.12
目的:去了解xml配置bean依賴注入和配置的一些細節(bean、陣列、list、map、set、properties)
二、準備工作
2.1 依賴
注意:這裡用到的應該是spring-context,但是spring-webmvc由於繼承的關係會有一張依賴網:
算是省心省力吧。
<!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!--JUnit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
2.2 實體類
這次使用基於setter的依賴注入,不顯式建立建構函式:
路徑
程式碼
Address.java
package com.duzhuan.pojo; /** * @Autord: HuangDekai * @Date: 2020/9/23 14:34 * @Version: 1.0 * @since: jdk11 */ public class Address { private int id; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "id=" + id + ", address='" + address + '\'' + '}'; } }
Student.java
package com.duzhuan.pojo; import java.util.*; /** * @Autord: HuangDekai * @Date: 2020/9/23 14:33 * @Version: 1.0 * @since: jdk11 */ public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; 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> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } 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 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) + ", hobbies=" + hobbies + ", card=" + card + ", games=" + games + ", info=" + info + '}'; } }
三、beans.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="address,a1" class="com.duzhuan.pojo.Address">
<property name="id" value="1"/>
<property name="address" value="上海市翻斗大街翻鬥花園二號樓1001室"/>
</bean>
<!--註冊一個Bean,可以看到,這樣的命名方式可以對一個bean命名多個name-->
<bean name="student,s1" class="com.duzhuan.pojo.Student">
<property name="name">
<null/>
</property>
<!--注入一個物件-->
<property name="address" ref="a1"/>
<!--注入一個數組-->
<property name="books">
<array>
<value>Spring實戰(第四版)</value>
<value>演算法導論</value>
</array>
</property>
<!--注入一個List-->
<property name="hobbies">
<list>
<value>寫程式碼</value>
<value>看番</value>
</list>
</property>
<!--注入一個Map-->
<property name="card">
<map>
<entry key="校園卡" value="20201111003"/>
<entry key="門禁卡" value="20191111001"/>
</map>
</property>
<!--注入一個set-->
<property name="games">
<set>
<value>LOL</value>
<value>WOW</value>
</set>
</property>
<!--注入一個Properties-->
<property name="info">
<props>
<prop key="學號">17123456</prop>
</props>
</property>
</bean>
</beans>
四、 測試樣例
import com.duzhuan.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Autord: HuangDekai
* @Date: 2020/9/23 20:41
* @Version: 1.0
* @since: jdk11
*/
public class StudentTest {
@Test
public void studentTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student s1 = (Student) context.getBean("s1");
System.out.println(s1);
}
}