1. 程式人生 > >Spring需要的幾個關鍵配置文件

Spring需要的幾個關鍵配置文件

eba lib name extc gmv 配置 apach apache logs

springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd
12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 <!-- 設置字符編碼 --> 15 <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> 16 <property name
="supportedMediaTypes"> 17 <list> 18 <value>application/json;charset=UTF-8</value> 19 </list> 20 </property> 21 </bean> 22 <!-- 自動註冊模型 --> 23 <mvc:annotation-driven> 24 <mvc:message-converters> 25 <!-- 引用上面設置的字符編碼 --> 26 <ref bean="stringHttpMessageConverter"/> 27 </mvc:message-converters> 28 </mvc:annotation-driven> 29 <!-- 放過靜態資源 --> 30 <mvc:default-servlet-handler/> 31 <!-- 多視圖解析器 --> 32 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 33 <!-- 指定json 用什麽工具解析 xml 用什麽工具解析 --> 34 <property name="defaultViews"> 35 <list> 36 <bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"> 37 <property name="charset" value="UTF-8"/> 38 </bean> 39 <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 40 <constructor-arg> 41 <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 42 <property name="classesToBeBound"> 43 <list> 44 <value>com.appsys.pojo.Ad_promotion</value> 45 <value>com.appsys.pojo.App_category</value> 46 <value>com.appsys.pojo.App_info</value> 47 <value>com.appsys.pojo.App_version</value> 48 <value>com.appsys.pojo.Backend_user</value> 49 <value>com.appsys.pojo.Data_dictionary</value> 50 <value>com.appsys.pojo.Dev_user</value> 51 </list> 52 </property> 53 </bean> 54 </constructor-arg> 55 </bean> 56 </list> 57 </property> 58 <!-- 指定 jsp解析器,完成視圖的對應 --> 59 <property name="viewResolvers"> 60 <list> 61 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 62 <property name="prefix" value="/" /> 63 <property name="suffix" value=".jsp" /> 64 </bean> 65 </list> 66 </property> 67 </bean> 68 <!-- 掃描註解 --> 69 <context:component-scan base-package="com.appsys.controller,com.appsys.service.impl" /> 70 <!-- 指定文件上傳解析 名字不能亂給 --> 71 <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 72 <property name="defaultEncoding" value="utf-8" /> 73 <property name="maxUploadSize" value="9223372036854775807" /> 74 </bean> 75 </beans>

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans.xsd">
 6     <!-- 創建數據源 -->
 7     <bean id="dataDasource" class="org.apache.commons.dbcp.BasicDataSource">
 8         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 9         <property name="url" value="jdbc:mysql://localhost:3306/appinfodb?characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=true" />
10         <property name="username" value="root" />
11         <property name="password" value="111111" />
12     </bean>
13     <!-- 創建sqlsessionfactory會話工廠 -->
14     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
15         <property name="dataSource" ref="dataDasource" />
16         <property name="configLocation" value="classpath:mybatis-config.xml" />
17         <property name="mapperLocations" value="classpath:mybatis-sqlmappers/*.xml" />
18         <property name="typeAliasesPackage" value="com.appsys.pojo" />
19     </bean>
20     <!-- 映射mapper文件(mybatis-sql語句) -->
21     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
22         <property name="basePackage" value="com.appsys.dao" />
23         <property name="sqlSessionFactory" ref="sqlSessionFactory" />
24     </bean>
25 </beans>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   <!-- 指定Spring Bean的配置文件所在目錄 
12               在web.xml中通過contextConfigLocation配置spring,
13        contextConfigLocation參數定義了要裝入的 Spring 配置文件。 
14   -->
15     <context-param>
16         <param-name>contextConfigLocation</param-name>
17         <param-value>classpath:applicationContext.xml</param-value>
18     </context-param>
19   
20      <!-- spring字符編碼過濾器start-->
21     <filter>
22         <!--① Spring 編碼過濾器 -->
23       <filter-name>encodingFilter</filter-name>
24       <filter-class>
25           org.springframework.web.filter.CharacterEncodingFilter
26       </filter-class>
27       <!--② 編碼方式  -->
28       <init-param> 
29           <param-name>encoding</param-name>
30           <param-value>UTF-8</param-value>
31       </init-param>
32       <!--③ 強制進行編碼轉換 -->
33       <init-param>
34           <param-name>forceEncoding</param-name>
35           <param-value>true</param-value>
36       </init-param>
37      </filter>
38      <!-- ② 過濾器的匹配 URL -->
39        <filter-mapping> 
40            <filter-name>encodingFilter</filter-name>
41            <url-pattern>/*</url-pattern>
42        </filter-mapping>
43     <!-- spring字符編碼過濾器end-->
44     
45     <!-- Spring MVC配置 -->
46     <servlet>
47         <servlet-name>springmvc</servlet-name>
48         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
49         <init-param>
50             <param-name>contextConfigLocation</param-name>
51             <param-value>classpath:springmvc-servlet.xml</param-value>
52         </init-param>
53         <load-on-startup>1</load-on-startup>
54     </servlet>
55     <servlet-mapping>
56         <servlet-name>springmvc</servlet-name>
57         <url-pattern>/</url-pattern>
58     </servlet-mapping>
59     
60      <!-- Spring配置 -->
61     <!-- 當系統啟動的時候,spring需要進行一些資源加載或者配置,都需要使用此監聽去做 -->
62     <listener>
63         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
64     </listener>
65     
66     <!-- log4j配置start -->
67     <context-param>
68         <param-name>log4jConfigLocation</param-name>
69         <param-value>classpath:log4j.properties</param-value>
70     </context-param>
71     <context-param>
72     <!-- 指定系統根目錄路徑 -->
73         <param-name>webAppRootKey</param-name>
74         <param-value>SMBMMVC.root</param-value>
75        </context-param>
76     <!-- Spring 加載 Log4j 的監聽 -->
77     <listener>
78         <listener-class>
79             org.springframework.web.util.Log4jConfigListener
80         </listener-class>
81     </listener>
82     <!-- log4j配置end -->
83 </web-app>

Spring需要的幾個關鍵配置文件