DI依賴注入傳入參數的方式,這里介紹了基本數據類型,集合,符合數據類型的傳遞(String類型比較特殊,其傳遞值和基本數據類型的方法一致)
注入基本數據類型和String類型
通過setter方法注入基本數據類型與String
案例:
<bean id="book" class="cn.xdl.bean.Book"> <!-- 對於基本數據類型 與 String的注入操作 通過set方法 ,完成注入 name: 屬性的名稱 value: 屬性要賦的值 <null/>: 節點表示null。 --> <property name="bookName" value="西游記"></property> <property name="bookId" value="10001"></property> <!-- null值 --> <property name="x"> <null/> </property> </bean>
通過構造方法 ,注入基本數據類型與String
方式1:
<bean id="book" class="cn.xdl.bean.Book"> <!-- 通過構造器 傳入形參名稱 與 value值, 進行參數的注入--> <constructor-arg name="bn" value="紅樓夢"></constructor-arg> <constructor-arg name="bi" value="10002"></constructor-arg> </bean>
方式2:
<bean id="book" class="cn.xdl.bean.Book"> <!-- 通過構造器 傳入形參列表的索引 ,與value進行參數的注入--> <constructor-arg index="0"> <null/> </constructor-arg> <constructor-arg index="1" value="10003"></constructor-arg> </bean>
注入集合類型的數據
注入List集合
<property name="v"> <!--向List集合 注入值--> <list> <!--每一個value節點, 表示集合中的一個元素--> <value>A</value> <value>B</value> <value>C</value> <value>D</value> <value>E</value> <value>F</value> </list> </property>
注入Set集合
<property name="v"> <!--向set集合 注入值--> <set> <!--每一個value節點, 表示集合中的一個元素--> <value>A</value> <value>B</value> <value>C</value> <value>D</value> <value>E</value> <value>F</value> </set> </property>
注入Map集合
<property name="v"> <map> <!--每一個enrty表示集合中的一個鍵值對--> <entry key="jiyou1" value="map1"></entry> <entry key="jiyou2" value="map2"></entry> </map> </property>
注入Properties集合
<property name="v"> <!--對於properties的注入 ,value是編寫在prop節點的文本內容區域的 每一個prop都表示proerties存儲的一個鍵值對的數據--> <props> <prop key="v1">A</prop> <prop key="v2">B</prop> <prop key="v3">C</prop> </props> </property>
通過引入的方式, 實現集合的重復利用
原理:
1. 配置一個集合對象到容器中, 並指定ID
2. 在其他對象使用時, 直接通過對象注入的方式, 將其注入即可
案例:
向容器中添加集合對象:
<util:set id="list"> <value>張三</value> <value>李四</value> <value>王二</value> <value>麻子</value> </util:set>
將對象注入其他對象中:
<bean id="person" class="cn.xdl.bean.Person"> <property name="refriendship" ref="list"></property> </bean>
對象的注入
通過Setter方法設置屬性
property節點: 通過set方法, 設置一個屬性的值
name屬性: 表示的是 當前類中的屬性的名稱
ref: 表示的是, 本次要注入的對象在當前容器中的id值
案例:
<bean id="bookinstance" class="BookClass"> <property name="book" ref="bookinstance"></property> </bean>
通過構造器設置屬性
constructor節點: 通過構造器 , 注入屬性的值
name屬性: 這是一個容易混淆的地方 , 它指的其實是構造方法中形式參數列表中參數的名稱
ref: 表示的是, 本次要注入的對象在當前容器中的id值
案例:
<bean id="bookinstance" class="BookClass"> <constructor-arg name="book" ref="bookinstance"></constructor-arg> </bean>
對象的自動裝配
我們可以在bean節點中, 添加autowire屬性來完成自動裝配的操作
屬性取值范圍:
1,no : 默認設置 , 表示關閉自動裝配
2,byName : 通過名稱完成自動裝配:
例如: 當前我們容器中存在一個對象, id為book;
而剛好當前的這個person類中有一個名稱為book的屬性
那么這個時候, 我們的容器, 會自動完成兩個對象的關聯
byName 不會判斷傳入的參數類型是否匹配, 只要名稱相同, 就會強行建立依賴關系 , 導致報錯!
3, byType : 通過類型完成自動裝配
例如: 當前我們容器中存在一個Book類的對象
而剛好我們當前的Person有一個Book類型的屬性
我們的容器, 就會自動完成兩個對象的關聯
4,constructor 與byType的方式類似,不同之處在於它應用於構造器參數
5,autodetect 通過bean類來決定是使用constructor還是byType方式進行自動裝配。如果發現默認的構造器,那么將使用byType方式
案例:
Person類:

package cn.xdl.bean; public class Person { private String name; private int age; private Book book; 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 Book getBook() { return book; } public void setBook(Book book) { this.book = book; } @Override public String toString() { return "這個人叫做:" + name + ", 今年" + age + "歲了, 他有一本書:" + book ; } public Person(String name, int age, Book book) { super(); this.name = name; this.age = age; this.book = book; } public Person() { super(); // TODO Auto-generated constructor stub } public Person(Book book) { super(); this.book = book; } }
applicationContext文件:

<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean id="book" class="cn.xdl.bean.Book"> <property name="bookName" value="水滸傳"></property> <property name="bookId" value="10001"></property> </bean> <!-- <bean id="person" class="cn.xdl.bean.Person"> 通過ref 關聯對象之間的關系 <constructor-arg name="book"> <null/> </constructor-arg> <constructor-arg name="name" value="張三"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <property name="book" ref="book"></property> </bean> --> <!-- 自動裝配 byName 通過屬性名稱 與 容器中對象的id 匹配 --> <bean id="person" class="cn.xdl.bean.Person" autowire="byName"> <property name="name" value="小李"></property> <property name="age" value="18"></property> </bean> </beans>
Test測試類:

package cn.xdl.test; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.xdl.bean.Person; import cn.xdl.test.util.ContextUtil; public class Test { @Test public void test() throws Exception { ClassPathXmlApplicationContext context = ContextUtil.getC("bean6.xml"); Person p= context.getBean("person", Person.class); System.out.println(p);//這個人叫做:小李, 今年23歲了, 他有一本書:書名=水滸傳, 編號=10001 } }
掃描組件:
掃描組件后,默認id值為類名首字母小寫,也可以自定義id。
下面是一些常用的@式,@后面的式可以寫值,也可以不寫,還有就是@Value(value="abc"),可以簡寫為@Value("abc")。
@Compenent 通用注解
@Repository 持久層組件注解
@Service 業務層組件注解
@Controller 控制層組件處理
@Value 可以注入指定變量的值
@Scope 可以指定對象的作用域singleton(單例模式,默認)、prototype(多例模式)、request、session、global Session,
@Transactional 表示事務
Spring表達式
這種表達式在語法上和EL非常像,可以讀取一個bean對象/集合中的數據
例如:
<!-- 進行掃描cn包 --> <context:component-scan base-package="cn"></context:component-scan> <!--從db.propertise文件中讀取配置信息--> <util:properties id="db" location="classpath:db.properties"/> <bean id="demo"class="com.xdl.bean.OracleDataSource"> <!--讀取name的值--> <property name=“username" value="#{db.name}"/> <!--讀取password--> <property name=“password"value="#{db.password}"/> <!--讀取url的值--> <property name=“url" value="#{db.url}"/>
<bean>