Spring需要的几个关键配置文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring需要的几个关键配置文件相关的知识,希望对你有一定的参考价值。

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需要的几个关键配置文件的主要内容,如果未能解决你的问题,请参考以下文章

Spring Container概览

Spring通过Xml方式注册Bean的几处关键实现点

spring 配置的几个info信息

Servlet的几个关键知识点

Servlet的几个关键知识点

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式