Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
1.前面講解的都是通過直接讀取配置文件,進(jìn)行的實(shí)例化ApplicationContext
AbstractApplicationContext app = new ClassPathXmlApplicationContext('beans.xml');
下面講解直接通過配置文件進(jìn)行初始化。
2.web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
這樣,ApplicationContext便已經(jīng)實(shí)例化了,默認(rèn)就直接加載了beans.xml里面的內(nèi)容。
來(lái)看看底層的代碼,類ContextLoaderListener中有個(gè)初始化方法
public void contextInitialized(ServletContextEvent event) { this.contextLoader = createContextLoader(); if (this.contextLoader == null) { this.contextLoader = this; } this.contextLoader.initWebApplicationContext(event.getServletContext()); }
進(jìn)入initWebApplicationContext方法 :
ApplicationContext parent = loadParentContext(servletContext); // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. this.context = createWebApplicationContext(servletContext, parent);
這句也就是容器加載的結(jié)果。
1和2一個(gè)是java代碼一個(gè)是xml代碼,不過實(shí)現(xiàn)的效果都是一樣的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS百分比padding制作圖片自適應(yīng)布局2. react實(shí)現(xiàn)組件狀態(tài)緩存的示例代碼3. CSS清除浮動(dòng)方法匯總4. React優(yōu)雅的封裝SvgIcon組件示例5. Vue如何使用ElementUI對(duì)表單元素進(jìn)行自定義校驗(yàn)及踩坑6. Electron調(diào)用外接攝像頭并拍照上傳實(shí)現(xiàn)詳解7. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))8. 不要在HTML中濫用div9. HTML5 Canvas繪制圖形從入門到精通10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
