使用IDEA詳解Spring中依賴注入的類型


使用IDEA詳解Spring中依賴注入的類型(上)

Spring中實現IoC容器的方法是依賴注入,依賴注入的作用是在使用Spring框架創建對象動態地將其所依賴的對象(例如屬性值)注入Bean組件中。

Spring框架的依賴注入通常有兩種實現方式,一種是使用構造方法注入,另一種是使用屬性的setter方法注入

使用構造方法注入

Spring框架可以采用Java反射機制,通過構造方法完成依賴注入

創建項目導入Maven模塊過程請看《使用IDEA開發Spring入門程序》,在這就不贅述了。在這繼續前面的項目,按照下面的步驟補充:

創建entity包,創建Person類

 

 

package entity;

public class Person {
    private String name;
    private String sex;

    public Person() { System.out.println("無參構造調用了..."); } public Person(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } 復制代碼

構造方法注入方式一

編寫配置文件

 

 
src根目錄下創建Spring配置文件 applicationContext.xml。在配置文件中首先將 entity.Person類托管給 Spring,讓Spring創建其 對象,同時給構造方法 傳遞實參

 

配置文件的具體代碼如下:

<?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"> <!--將指定類TestDaoImpl配置給Spring,即注冊一個TestDaoImpl對象,讓Spring創建其實例--> <!-- 一個Bean標簽可以注冊一個組件(對象、類) class:寫要注冊的組件的全類名 id:這個對象的唯一標識 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用構造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗賢若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> </beans> 復制代碼
在測試類TestDemo中測試
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test4(){ //初始化spring容器ApplicationContext,加載配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通過容器獲取test實例 Person person2 =(Person) applicationContext.getBean("person2"); System.out.println("姓名:"+person2.getName()+";"+"性別:"+person2.getSex()); } } 復制代碼
測試結果

 

 

構造方法注入方式二

編寫配置文件
<?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"> <!--將指定類TestDaoImpl配置給Spring,即注冊一個TestDaoImpl對象,讓Spring創建其實例--> <!-- 一個Bean標簽可以注冊一個組件(對象、類) class:寫要注冊的組件的全類名 id:這個對象的唯一標識 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用構造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗賢若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--可以省略name屬性,嚴格按照構造器參數的位置賦值--> <bean id="person3" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗賢若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> </beans> 復制代碼
在測試類TestDemo中測試
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test5(){ //初始化spring容器ApplicationContext,加載配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通過容器獲取test實例 Person person3 =(Person) applicationContext.getBean("person3"); System.out.println("姓名:"+person3.getName()+";"+"性別:"+person3.getSex()); } } 復制代碼
測試結果

 

 

需要注意的是,如果使用這種方法,要嚴格按照構造器參數的位置賦值,如果不這樣賦值,當然也不會報錯,但會造成賦值錯亂,比如會把姓名賦值成性別,這當然是我們不願意看到的,如果你非不按要求賦值(有點極端,皮一下),有種方法可以避免你賦值錯亂,請看下面代碼:

編寫配置文件
<?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"> <!--將指定類TestDaoImpl配置給Spring,即注冊一個TestDaoImpl對象,讓Spring創建其實例--> <!-- 一個Bean標簽可以注冊一個組件(對象、類) class:寫要注冊的組件的全類名 id:這個對象的唯一標識 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用構造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗賢若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--可以省略name屬性,嚴格按照構造器參數的位置賦值--> <bean id="person3" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗賢若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> <bean id="person4" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <!--index="1",為參數指定索引,從0開始--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗賢若如" ></constructor-arg> </bean> </beans> 復制代碼
在測試類TestDemo中測試
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test5(){ //初始化spring容器ApplicationContext,加載配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通過容器獲取test實例 Person person4 =(Person) applicationContext.getBean("person4"); System.out.println("姓名:"+person4.getName()+";"+"性別:"+person4.getSex()); } } 復制代碼
測試結果

 

 

 


 

不要以為這樣就完了,我在想,如果出現重載的情況,該如何辦?且看我向下分解:

將entity包下的Person類修改如下
package entity;

public class Person {
    private String name;
    private String sex;
    private Integer age;
    private String email;

    public Person() { System.out.println("無參構造調用了..."); System.out.println("Person創建了..."); } public Person(String name, String sex) { this.name = name; this.sex = sex; System.out.println("有參構造器"); } public Person(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public Person(String name, String sex, String email) { this.name = name; this.sex = sex; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } 復制代碼
編寫配置文件
<?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"> <!--將指定類TestDaoImpl配置給Spring,即注冊一個TestDaoImpl對象,讓Spring創建其實例--> <!-- 一個Bean標簽可以注冊一個組件(對象、類) class:寫要注冊的組件的全類名 id:這個對象的唯一標識 --> <bean id="test" class="dao.TestDaoImpl"/> <bean id="person1" class="entity.Person"/> <!--使用構造方法注入--> <bean id="person2" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg name="name" value="泰斗賢若如"></constructor-arg> <constructor-arg name="sex" value="男"></constructor-arg> </bean> <!--可以省略name屬性,嚴格按照構造器參數的位置賦值--> <bean id="person3" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <constructor-arg value="泰斗賢若如"></constructor-arg> <constructor-arg value="男"></constructor-arg> </bean> <bean id="person4" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!-- public Person(String name, String sex) { this.name = name; this.sex = sex; }--> <!--index="1",為參數指定索引,從0開始--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗賢若如" ></constructor-arg> </bean> <bean id="person5" class="entity.Person"> <!--使用有參構造器進行創建對象並賦值--> <!--public Person(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public Person(String name, String sex, String email) { this.name = name; this.sex = sex; this.email = email; } --> <!--重載的情況下type可以指定參數的類型--> <constructor-arg value="男" index="1"></constructor-arg> <constructor-arg value="泰斗賢若如" index="0"></constructor-arg> <constructor-arg value="22" index="2" type="java.lang.Integer"></constructor-arg> </bean> </beans> 復制代碼
在測試類TestDemo中測試
package test; import dao.TestDao; import entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.FileSystemResource; public class TestDemo { @Test public void test6(){ //初始化spring容器ApplicationContext,加載配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通過容器獲取test實例 Person person5 =(Person) applicationContext.getBean("person5"); System.out.println("姓名:"+person5.getName()+";"+"性別:"+person5.getSex()+";"+"年齡:"+person5.getAge()); } } 復制代碼
測試結果

 

 

不過話又說過來了,明明name能搞定的事情弄這么復雜干嘛,所以常用的還是方式一

使用屬性的setter方法注入

這部分放到下一篇講解吧,篇幅有點多了,請持續關注!

 


免責聲明!

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



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