Spring-06-依賴注入(DI)
阿新 • • 發佈:2020-08-22
依賴注入(DI)
1 構造器注入
(前面已經說過了)
2 Set方式注入(重點)
- 依賴注入:Set注入!
- 依賴:bean物件的建立依賴於容器;
- 注入:bean物件的所有屬性,由容器來注入;
環境搭建
- 複雜型別
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
- 真實測試物件
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;
}
- 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="student" class="com.kuang.pojo.Student"> <!--第一種,普通值注入,用value--> <property name="name" value="huba"/> </bean> </beans>
- 測試類
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
完善注入資訊:
<?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.kuang.pojo.Address"/>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一種,普通值注入,用value-->
<property name="name" value="huba"/>
<!--第二種 bean注入 用ref-->
<property name="address" ref="address"/>
<!--陣列-->
<property name="books">
<array>
<value>紅樓夢</value>
<value>西遊記</value>
<value>水滸傳</value>
<value>三國演義</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>聽歌</value>
<value>敲程式碼</value>
<value>看電影</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="身份證" value="111111222222223333"/>
<entry key="銀行卡" value="123123123123123"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--properties-->
<property name="info">
<props>
<prop key="driver">1601400105</prop>
<prop key="url">男</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
3 拓展方式注入
我們可以使用p名稱空間和c名稱空間注入。
官方解釋:
使用:
<?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名稱空間注入 p=properties-->
<bean id="user" class="com.kuang.pojo.User" p:name="huba" p:age="18">
</bean>
<!--c名稱空間注入 c=construct-args -->
<bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba"/>
</beans>
測試:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userebeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
注意點:
需要匯入名稱空間約束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
4 Bean的作用域(Scope)
- 單例模式(Spring預設機制)
<bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba" scope="singleton"/>
- 原型模式:每次從容器中get的時候,都會產生一個新物件
<bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba" scope="prototype"/>
- 其餘的request、session、application,這些個只能在web開發中使用!