Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
1.前面講解的都是通過(guò)直接讀取配置文件,進(jìn)行的實(shí)例化ApplicationContext
AbstractApplicationContext app = new ClassPathXmlApplicationContext('beans.xml');
下面講解直接通過(guò)配置文件進(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代碼,不過(guò)實(shí)現(xiàn)的效果都是一樣的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Ajax返回值類型與用法實(shí)例分析3. .NET6打包部署到Windows Service的全過(guò)程4. Vue element ui用戶展示頁(yè)面的實(shí)例5. 如何在jsp界面中插入圖片6. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)7. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)8. asp批量添加修改刪除操作示例代碼9. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼10. css代碼優(yōu)化的12個(gè)技巧
