spring中的BeanFactory與ApplicationContext的作用和區別?


作用:

1. BeanFactory負責讀取bean配置文檔,管理bean的加載,實例化,維護bean之間的依賴關系,負責bean的聲明周期。
2. ApplicationContext除了提供上述BeanFactory所能提供的功能之外,還提供了更完整的框架功能:

a. 國際化支持
b. 資源訪問:Resource rs = ctx. getResource(“classpath:config.properties”), “file:c:/config.properties”
c. 事件傳遞:通過實現ApplicationContextAware接口
3. 常用的獲取ApplicationContext的方法:
FileSystemXmlApplicationContext:從文件系統或者url指定的xml配置文件創建,參數為配置文件名或文件名數組
ClassPathXmlApplicationContext:從classpath的xml配置文件創建,可以從jar包中讀取配置文件
WebApplicationContextUtils:從web應用的根目錄讀取配置文件,需要先在web.xml中配置,可以配置監聽器或者servlet來實現

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

 


這兩種方式都默認配置文件為web-inf/applicationContext.xml,也可使用context-param指定配置文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM