Spring構造方法注入和set注入


1,基於構造方法注入

無參構造方法注入

無參構造方法注入就是Spring會自動調用類的無參構造方法來創建一個對象,然后再把這個對象提交到Spring容器。像前一篇里寫的Person就不需要提交任何的參數。

有參構造方法注入

但是有一些對象是有一些屬性的,這些屬性的初始化是通過構造方法傳遞進去的。這個時候創建對象時就必須要指定對應的參數。
Spring提供了通過構造器傳入參數注入對象的方法。
1.改造一下Person:

package com.xl.spring.xlIoc.beans;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Person {

    private Integer id;
    private String name;

    public void sayHello(String name){
        System.out.println(this.name + " say hello to " + name);
    }
}

2.修改注入配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 存放bean的容器 -->
<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:用來指定一個需要spring創建並注入到IOC容器的對象
            id:表示這個對象的名字
            class:表示類型,類的全限定名
        constructor-arg:表示對應的構造方法的參數
            name:表示參數的名字
            value:表示參數的值
            index:表示參數的位置,從0開始
    -->
    <bean id="person" class="com.xl.spring.xlIoc.beans.Person">
        <constructor-arg index="0" value="1"/>
        <constructor-arg name="name" value="金鍾國"/>
    </bean>
</beans>

測試:

package com.xl.spring.xlIoc.test;

import com.xl.spring.xlIoc.beans.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Ioc001 {
    public static void main(String[] args) {
        //1.通過配置文件獲取到spring的上下文:獲取Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml");
        //2.獲取對應想要用的對象,這一步是通知spring的ioc容器讓它拿一個對象來
        Person person = (Person) ac.getBean("person"); //通過id來獲取對象
        //3.可以調用對象的方法了
        person.sayHello("宋智孝");
    }
}

有參構造方法的注入缺陷

有參構造方法有一個小缺陷,假如Person類中有三個參數,然后定義了兩個有參構造方法,兩個方法都是只傳兩個參數。這個時候就會發現,始終有一個方法沒有被調用到。因為Spring調用有參構造方法的邏輯是按照構造方法的定義順序來的,從上往下找到第一個滿足參數個數匹配的方法來調用。
想要解決這個問題很簡單。
這么寫就好了:

<bean id="person" class="com.xl.spring.xlIoc.beans.Person">
    <constructor-arg name="nickName" value="懵懵"/>
    <constructor-arg name="name" value="宋智孝"/>
</bean>

2,基於Setter注入

setter注入的原理是先調用無參構造方法來創建對象,然后掉用對應的set方法來為屬性賦值。用setter注入的方式也可以解決之前那個有參構造方法的缺陷,並且可以提高可讀性。
1.還是先改造Person:

package com.xl.spring.xlIoc.beans;

import lombok.Setter;

@Setter
public class Person {

    private Integer id;
    private String name;
    private String nickName;

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

2.通過property標簽來注入對應的屬性:

<!-- name:屬性名
     value:屬性值 -->
<bean id="person" class="com.xl.spring.xlIoc.beans.Person">
    <property name="id" value="1"/>
    <property name="name" value="宋智孝"/>
    <property name="nickName" value="懵懵"/>
</bean>

測試還是一樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM