Spring創建Bean過程的方法
1. beans包提供了以編程方式管理和操作bean的基本功能,而context包增加了ApplicationContext,它以一種更加面向框架的方式增強了BeanFactory的功能。
2. context包的基礎是位於ext包中的ApplicationContext接口。它是由 BeanFactory接口派生而來的,提供了BeanFactory的所有功能。爲了以一種更面向框架的方式工作,使用分層和繼承關係的上下文,context包還提供了一下的功能。
a. MessageSource,對I18N消息的訪問。
b. 資源訪問,例如URL和文件
c. 事件傳遞給是吸納了ApplicationListener接口的bean
d. 載入多個(有繼承關係)上下文,使得每一個上下文都專注於一個特定的層是,比如應用的web層。
國際化支持
1. ApplicationContext擴展了MessageSource接口,因而提供了messaging的功能(I18N或者國際化)。同 NestingMessageSource一起使用,還能處理分級的信息,這些是spring提供的處理信息的基本接口。
2. 當一個ApplicationContext被加載時,它會自動查找在context中定義的MessageSource bean.這個bean必須交做messageSource.如果找了這樣一個bean,所有對上述方法的調用將被委託給找到的 messageSource.如果沒有找到messageSource,ApplicationContext將會嘗試查找他的父親是否包含有同名的 bean.如果有,它將把找到的bean作爲MessageSource.如果他沒有找到任何的信息處理源,他會創建一個 StaticMessageSource.
3. Spring目前提供了兩個MessageSource的實現,他們是
ResourceBundleMessageSource和StaticMessageSource.兩者都實現了 NestingMessageSource一邊能夠處理嵌套的信息。StaticMessageSource很少被使用,但是他提供了編程的方式向 source增加信息,我們經常使用的是ResourceBundleMessageSource.
在Spring中使用資源
1. ApplicationContext繼承了ResourceLoader接口,在這個接口中,定義了getResource()方法,如下:
Resource getResource(String location)
該方法返回一個資源句柄。這個句柄應該總是一個可重複使用的資源描述符,允許多次調用getInputStream();
2. getResource()方法的`參數是一個資源訪問地址,例如:
file:c:/
classpath:(從classpath路徑中查找文件並返回他的資源句柄)
WEB-INF/
注意:getResource()方法返回的Resource句柄並不意味着資源實際存在,你需要調用Resource接口的exists()方法判斷資源是否存在。
Resource提供了與協議無關的特性。
事件傳遞
1. ApplicationContext中的時間處理是通過AppcationEvent類和ApplicationListener接口來提供的。如果上下文中部署了一個實現了ApplicationListener接口的bean,每次一個ApplicationEvent發佈到 ApplicationContext時,那個bean就會被通知。實質撒謊功能,這是標準的Observer設計模式。
2. Spring提供的三個標準事件
a. ContextRefreshedEvent
當ApplicationContext已經初始化或刷新後發送的事件。這裏初始化意味着:所有的bean被裝載,singleton被預實例化,以及ApplicationContext已經準備好。
b. ContextClosedEvent
當使用ApplicationContext的close()方法結束上下文的時候發送的事件。這裏意味着:singleton被銷燬。
c. RequestHandledEvent
一個與web相關的事件,告訴所有的bean一個HTTP請求已經被響應了(這個時間將會在一個請求結束後被髮送—)。注意,這個時間只能應用於使用了Spring的DispatcherServlet的web應用。
相關文章
-
Passage Eighteen (Strictly Ban smoking)英語閱讀理解
英語閱讀理解100篇(基礎篇)-18Passage Eighteen (Strictly Ban smoking)If you smoke and you still don’t believe that there’s a definite link between smoking and bronchial troubles, heart disease and lung -
英語口語:Teaching and Learning Responsibility
“Teaching a man to fish, and you feed him for a lifetime.” It sounds good in theory. But is it realistic to make your kids “work”? They are supposed to study or play or do something other than “w -
牛津譯林版九年級上冊英語 Unit 3 Teenage problems-Reading 同步練習
一、語法填空 is short for the most ________ (value) player in a game. should find out more information before making your final _______ (choose).ks for offering us such useful _________ (suggest). -
雅思口語範文:My Experience as an English Learner
I began leaing English in the middle school. Soon I became very interested in it. English exams were very easy to me and I always got the highest marks in my class. Then I succeeded in the College Ent -
Java認證考試知識點:Spring獲取Bean四種方法
下面YJBYS小編爲大家整理了Spring獲取Bean四種方法,希望對你有所幫助。更多Java認證考試信息,盡在應屆畢業生培訓網! 方法一:通過Spring提供的工具類獲取ApplicationContext對象代碼:import pplicationContextUtils;Ap -
Brain training rules大腦訓練法則英語閱讀
our brain is like muscles. the more you use and exercise it, the stronger it becomes. but, if you want to get maximum results, you should do more than just practice. what you need is deliberate practi -
英語閱讀:Covergirl unveils empowering brand update
It may have been founded 60 years ago old, but US cosmetics giant Covergirl is proving that you're never too old to reinvent yourself.The Coty-owned makeup brand, which has been attracting attenti -
Unit six Opening and Closing Ceremonies口譯材料
Unit six Opening and Closing CeremoniesLesson 1女士們,先生們,上午好!我很榮幸地代表戴比爾斯公司歡迎你們來到美麗的大連參加“中國鑽石節”。//藉此機會我想介紹一下出席大會的代表們(介紹代表們的名字、職位和簡要 -
英語閱讀:Web Boosts Grandpas Brain
每個國小生都想成爲十佳學生,成爲十佳少年之後要做好自我介紹給大家留個好印象。下面是本站小編整理的國小生十佳少年自我介紹,歡迎大家閱讀。國小生十佳少年自我介紹篇1尊敬的校領導、老師、同學們:我是三年級一班的學 -
Diaspora Finance Powers Global Development美文欣賞
Diaspora Finance Powers Global DevelopmentLuis Lopez-Ramirez, a 30-year old Honduran immigrant, makes pizza.He has worked two jobs over 16 hours each day.He carefully saves his money and rents a small