Spring_4 屬性注入介紹及使用

IOC 和 DI 區別

  • IOC : 控制反轉,把對象創建交給spring 進行配置。
  • DI : 依賴注入,向類里面的屬性中設置值。
  • 關系 : 依賴注入不能單獨存在,需要在IOC基礎之上完成操作。

屬性注入

屬性注入是在創建對象時候,向類里面屬性里面設置值。

在 Java 中屬性注入的三種方式

  1. 使用 set 方法注入
public class User{
    private String name;
    public void setName(String name){
        this.name = name;
    }
}

User user = new User();
user.setName("abcd");
  1. 使用有參構造注入
public class User{
    private String name;
    public User(String name){
        this.name = name;
    }
}

User user = new User("abc");
  1. 使用接口注入
public interface Dao{
    public void insert(String name);
}

public class DaoImpl implements Dao{
    private String name;
    
    public void insert(String name){
        this.name = name;
    }
}

在 Spring 框架里面,支持兩種方式

  1. set 方法注入(重點)

實體類

package com.sfox.bean;

public class Book {
    private String name;
    public void setName(String name){
        this.name = name;
    }
    
    public void putMsg(){
        System.out.println("out put name:" + name);
    }
}


beans.xml配置

<!-- 使用set方法注入屬性 -->
<bean id="book" class="com.sfox.bean.Book">
    <!-- 注入屬性值
        name 屬性:實體類里面定義的屬性
        value 屬性:設置具體的值
    -->
    <property name="name" value="戰士"/>
</bean>
  1. 有參數構造注入

實體類

package com.sfox.bean;

public class Book {
    private String name;
    public Book(String name){
        this.name = name;
    }
    
    public void putMsg(){
        System.out.println("out put name:" + name);
    }
}

上面代碼是使用set方法注入屬性值,需要注意一下幾點,

  • 要在對應的實體類中添加需要注入的屬性的set方法。
  • beans.xml 中<property name="name" value="戰士"/>name 對應實體中的要注入的屬性名,value 是name設置的屬性值對應的內容。

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 id="book" class="com.sfox.bean.Book">
        <constructor-arg name="name" value="王五"/>
    </bean>
</beans>

通過上面代碼,可以知道有參構造注入的方法,在使用有參構造注入的時候,需要注意,在beans.xml 文件中bean的配置,<constructor-arg name="name" value="王五"/> name 對應實體中的要注入的屬性名,value 是name設置的屬性值對應的內容。

注入對象類型屬性值

下面的示例是向一個 Service 中注入一個 Dao ,在Service 中調用注入的 Dao.


實體Service

package com.sfox.bean;

public class ServiceDao {
    
    private BookDao bookDao;
    
    public void setBookDao(BookDao bookDao){
        this.bookDao = bookDao;
    }
    
    public void serviceOut(){
        System.out.println("service out......");
        bookDao.bookDaoOut();
    }
}


實體Dao

package com.sfox.bean;

public class BookDao {
    public void bookDaoOut(){
        System.out.println("book Dao....");
    }
}


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 id="bookDao" class="com.sfox.bean.BookDao"/>
    <bean id="serviceDao" class="com.sfox.bean.ServiceDao">
        <!-- 注入dao 對象
            name 屬性值:ServiceDao 類里面屬性名稱
                        現在不要寫value屬性,因為是對象
            ref 屬性: dao 配置bean標簽中的id值
         -->
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>

P 命名空間注入

p命名空間注入和上面介紹的有些相同,很簡單,在約束中加入如下約束:

xmlns:p="http://www.springframework.org/schema/p"

好了,現在我們就可以使用p命名空間注入了。
P命名空間注入bean配置也非常簡單,看下面示例:

  • 實體類:

    public class UserP {
        private String pname;
        public void setPname(String pname) {
            this.pname = pname;
        }
        
        public void show() {
            System.out.println("pname:" + pname);
        }
    }
    
    
  • beans.xml 中bean 的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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 id="userp" class="com.cfox.spring.UserP" p:pname="wangwu"></bean>
</beans>

說明:在約束中定義P命名空間,然后在bean 中使用 p:變量 對變量進行賦值。

復雜類型注入

復雜類型注入其實也非常簡單,在<bean>標簽中使用<property>,具體看下面示例:

  1. 數組
  2. list 類型
  3. set 類型
  4. map 類型
  5. properties 類型
  • 創建實體bean
public class MultType {
    private String[] arr;
    private ArrayList<String> list;
    private Set<String> set;
    private Map<String, String> map;
    private Properties properties;
    public void setArr(String[] arr) {
        this.arr = arr;
    }
    public void setList(ArrayList<String> list) {
        this.list = list;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public void show() {
        System.out.println( "MultType [arr=" + Arrays.toString(arr) + ", list=" + list
                + ", set=" + set + ", map=" + map + ", properties="
                + properties + "]");
    }
}

下面我們看一下beans.xml 中如何配置向bean 中注入結合屬性的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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 id="multType" class="com.cfox.spring.MultType">
            <!-- 數組 -->
            <property name="arr">
                <list>
                    <value>arr1</value>
                    <value>arr2</value>
                    <value>arr3</value>
                </list>
            </property>
            
            <!-- list 集合 -->
            <property name="list">
                <list>
                    <value>zhangsan</value>
                    <value>wangwu</value>
                </list>
            </property>
            <!-- set 集合 -->
            <property name="set">
                <set>
                    <value>hello</value>
                    <value>good</value>
                </set>
            </property>
            <!-- map 集合 key->value -->
            <property name="map">
                <map>
                    <entry key="key1" value="value1"></entry>
                    <entry key="key2" value="value2"></entry>
                    <entry key="key3" value="value3"></entry>
                </map>
            </property>
            <!-- properties -->
            <property name="properties">
                <props>
                    <prop key="pro1">pro.value1</prop>
                    <prop key="pro2">pro.value2</prop>
                </props>
            </property>
        </bean>
</beans>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容