【Spring Framework】5、依賴注入
阿新 • • 發佈:2021-07-08
1、構造器注入
User 實體類
package com.xg.pojo; public class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void show() { System.out.println("name:" + name); } }
1.1、使用下標建立
<?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="user" class="com.xg.pojo.User"> <constructor-arg index="0" value="遇見星光"/> </bean> </beans>
1.2、使用型別建立
不建議使用
<?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="user" class="com.xg.pojo.User"> <constructor-arg type="java.lang.String" value="遇見星光"/> </bean> </beans>
1.3、通過引數名直接建立
<?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="user" class="com.xg.pojo.User">
<constructor-arg name="name" value="遇見星光"/>
</bean>
</beans>
2、Set方式注入【重點】
- 依賴注入:Set注入!
- 依賴:bean物件的建立依賴於容器
- 注入:bean物件的所有屬性,由容器來注入
2.1、環境搭建
1、複雜型別
Student
package com.xg.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobby;
private Map<String,String> card;
private Set<String> game;
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<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGame() {
return game;
}
public void setGame(Set<String> game) {
this.game = game;
}
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 +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", game=" + game +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
2、真實測試物件
Address
package com.xg.pojo;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Address{" +
"name='" + name + '\'' +
'}';
}
}
3、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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.xg.pojo.Address">
<property name="name" value="預設地址"/>
</bean>
<bean id="student" class="com.xg.pojo.Student">
<!-- 普通注入 value-->
<property name="name" value="遇見星光"/>
<!-- bean注入 ref-->
<property name="address" ref="address"/>
<!-- 陣列-->
<property name="books">
<array>
<value>紅樓夢</value>
<value>三國演義</value>
<value>西遊記</value>
<value>水滸傳</value>
</array>
</property>
<!-- List -->
<property name="hobby">
<list>
<value>聽歌</value>
<value>看電影</value>
<value>敲程式碼</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份證" value="***************************"/>
<entry key="手機" value="123****1231"/>
</map>
</property>
<!-- Set-->
<property name="game">
<set>
<value>LOL</value>
<value>COC</value>
</set>
</property>
<!-- null-->
<property name="wife">
<null/>
</property>
<!-- Properties-->
<property name="info">
<props>
<prop key="user">Admin</prop>
<prop key="password">123456</prop>
<prop key="sex">男</prop>
</props>
</property>
</bean>
</beans>
4、測試類
import com.xg.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student.toString());
}
}
/*
Student{
name='遇見星光',
address=Address{name='預設地址'},
books=[紅樓夢, 三國演義, 西遊記, 水滸傳],
hobby=[聽歌, 看電影, 敲程式碼],
card={
身份證=***************************,
手機=123****1231
},
game=[LOL, COC],
info={user=Admin, password=123456, sex=男},
wife='null'
}
*/
3、拓展方式注入
可以使用p名稱空間和c名稱空間進行注入
userBeans.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p名稱空間注入, 可以直接注入一些簡單的值: property-->
<bean id="user" class="com.xg.pojo.User" p:name="遇見星光"/>
<!-- c名稱空間注入, 通過構造器注入: constructor-arg-->
<bean id="user2" class="com.xg.pojo.User" c:name="遇見星光" c:age="18"/>
</beans>
測試
@Test
public void userTest() {
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
@Test
public void user2Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user.toString());
}
注意點
使用p名稱空間和c名稱空間需要匯入對應的xml約束!
-
p名稱空間
-
xmlns:p="http://www.springframework.org/schema/p"
-
-
c名稱空間
-
xmlns:c="http://www.springframework.org/schema/c"
-