學習過Spring框架的人必定都會聽過Spring的IoC(控制反轉) 、DI(依賴注入)這兩個概念,對於初學Spring的人來講,總以爲IoC 、DI這兩個概念是模糊不清的,是很難理解的,java
Ioc—Inversion of Control,即「控制反轉」,不是什麼技術,而是一種設計思想。mysql
在Java開發中,Ioc意味着將你設計好的對象交給容器控制,而不是傳統的在你的對象內部直接控制。如何理解好Ioc呢?理解好Ioc的關鍵是要明確「誰控制誰,控制什麼,爲什麼是反轉(有反轉就應該有正轉了),哪些方面反轉了」,那咱們來深刻分析一下:spring
●誰控制誰,控制什麼:傳統Java SE程序設計,咱們直接在對象內部經過new進行建立對象,是程序主動去建立依賴對象;而IoC是有專門一個容器來建立這些對象,即由Ioc容器來控制對 象的建立;誰控制誰?固然是IoC 容器控制了對象;控制什麼?那就是主要控制了外部資源獲取(不僅是對象包括好比文件等)。sql
●爲什麼是反轉,哪些方面反轉了:有反轉就有正轉,傳統應用程序是由咱們本身在對象中主動控制去直接獲取依賴對象,也就是正轉;而反轉則是由容器來幫忙建立及注入依賴對象;爲什麼是反轉?由於由容器幫咱們查找及注入依賴對象,對象只是被動的接受依賴對象,因此是反轉;哪些方面反轉了?依賴對象的獲取被反轉了。app
用圖例說明一下,傳統程序設計如圖2-1,都是主動去建立相關對象而後再組合起來:框架
當有了IoC/DI的容器後,在客戶端類中再也不主動去建立這些對象了,如圖2-2所示:curl
以上就是簡單的IOC的理解ide
那麼接下來咱們就來實現下咱們本身的第一個Spring示例函數
1.1咱們準備一個HelloWord類學習
package cn.ljy.clazz; public class HelloWord { //名稱 private String name; //性別 private String sex; //年齡 private int age; //體重 private double weight; //準備構造函數 //無參構造 初始化IOC容器時須要 public HelloWord() { } //姓名 性別 年齡的構造 public HelloWord(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } //姓名 性別 體重的構造 public HelloWord(String name, String sex, double weight) { this.name = name; this.sex = sex; this.weight = weight; } public String sayHello(){ return "HelloWord"+name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "HelloWord [name=" + name + ", sex=" + sex + ", age=" + age + ", weight=" + weight + "]"; } }
1.2咱們必須得準備一個applicationContext.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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <!-- 將 HelloWord 類交給Spring容器進行管理--> <!-- 經過屬性的方式傳入參數 --> <bean id="HelloWord" class="cn.ljy.clazz.HelloWord"> <!-- 爲參數賦值 --> <property name="name" value="巴黎的雨季"></property> </bean> </beans>
這樣就整理好了第一個配置文件了,而後咱們進行一道測試
package cn.ljy.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.ljy.clazz.HelloWord; import cn.ljy.clazz.NewPeople; import cn.ljy.clazz.People; import cn.ljy.clazz.Person; public class MyTest { public static void main(String[] args) { //實例化Spring容器的上下文 (建立IOC容器) ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //經過ApplicationContext的getBean()方法,根據id來獲取Bean的實例 HelloWord hello = (HelloWord)context.getBean("HelloWord"); //調用HelloWord類中的方法 System.out.println("經過屬性的方式給name賦值:"); String result = hello.sayHello(); System.out.println(result); }
實現結果爲:
1.3以上在配置文件中使用的是setter方法依賴注入,接下來咱們使用構造器的方式來實現依賴注入
applicationContext.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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <!-- 將 HelloWord 類交給Spring容器進行管理--> <!-- 經過構造器的方式傳入參數 --> <bean id="AttributeByConstructor1" class="cn.ljy.clazz.HelloWord"> <!-- 經過構造器方式傳參 --> <constructor-arg value="巴黎的雨季" index="0"/> <constructor-arg value="男" index="1"/> <constructor-arg value="18" type="int"/> </bean> </beans>
執行結果如圖
1.4使用引用類型,引用自定義的Bean(外部Bean)
準備一個person對象
package cn.ljy.clazz; public class Person { private String name; private int age; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", car=" + car + "]"; } }
applicationContext.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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <!-- 監管Car 準備三car--> <bean id="car" class="cn.ljy.clazz.Car"> <property name="brand" value="奔馳"></property> <property name="business" value="上海"></property> <property name="price" value="300000"></property> </bean> <!-- 配置person 引用Car --> <bean id="person" class="cn.ljy.clazz.Person"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> </bean> </bean> </beans>
執行結果
1.5使用內部Bean
<!-- 內部bean --> <bean id="person2" class="cn.ljy.clazz.Person"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> <!-- 注意:內部bean只能在內部使用,不能被外部所引用 --> <property name="car" > <bean class="cn.ljy.clazz.Car"> <property name="brand" value="福特"></property> <property name="business" value="北京"></property> <property name="price" value="400000"></property> </bean> </property> </bean>
執行結果與上一步基本一致
1.6配置list集合屬性
People
package cn.ljy.clazz; import java.util.List; public class People { private String name; private int age; private List<Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "People [name=" + name + ", age=" + age + ", cars=" + cars + "]"; } }
<!-- list集合屬性的配置 --> <bean id="people" class="cn.ljy.clazz.People"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="cars"> <!-- 使用list節點爲集合屬性賦值 --> <list> <ref bean="car"/> <ref bean="car2"/> <ref bean="car3"/> </list> </property> </bean>
執行結果
1.7使用Map集合配置屬性
NewPeople
package cn.ljy.clazz; import java.util.Map; import java.util.Properties; public class NewPeople { private String name; private int age; private Map<String,Car> cars; //配置properties屬性 private Properties properties; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Map<String, Car> getCars() { return cars; } public void setCars(Map<String, Car> cars) { this.cars = cars; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "NewPeople [name=" + name + ", age=" + age + ", cars=" + cars + "]"; } }
<!--Map集合屬性值的配置 --> <bean id="people2" class="cn.ljy.clazz.NewPeople"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="cars"> <map> <entry key="OneCar" value-ref="car"></entry> <entry key="TwoCar" value-ref="car2"></entry> <entry key="Three"> <bean class="cn.ljy.clazz.Car"> <property name="brand" value="法拉利"></property> <property name="business" value="上海"></property> <property name="price" value="4000000"></property> </bean> </entry> </map> </property> </bean>
以上就是一些基本的經常使用的依賴注入了,還有幾個例子我會在下面的代碼中貼出來,由於用的很少,因此就不作詳細的講解了
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 將 HelloWord 類交給Spring容器進行管理--> <!-- 經過屬性的方式傳入參數 --> <bean id="HelloWord" class="cn.ljy.clazz.HelloWord"> <!-- 爲參數賦值 --> <property name="name" value="巴黎的雨季"></property> </bean> <!-- 經過構造器的方式傳入參數 --> <bean id="AttributeByConstructor1" class="cn.ljy.clazz.HelloWord"> <!-- 經過構造器方式傳參 --> <constructor-arg value="巴黎的雨季" index="0"/> <constructor-arg value="男" index="1"/> <constructor-arg value="18" type="int"/> </bean> <!--給第二個構造器賦值 --> <bean id="AttributeByConstructor2" class="cn.ljy.clazz.HelloWord"> <!-- 經過構造器方式傳參 --> <constructor-arg value="巴黎的雨季" index="0"/> <constructor-arg name="sex" value="男" /> <constructor-arg value="130" type="double"/> </bean> <!-- 監管Car 準備三car--> <bean id="car" class="cn.ljy.clazz.Car"> <property name="brand" value="奔馳"></property> <property name="business" value="上海"></property> <property name="price" value="300000"></property> </bean> <bean id="car2" class="cn.ljy.clazz.Car"> <property name="brand" value="大衆"></property> <property name="business" value="廣州"></property> <property name="price" value="120000"></property> </bean> <bean id="car3" class="cn.ljy.clazz.Car"> <property name="brand" value="雪佛蘭"></property> <property name="business" value="深圳"></property> <property name="price" value="150000"></property> </bean> <!-- 配置person 引用Car --> <bean id="person" class="cn.ljy.clazz.Person"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> </bean> <!-- 內部bean --> <bean id="person2" class="cn.ljy.clazz.Person"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> <!-- 注意:內部bean只能在內部使用,不能被外部所引用 --> <property name="car" > <bean class="cn.ljy.clazz.Car"> <property name="brand" value="福特"></property> <property name="business" value="北京"></property> <property name="price" value="400000"></property> </bean> </property> </bean> <!-- 級聯屬性的使用 --> <bean id="person3" class="cn.ljy.clazz.Person"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> <!-- 級聯,改變原來的值 注意:必須是存在car對象才能夠,Spring不會自動的建立car --> <property name="car.brand" value="勞斯萊斯"></property> </bean> <!-- list集合屬性的配置 --> <bean id="people" class="cn.ljy.clazz.People"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="cars"> <!-- 使用list節點爲集合屬性賦值 --> <list> <ref bean="car"/> <ref bean="car2"/> <ref bean="car3"/> </list> </property> </bean> <!--Map集合屬性值的配置 --> <bean id="people2" class="cn.ljy.clazz.NewPeople"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="cars"> <map> <entry key="OneCar" value-ref="car"></entry> <entry key="TwoCar" value-ref="car2"></entry> <entry key="Three"> <bean class="cn.ljy.clazz.Car"> <property name="brand" value="法拉利"></property> <property name="business" value="上海"></property> <property name="price" value="4000000"></property> </bean> </entry> </map> </property> </bean> <!-- 配置properties屬性--> <bean id="people3" class="cn.ljy.clazz.NewPeople"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">123</prop> <prop key="jdbcurl">jdbc:mysql:/test</prop> <prop key="drivarClass">com.mysql.jdbc.java</prop> </props> </property> </bean> <!-- 獨立出一個bean 好比說list 須要引入新的命名空間 util--> <!-- <util:list id="cars"> <ref bean="car"/> <ref bean="car2"/> <ref bean="car3"/> </util> <bean id="people4" class="cn.ljy.clazz.People"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="28"></property> <property name="cars" ref="cars"></property> </bean> --> <!-- 自動裝配 可使用autowire屬性指定自動裝配的方式 byName 根據Bean的名字和當前Bean的setter風格的屬性名進行自動裝配,如有匹配的,則進行自動裝配 ,若沒有匹配的則不進行裝配 byType 根據Bean的類型和當前Bean的屬性的類型進行自動裝配,若IOC容器中定義了多個類型匹配的Bean 則會拋出異常 --> <bean id="person4" class="cn.ljy.clazz.Person" autowire="byName"> <property name="name" value="巴黎的雨季"></property> <property name="age" value="18"></property> </bean> </beans>