在Spring中實現IoC容器的方法是依賴注入,依賴注入的做用是在使用Spring框架建立對象時動態地將其所依賴的對象(例如屬性值)注入Bean組件中。html
Spring框架的依賴注入一般有兩種實現方式,一種是使用構造方法注入,另外一種是使用屬性的setter方法注入。java
Spring框架能夠採用Java反射機制,經過構造方法完成依賴注入。spring
建立項目及導入Maven模塊過程請看《使用IDEA開發Spring入門程序》,在這就不贅述了。在這繼續前面的項目,按照下面的步驟補充:api
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;
}
}
複製代碼
配置文件的具體代碼以下:bash
<?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>
複製代碼
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>
複製代碼
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());
}
}
複製代碼
須要注意的是,若是使用這種方法,要嚴格按照構造器參數的位置賦值,若是不這樣賦值,固然也不會報錯,但會形成賦值錯亂,好比會把姓名賦值成性別,這固然是咱們不肯意看到的,若是你非不按要求賦值(有點極端,皮一下),有種方法能夠避免你賦值錯亂,請看下面代碼:app
<?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>
複製代碼
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());
}
}
複製代碼
不要覺得這樣就完了,我在想,若是出現重載的狀況,該如何辦?且看我向下分解:框架
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>
複製代碼
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能搞定的事情弄這麼複雜幹嗎,因此經常使用的仍是方式一測試
這部分放到下一篇講解吧,篇幅有點多了,請持續關注!ui