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)容。
來看看底層的代碼,類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. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
