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. ASP常用日期格式化函數(shù) FormatDate()2. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式3. PHP設(shè)計(jì)模式中工廠模式深入詳解4. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)5. XML入門的常見問(wèn)題(二)6. 如何在jsp界面中插入圖片7. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法8. 利用CSS3新特性創(chuàng)建透明邊框三角9. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法10. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法
