Java Spring WEB應用實例化如何實現
1.前面講解的都是通過直接讀取配置文件,進行的實例化ApplicationContext
AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
下面講解直接通過配置文件進行初始化。
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便已經實例化了,默認就直接加載了beans.xml里面的內容。
來看看底層的代碼,類ContextLoaderListener中有個初始化方法
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
進入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);
這句也就是容器加載的結果。
1和2一個是java代碼一個是xml代碼,不過實現的效果都是一樣的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Java基礎之spring5新功能學習
- Java Spring5學習之JdbcTemplate詳解
- Java基礎之Spring5的核心之一IOC容器
- SpringBoot+JavaMailSender實現騰訊企業(yè)郵箱配置
- Spring通過Java配置集成Tomcat的方法
- java、spring、springboot中整合Redis的詳細講解
- Java中Spring Boot支付寶掃碼支付及支付回調的實現代碼
- Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗注解
- 最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應用(附源碼)
- Java Spring數據單元配置過程解析
- Java Spring事務使用及驗證過程詳解
- Spring5學習之基礎知識總結
相關文章
idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結合的方式給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-04-04
IDEA的spring項目使用@Qualifier飄紅問題及解決
這篇文章主要介紹了IDEA的spring項目使用@Qualifier飄紅問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot+SpringSecurity 不攔截靜態(tài)資源的實現
這篇文章主要介紹了SpringBoot+SpringSecurity 不攔截靜態(tài)資源的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
圖解Java?ReentrantLock的條件變量Condition機制
想必大家都使用過wait()和notify()這兩個方法把,他們主要用于多線程間的協(xié)同處理。而RenentrantLock也支持這樣條件變量的能力,而且相對于synchronized?更加強大,能夠支持多個條件變量,本文就來詳細說說2022-10-10
Java實現stream的三個常用方式(toMap,groupingBy,findFirst)
本文主要介紹了Java實現stream的三個常用方式,主要包括toMap,groupingBy,findFirst,具有一定的參考價值,感興趣的可以了解一下2023-10-10

