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