Spring的IOC的xml配置
* 獲取spring的ioc核心容器,並根據id獲取對象
* ApplicationContext的三個常用實現類
* ClassPathXmlApplicationContext
* 它可以加載類路徑下的配置文件,要求配置文件必須在類路徑下,不在的話,加載不了
* FileSystemXmlApplicationContext
* 它可以加載磁盤任意路徑下的配置文件(必須有訪問權限)
* AnnotationConfigApplicationContext
* 它是用於讀取註解創建容器的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.yang.service.impl.AccountServiceImpl"/>
<bean id="accountDao" class="com.yang.dao.impl.AccountDaoImpl"/>
</beans>
bean標籤的scope屬性:
作用: 用於指定bean的作用範圍
取值: 常用的就是單例和多例的
singleton: 單例(默認值)
prototype: 多例的
request: 作用於web應用的請求範圍
session: 作用於web應用的會話範圍
global-session: 作用於集羣環境的會話範圍(全局會話): 當不是集羣環境時,他就是session
bean對象的生命週期
單例對象
出生: 當容器創建時對象出生
活着: 只要容器還在,對象一直活着
死亡: 容器銷燬,對象消亡
總結: 單例對象的生命週期和容器相同
多例對象
出生: 當我們使用對象時spring爲我們創建
活着: 對象只要在使用過程中就一直活着
死亡: 當對象長時間不用且沒有別的對象引用時,由java的垃圾回收機制回收