內容包括:配置Bean、spring容器、獲取bean 、屬性注入 、bean的作用域 、spring使用外部屬性文件
一:配置Bean
class:bean的全類名,通過反射的方式在IOC容器中創建bean,所以要求Bean中必須有無參數的構造器。
id:標識容器中的bean的id
<bean id="HelloWorld" class="com.wh.spring.beans.HelloWorld">
<property name="name2" value="spring"></property>
二:spring容器
beanFactory是IOC的基本實現,是Spring框架的基礎設施,面向spring本身。
ApplicationContext是beanFactory的子接口,面向spring框架的開發者。
無論使用何種方式,配置文件時相同的。
•ConfigurableApplicationContext 擴展於 ApplicationContext,新增加兩個主要方法:refresh() 和 close(), 讓 ApplicationContext 具有啓動、刷新和關閉上下文的能力
•ApplicationContext 在初始化上下文時就實例化所有單例的 Bean。
•WebApplicationContext 是專門爲 WEB 應用而準備的,它允許從相對於 WEB 根目錄的路徑中完成初始化工作
–ClassPathXmlApplicationContext:從 類路徑下加載配置文件
–FileSystemXmlApplicationContext: 從文件系統中加載配置文件
三:獲取bean
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
HeloWord helloword=(HeloWord) ctx.getBean("HelloWorld");
也可以使用getBean(HelloWorld.class),這時候不需要強制類型轉換。
//根據類型來獲取 bean 的實例: 要求在 IOC 容器中只有一個與之類型匹配的 bean, 若有多個則會拋出異常.
//一般情況下, 該方法可用, 因爲一般情況下, 在一個 IOC 容器中一個類型對應的 bean 也只有一個.
// HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
四:屬性注入
setter方法注入。
<property name="user" value="Tom"></property>
constructor-arg注入
<constructor-arg value="Mike"></constructor-arg>
可以通過構造器屬性的類型和參數的位置區分重載的構造器。
<bean id="car" class="com.wh.spring.beans.Car">
<constructor-arg type="java.lang.String">
<value><![CDATA[<SHANGHAI>]]></value>
</constructor-arg>
</bean>
字面值包含特殊字符,使用<![CDATA]>包含。
bean引用:
<!-- 通過 ref 屬性值指定當前屬性指向哪一個 bean! -->
<property name="dao" ref="dao5"></property>
內部bean
<!-- 聲明使用內部 bean -->
<bean id="service2" class="com.atguigu.spring.ref.Service">
<property name="dao">
<!-- 內部 bean, 類似於匿名內部類對象. 不能被外部的 bean 來引用, 也沒有必要設置 id 屬性 -->
<bean class="com.atguigu.spring.ref.Dao">
<property name="dataSource" value="c3p0"></property>
</bean>
</property>
</bean>
賦空值:
<constructor-arg><null/></constructor-arg>
爲級聯屬性賦值:
<property name="service" ref="service2"></property>
<!-- 設置級聯屬性(瞭解) -->
<property name="service.dao.dataSource" value="DBCP2"></property>
裝配集合屬性:
spring可以通過list,map,set裝配集合屬性。
<!-- 裝配集合屬性 -->
<bean id="user" class="com.atguigu.spring.helloworld.User">
<property name="userName" value="Jack"></property>
<property name="cars">
<!-- 使用 list 元素來裝配集合屬性 -->
<list>
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean>
util聲明:
<!-- 聲明集合類型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
自動裝配:
byType: 根據類型進行自動裝配. 但要求 IOC 容器中只有一個類型對應的 bean, 若有多個則無法完成自動裝配.
byName: 若屬性名和某一個 bean 的 id 名一致, 即可完成自動裝配. 若沒有 id 一致的, 則無法完成自動裝配
在 Bean 配置文件裏設置 autowire 屬性進行自動裝配將會裝配 Bean 的所有屬性. 然而, 若只希望裝配個別屬性時, autowire 屬性就不夠靈活了.
autowire 屬性要麼根據類型自動裝配, 要麼根據名稱自動裝配, 不能兩者兼而有之.
使用bean的parent屬性進行配置繼承,。
<bean id="parent" class="xx.xx.xx"></bean>
<bean id="child" parent="parent"></bean>
Spring 允許繼承 bean 的配置, 被繼承的 bean 稱爲父 bean. 繼承這個父 Bean 的 Bean 稱爲子 Bean
子 Bean 從父 Bean 中繼承配置, 包括 Bean 的屬性配置
子 Bean 也可以覆蓋從父 Bean 繼承過來的配置
父 Bean 可以作爲配置模板, 也可以作爲 Bean 實例. 若只想把父 Bean 作爲模板, 可以設置 <bean> 的abstract 屬性爲 true, 這樣 Spring 將不會實例化這個 Bean
並不是 <bean> 元素裏的所有屬性都會被繼承. 比如: autowire, abstract 等.
也可以忽略父 Bean 的 class 屬性, 讓子 Bean 指定自己的類, 而共享相同的屬性配置. 但此時 abstract 必須設爲 true
五:bean的作用域
單例bean:scope="singleton"(默認情況)
每次向容器獲取bean,都返回同一個bean,創建容器時創建bean.
原型bean:scope="prototype"
每次向容器獲取bean,都返回一個新創建的bean,創建容器時不創建bean
六:spring使用外部屬性文件
Spring 提供了一個 PropertyPlaceholderConfigurer 的 BeanFactory 後置處理器, 這個處理器允許用戶將 Bean 配置的部分內容外移到屬性文件中. 可以在 Bean 配置文件裏使用形式爲 ${var} 的變量, PropertyPlaceholderConfigurer 從屬性文件里加載屬性, 並使用這些屬性來替換變量.
<context:property-placeholder
location="classpath:db.properties"/>