javascript
SpringIOC的依耐注入DI---set注入---constructor有参构造注入---了解P命名空间---了解SpEL
依耐注入DI
DI 依耐注入通俗來將就是給對象的成員變量(屬性) 賦值依耐注入的兩種方式
? set注入
? 構造器注入
set注入
set注入(主流)
? 名稱:property
? 類型:標簽
? 歸屬:bean標簽
? 作用:使用set方法的形式為bean提供資源
? 格式:
< bean>
< property />
< /bean>
? 基本屬性:
< property name=“propertyName” value=“propertyValue” ref=“beanId”/>
◆ name:對應bean中的屬性名,要求該屬性必須提供可訪問的set方法(嚴格規范為此名稱是set方法對應名稱)
◆ value:設定非引用類型屬性對應的值,不能與ref同時使用
◆ ref:設定引用類型屬性對應bean的id ,不能與value同時使用
? 注意:一個bean可以有多個property標簽
實體類準備
創建一個person類,并創建3個屬性,2個基本類型,一個引用類型,并提供set方法(讓DI依耐注入)
package com.fs.di; /* spring的DI依耐注入*/ public class Person {private String name;private int age;private Student student;//因為我們DI的注入方式為set注入,所以生成一些set方法public void setName(String name) {/*為什么spring要使用set呢,不直接反射獲得屬性直接復制如果我們使用set的話,可以在set中做校驗,比如我這里的名字,我可以在set方法中做判斷*/if (name.length()<2){throw new RuntimeException("注入的名字的字數請大于2位~~~");}this.name = name;}public void setAge(int age) {this.age = age;}public void setStudent(Student student) {this.student = student;}public Student getStudent() {return student;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", student=" + student +'}';} }創建一個類,這個類是person中的一個屬性,目的就是體現依耐注入可以給實體類屬性注入值
package com.fs.di;public class Student {public void study(){System.out.println("好好學習!天天向上");} }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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DI 依耐注入通俗來將就是給對象的成員變量(屬性) 賦值 --><!-- 把student交給spring管理--><bean id="student" class="com.fs.di.Student"/><!--把person交給spring管理,并且set給person的屬性賦值--><bean id="person" class="com.fs.di.Person"> <!-- 基本數據類型--><property name="name" value="小付"/><property name="age" value="18"/> <!-- ref是引用IOC中已有的bean,從ioc容器中獲取對應的類將其注入這里注入的就是上面的student--><property name="student" ref="student"/></bean> </beans>測試方法
//測試DI依耐注入set方法注入@Testpublic void DISet(){//創建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//先從ioc中獲取student對象Student student = (Student) applicationContext.getBean("student");//從ioc容器中獲取對象Person person = (Person) applicationContext.getBean("person");//得到person中的student對象Student personStudent = person.getStudent();//判斷一下兩個對象是否是一個,是一個,說明是在配置文件中注入的student,都是從ioc中獲取的System.out.println(student == personStudent);//true//打印輸出一下System.out.println(person);/*由配置文件得知person是spring通過set注入的,在配置文件通過ref引用的上面配置的student所以,這兩個student地址值肯定是一樣的,因為springIOC默認是單例模式*/}控制臺輸出結果
構造器注入
構造器注入(了解)
? 名稱:constructor-arg
? 類型:標簽
? 歸屬:bean標簽
? 作用:使用構造方法的形式為bean提供資源,兼容早期遺留系統的升級工作
? 格式:
< bean>
< constructor-arg />
< /bean>
? 基本屬性:
<constructor-arg name=“argsName” value="argsValue />
◆ name:對應bean中的構造方法所攜帶的參數名
◆ value:設定非引用類型構造方法參數對應的值,不能與ref同時使用
? 注意:一個bean可以有多個constructor-arg標簽
? 其他屬性:
< constructor-arg index=“arg-index” type=“arg-type” ref=“beanId”/>
◆ ref:設定引用類型構造方法參數對應bean的id ,不能與value同時使用
◆ type :設定構造方法參數的類型,用于按類型匹配參數或進行類型校驗
◆ index :設定構造方法參數的位置,用于按位置匹配參數,參數index值從0開始計數
實體類準備
創建一個animal類,創建3個屬性,一個屬性為dog實體類,提供一個構造方法供給DI依耐注入
package com.fs.di; /* spring使用有參構造給屬性賦值*/ public class Animal {private String name;private String genre;private Dog dog;//創建一個有參構造public Animal(String name, String genre, Dog dog) {this.name = name;this.genre = genre;this.dog = dog;}public Dog getDog() {return dog;}@Overridepublic String toString() {return "Animal{" +"name='" + name + '\'' +", genre='" + genre + '\'' +'}';} }dao實體類
package com.fs.di;public class Dog {public void testDog(){System.out.println("汪汪汪~~~");} }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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DI 依耐注入通俗來將就是給對象的成員變量(屬性) 賦值 --> <!-- 將dog交給ioc管理--><bean id="dog" class="com.fs.di.Dog"/> <!-- 將Animal交給ioc管理,使用有參構造創建Animal bean --><bean id="animal" class="com.fs.di.Animal"> <!-- name 構造方法參數的名 value 基本數據 ref 引用ioc已有的類--><constructor-arg name="name" value="旺財"/><constructor-arg name="genre" value="柴犬"/><constructor-arg name="dog" ref="dog"/></bean> </beans>測試方法
//測試有參構造將類交給ioc管理@Testpublic void testConstructor(){//創建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//獲取animalAnimal animal = (Animal) applicationContext.getBean("animal");//獲取spring通過有參構造方法注入的dogDog dog = animal.getDog();dog.testDog();//輸出一下spring通過有參構造注入的屬性System.out.println(animal);}控制臺輸出結果
集合類型數據注入
集合類型數據注入
? 名稱:array,list,set,map,props
? 類型:標簽
? 歸屬:property標簽 或 constructor-arg標簽
? 作用:注入集合數據類型屬性
? 格式:
< property>
< list>< /list>
< /property>
實體類準備
創建一個類,提供三個屬性,類型為int[]和List< String>和HashMap< String,Object> ,提供set方法讓DI依耐注入
package com.fs.list;import java.util.Arrays; import java.util.HashMap; import java.util.List;/* 集合類型的數據注入 set方式注入*/ public class CollectionDI {private int[] array;private List<String> list;private HashMap<String,Object> map;public void setArray(int[] array) {this.array = array;}public void setList(List<String> list) {this.list = list;}public void setMap(HashMap<String, Object> map) {this.map = map;}@Overridepublic String toString() {return "CollectionDI{" +"array=" + Arrays.toString(array) +", list=" + list +", map=" + map +'}';} }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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DI 依耐注入通俗來將就是給對象的成員變量(屬性) 賦值 --><!-- 把student交給spring管理--><bean id="student" class="com.fs.di.Student"/><!-- set方式注入數組集合--><bean id="collection" class="com.fs.list.CollectionDI"><property name="array"><array><value>10</value><value>20</value></array></property><property name="list"><list><value>小付</value><value>小花</value></list></property><property name="map"><map><entry key="學生map" value="學生map的值"/><entry key="學生" value-ref="student"/></map></property></bean> </beans>測試方法
//set注入集合類型@Testpublic void testCollection(){//創建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");CollectionDI collection = (CollectionDI) applicationContext.getBean("collection");System.out.println(collection);}控制臺輸出結果
使用p命名空間簡化配置
applicationContext.xml配置文件
<!-- p命名空間 了解xmlns:p="http://www.springframework.org/schema/p"需要引入命名空間--><bean id="personP" class="com.fs.di.Person"p:name="小花"p:age="18"p:student-ref="student"/>SpEL
applicationContext.xml配置文件
<!--Spring提供了對EL表達式的支持,統一屬性注入格式注意:所有屬性值不區分是否引用類型,統一使用value賦值--><bean id="personEL" class="com.fs.di.Person"><property name="name" value="#{'小李'}"/><property name="age" value="#{12}"/><property name="student" value="#{student}"/></bean>總結
以上是生活随笔為你收集整理的SpringIOC的依耐注入DI---set注入---constructor有参构造注入---了解P命名空间---了解SpEL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringIOC配置文件「bean」标
- 下一篇: spring核心配置文件引入外部prop