Spring——監聽器ContextLoaderListener作用
ContextLoaderListener作用:
在啟動Web 容器時,自動裝配 Spring applicationContext.xml 的配置資訊。
因為它實現了ServletContextListener 這個介面,在web.xml 配置這個監聽器,啟動容器時,就會預設執行它實現的方法。在ContextLoaderListener 中關聯了ContextLoader 這個類,所以整個載入配置過程由ContextLoader 來完成
spring在web下的入口在配置檔案web.xml的監聽器中
<listener>
<listener-class >
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/applicationContext.xml</param-value>
</context-param >
上述是在web.xml中的配置資訊。
實現了介面ServletContextListener,也就是說必須實現contextDestroyed, contextInitialized這兩個方法
public class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
//Spring框架由此啟動, contextInitialized也就是監聽器類的main入口函式
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
}
總的來說這個入口非常簡單,所有實現都隱藏在ContextLoader類裡。
ServletContextListener作用
ServletContext 被 Servlet 程式用來與 Web 容器通訊。例如寫日誌,轉發請求。每一個 Web 應用程式含有一個Context,被Web應用內的各個程式共享。因為Context可以用來儲存資源並且共享,所以我所知道的 ServletContext 的最大應用是Web快取—-把不經常更改的內容讀入記憶體,所以伺服器響應請求的時候就不需要進行慢速的磁碟I/O了。
ServletContextListener 是 ServletContext 的監聽者,如果 ServletContext 發生變化,如伺服器啟動時 ServletContext 被建立,伺服器關閉時 ServletContext 將要被銷燬。
在JSP檔案中,application 是 ServletContext 的例項,由JSP容器預設建立。Servlet 中呼叫 getServletContext()方法得到 ServletContext 的例項。
我們使用快取的思路大概是:
1. 伺服器啟動時,ServletContextListener 的 contextInitialized()方法被呼叫,所以在裡面建立好快取。可以從檔案中或者從資料庫中讀取取快取內容生成類,用 ervletContext.setAttribute()方法將快取類儲存在 ServletContext 的例項中。
2. 程式使用 ServletContext.getAttribute()讀取快取。如果是 JSP,使用a pplication.getAttribute()。如果是 Servlet,使用 getServletContext().getAttribute()。如果快取發生變化(如訪問計數),你可以同時更改快取和檔案/資料庫。或者你等 變化積累到一定程式再儲存,也可以在下一步儲存。
3. 伺服器將要關閉時,ServletContextListener 的 contextDestroyed()方法被呼叫,所以在裡面儲存快取的更改。將更改後的快取儲存迴文件或者資料庫,更新原來的內容。
import User; //my own
classimport DatabaseManager; // my own class
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
public class MyContextListener implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
User user = DatabaseManager.getUserById(1);
context.setAttribute("user1", user);
}
public void contextDestroyed(ServletContextEvent event) {
User user = (User)context.getAttribute("user1");
DatabaseManager.updateUserData(user);
this.context = null;
}
}
佈署 ServletContextListener
你實現(implements)了 ServletContextListener 編譯後,把它放在正確的WEB-INF/classes目錄下,更改WEB-INF目錄下的 web.xml檔案,在web-app節點裡新增
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
相關推薦
Spring——監聽器ContextLoaderListener作用
ContextLoaderListener作用: 在啟動Web 容器時,自動裝配 Spring applicationContext.xml 的配置資訊。 因為它實現了ServletContextListener 這個介面,在web.xml
spring監聽器ContextLoaderListener的疑問
今天新搭了專案,採用Spting和mybatis,MVC用的是Spring MVC ,配置檔案採用的是之前的專案的。 在執行後訪問專案時候報錯了: java.lang.IllegalStateException: No WebApplicationContext found
Spring中ContextLoaderListener作用
每一個整合spring框架的專案中,總是不可避免地要在web.xml中加入這樣一段配置。 <!-- Spring配置檔案開始 --> <context-param> <param-name>contextConfigLoca
Spring上下文ContextLoaderListener作用
1·ContextLoaderListener 用法 ContextLoaderListener監聽器的作用就是啟動Web容器時,自動裝配ApplicationContext的配置資訊。因為它實現了ServletContextListener這個介面,在web
Spring :監聽器ContextLoaderListener的作用
在開始使用Spring讀取配置檔案ApplicationContext.xml的時候沒有配置監聽器,在web.ml中配置如下: <context-param> <param-name>contextConfigLoc
[Spring]web.xml中配置ContextLoaderListener監聽器的作用
在spring的核心配置檔案中,為什麼配置ContextLoaderListener監聽器 <listener> <listener-class>org.springframework.web.context.ContextLoaderLi
Spring --17.Spring中的核心監聽器ContextLoaderListener
開發環境: JDK 1.8 Idea 2017 Tomcat:apache-tomcat-8 Spring:5.0.7 1、概述 ContextLoaderListener是Spring中的核心監聽器,當web工程啟動時, 該監聽器負責建立Spring的IOC容器,存放在Servle
spring中ContextLoaderListener的作用
ContextLoaderListener的作用是啟動Web容器時,自動裝配ApplicationContext的配置資訊。因為它實現了ServletContextListener這個介面,在web.xml配置這個監聽器,啟動容器時,就會預設執行它實現的方
Spring之ContextLoaderListener的作用
原文連結:http://blog.csdn.net/c5153000/article/details/6234207 [java] view plaincopyprint? Spring org.springframework.web.context.Con
web.xml中配置spring監聽器和spring配置文件位置
nco erl spring XML param onf ati spa extc <!-- spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</
spring各個jar作用
let 還需 request ons support epo col cati mock spring.jar --->包含完整發布模塊的單個jar,但是不包括mock.jar,aspects.jar,spring-porltet.jar,spring-hiberna
spring @component的作用
repo scan 組件 普通 class 方法 pub spring 初始 該文轉載自:http://tomfish88.iteye.com/blog/1497557 [email protected]/* */ 控制器(註入服務) [email p
spring之scope作用域
tle .cn lob height 模式 新建 應用 let image spring中,bean的作用域有五種類型:默認是單例模式, singleton prototype request session
ContextLoaderListener作用詳解
inter ble tle 分隔 name 在一起 vax rabl 自定義文件 ContextLoaderListener的作用後就是:加載spring 的上下文信息,這是個spring的類,但實現了javax的servletcontextListener接口。 Con
spring:bean的作用域
基於 一個 com single 常量 componet 上下文 blog ble spring:bean的作用域 Spring定義了多種作用域,可以基於這些作用域創建bean,包括:單例(Singleton):在整個應用中,只創建bean的一個實例。原型(Prototyp
描述一下Spring框架的作用和優點?
選擇 第三方框架 方案 16px clas style 方框 spring 簡化 Spring框架的作用和優點如下: 1.Spring是一個開源的輕量級的應用開發框架,其目的是用於簡化企業級應用程序開發,減少侵入; 2.Spring提供的IOC和AOP應用,可以
java----監聽器的作用
pre 我們 IT 執行過程 string gif 內容 傳遞 int 監聽器是JAVA Web開發中很重要的內容,其中涉及到的知識,可以參考下面導圖: Web監聽器 1 什麽是web監聽器? web監聽器是一種Servlet中的特殊的類,它們能幫助開
Spring bean的作用域
nbsp cnblogs imp 過程 erp dao 代碼 精確 只有一個 去面試,做面試題,有一題直接問請寫出spring bean的作用域,直接傻眼,回來趕緊補課 Bean的作用域 Spring 3中為Bean定義了5中作用域,分別為singleton(單
後端-框架-Spring-bean的作用域-未完成
後端-框架-Spring-bean的作用域 scope Value singleton 單例模式 prototype 每次建立新例項
spring常用註解作用小結
1、@controller 控制器(注入服務) 2、@service 服務(注入dao) 3、@repository dao(實現dao訪問) 4、@component (把普通pojo例項化到spring容器中,相當於配置檔案中的) @Component,@Service,@Control