3 依賴注入(DI)
module:spring-04-di
概念
- 依賴注入(Dependency Injection,DI),
- 依賴:指Bean物件的創建依賴于容器 . Bean物件的依萊澩 ,
- 注入 : 指Bean物件所依賴的資源 , 由容器來設定和裝配 ,
構造器注入
前文已經介紹過,
Set方法注入(重點)
要求被注入的屬性必須有set方法,set方法的方法名由set + 屬性首字母大寫,如果屬性是boolean型別 , 沒有set方法 , 是 is ,
物體類物件創建:
Address.java
package com.zzb.pojo;
public class Address {
private String Address;
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
@Override
public String toString() {
return "Address{" +
"Address='" + Address + '\'' +
'}';
}
}
Student.java
package com.zzb.pojo;
import java.util.*;
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 String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
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 +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public String[] getBooks() {
return books;
}
public List<String> getHobbies() {
return hobbies;
}
public Map<String, String> getCard() {
return card;
}
public Set<String> getGames() {
return games;
}
public String getWife() {
return wife;
}
public Properties getInfo() {
return info;
}
}
1、常量注入
<bean name="student" >
<!--普通值注入-->
<property name="name" value="https://www.cnblogs.com/zzbstudy/p/Zzb"/>
</bean>
測驗:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
測驗結果:
Zzb
2、Bean 注入
注意:這里的值是一個參考,ref
<bean name="address" >
<property name="address" value="https://www.cnblogs.com/zzbstudy/p/廈門"/>
</bean>
<bean name="student" >
<!--普通值注入-->
<property name="name" value="https://www.cnblogs.com/zzbstudy/p/Zzb"/>
<!--參考注入-->
<property name="address" ref="address"/>
</bean>
3、陣列注入
<!--陣列注入-->
<property name="books">
<array>
<value>紅樓夢</value>
<value>三國演義</value>
<value>水滸傳</value>
<value>西游記</value>
</array>
</property>
4、List 注入
<!--List注入-->
<property name="hobbies">
<list>
<value>打游戲</value>
<value>聽歌</value>
<value>敲代碼</value>
</list>
</property>
5、Map 注入
<!--Map注入-->
<property name="card">
<map>
<entry key="身份證" value="https://www.cnblogs.com/zzbstudy/p/111111222222223333"/>
<entry key="校園卡" value="https://www.cnblogs.com/zzbstudy/p/22222221111587"/>
</map>
</property>
6、Set 注入
<!--Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CS</value>
</set>
</property>
7、Null 注入
<!--Null注入-->
<property name="wife">
<null/>
</property>
8、Properties 注入
<!--Properties注入-->
<property name="info">
<props>
<prop key="性別">男</prop>
<prop key="年齡">23</prop>
</props>
</property>
測驗:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
測驗結果:
Student{name='Zzb', address=Address{Address='廈門'}, books=[紅樓夢, 三國演義, 水滸傳, 西游記], hobbies=[打游戲, 聽歌, 敲代碼], card={身份證=111111222222223333, 校園卡=22222221111587}, games=[LOL, CS], wife='null', info={性別=男, 年齡=23}}
p 命名和 c 命名注入
創建 User 物體類:
注意:這里沒有寫有參建構式
package com.zzb.pojo;
public class User {
private int age;
private String name;
public User() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
1、p命名空間注入:需要在頭檔案中加入約束
匯入約束:xmlns:p="http://www.springframework.org/schema/p"
<!--p命名空間注入,可以直接注入屬性的值:property-->
<bean name="user1" p:age="23" p:name="Zzb"/>
2、c命名空間注入:需要在頭檔案中加入約束
匯入約束:xmlns:c="http://www.springframework.org/schema/c"
<!--c命名空間注入,通過構造器注入:constructor-arg-->
<bean name="user2" c:name="ZZB" c:age="23"/>
發現問題,程式報紅,因為User物體類沒有撰寫有參函式構造方法,
解決辦法:把有參建構式添加上,因此,可以得出結論,c就是所謂的構造器注入!
public User(int age, String name) {
this.age = age;
this.name = name;
System.out.println("ok");
}
測驗代碼:
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user1 = context.getBean("user1", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user1.toString());
System.out.println(user2.toString());
}
測驗結果:
ok
User{age=23, name='Zzb'}
User{age=23, name='ZZB'}
Bean 的作用域
在Spring中,那些組成應用程式的主體及由Spring IoC容器所管理的物件,被稱之為bean,簡單地講,bean就是由IoC容器初始化、裝配及管理的物件 ,

幾種作用域中,request、session、globalSession作用域僅在基于web的應用中使用(不必關心你所采用的是什么web應用框架),只能用在基于web的Spring ApplicationContext環境,
Singleton
當一個bean的作用域為Singleton,那么Spring IoC容器中只會存在一個共享的bean實體,并且所有對bean的請求,只要id與該bean定義相匹配,則只會回傳bean的同一實體,Singleton是單例型別,就是在創建起容器時就同時自動創建了一個bean的物件,不管你是否使用,他都存在了,每次獲取到的物件都是同一個物件,注意,Singleton作用域是Spring中的預設作用域,即默認作用域,要在XML中將bean定義成singleton,可以這樣配置:
<bean name="user2" c:name="ZZB" c:age="23" scope="singleton"/>
測驗:
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user1 = context.getBean("user1", User.class);
User user2 = context.getBean("user2", User.class);
User user3 = context.getBean("user2", User.class);
System.out.println(user3 == user2);
System.out.println(user1.toString());
System.out.println(user2.toString());
}
測驗結果:
ok
true
User{age=23, name='Zzb'}
User{age=23, name='ZZB'}
Prototype
當一個bean的作用域為Prototype,表示一個bean定義對應多個物件實體,Prototype作用域的bean會導致在每次對該bean請求(將其注入到另一個bean中,或者以程式的方式呼叫容器的getBean()方法)時都會創建一個新的bean實體,Prototype是原型型別,它在我們創建容器的時候并沒有實體化,而是當我們獲取bean的時候才會去創建一個物件,而且我們每次獲取到的物件都不是同一個物件,根據經驗,對有狀態的bean應該使用prototype作用域,而對無狀態的bean則應該使用singleton作用域,在XML中將bean定義成prototype,可以這樣配置:
<bean name="user2" c:name="ZZB" c:age="23" scope="prototype"/>
或者
<bean id="user2" singleton="false"/>
測驗:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user1 = context.getBean("user1", User.class);
User user2 = context.getBean("user2", User.class);
User user3 = context.getBean("user2", User.class);
System.out.println(user3 == user2);
System.out.println(user1.toString());
System.out.println(user2.toString());
}
測驗結果:
ok
ok
false
User{age=23, name='Zzb'}
User{age=23, name='ZZB'}
Request
當一個bean的作用域為Request,表示在一次HTTP請求中,一個bean定義對應一個實體;即每個HTTP請求都會有各自的bean實體,它們依據某個bean定義創建而成,該作用域僅在基于web的Spring ApplicationContext情形下有效,考慮下面bean定義:
<bean id="loginAction" class=com.zzb.LoginAction" scope="request"/>
針對每次HTTP請求,Spring容器會根據loginAction bean的定義創建一個全新的LoginAction bean實體,且該loginAction bean實體僅在當前HTTP request內有效,因此可以根據需要放心的更改所建實體的內部狀態,而其他請求中根據loginAction bean定義創建的實體,將不會看到這些特定于某個請求的狀態變化,當處理請求結束,request作用域的bean實體將被銷毀,
Session
當一個bean的作用域為Session,表示在一個HTTP Session中,一個bean定義對應一個實體,該作用域僅在基于web的Spring ApplicationContext情形下有效,考慮下面bean定義:
<bean id="userPreferences" scope="session"/>
針對某個HTTP Session,Spring容器會根據userPreferences bean定義創建一個全新的userPreferences bean實體,且該userPreferences bean僅在當前HTTP Session內有效,與request作用域一樣,可以根據需要放心的更改所創建實體的內部狀態,而別的HTTP Session中根據userPreferences創建的實體,將不會看到這些特定于某個HTTP Session的狀態變化,當HTTP Session最終被廢棄的時候,在該HTTP Session作用域內的bean也會被廢棄掉,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232848.html
標籤:Java
上一篇:goland2020.1中文破解版 附安裝教程免激活碼
下一篇:4 自動裝配
