spring進階(一)之配置文件解析

1. spring 做junit測試時,需要從配置文件中創建ApplicationContext對象

     整合到web項目中時,通過配置ContextLoaderListener來裝配ApplicationContext的配置信息。

     applictionContext的作用:

作用1:責讀取bean配置文檔,管理bean的加載,實例化,維護bean之間的依賴關係,負責bean的聲明週期。
  作用2:提供更完整的框架功能:
   a. 國際化支持
   b. 資源訪問:Resource rs = ctx. getResource(“classpath:config.properties”),“file:c:/config.properties”
   c. 事件傳遞:通過實現ApplicationContextAware接口

    contextLoaderListener的作用(web.xml中):

啓動Web容器時,自動裝配ApplicationContext的配置信息。
因爲它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。

contextLoaderListener一般配合contextConfigLocation一起使用,來加載spring配置文件,否則會默認查找/WEB-INF/applicationContext.xml路徑下的配置文件。

---------至此spring在web.xml中的配置結束--------------------------------


2. 配置springmvc

      springmvc的核心控制器:DispatcherServlet

       contextConfigLocation配置文件路徑

       配置攔截器


3.  配置亂碼編碼

     

<!-- 亂碼解決 -->
	  <filter>  
	        <filter-name>characterEncodingFilter</filter-name>  
	        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
	        <init-param>  
	            <param-name>encoding</param-name>  
	            <param-value>UTF-8</param-value>  
	        </init-param>  
	        <init-param>  
	            <param-name>forceEncoding</param-name>  
	            <param-value>true</param-value>  
	        </init-param>  
	 </filter>  
	    <filter-mapping>  
	        <filter-name>characterEncodingFilter</filter-name>  
	        <url-pattern>/*</url-pattern>  
	    </filter-mapping>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章