一、web.xml配置详解
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6
7 <display-name>Archetype Created Web Application</display-name>
8 <!-- 起始欢迎界面 -->
9 <welcome-file-list>
10 <welcome-file>index.jsp</welcome-file>
11 </welcome-file-list>
12
13 <!-- Spring监听器:启动Web容器时,自动装配ApplicationContext的配置信息 -->
14 <listener>
15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16 </listener>
17
18 <!-- 读取spring配置文件
19 Spring应用上下文,层次化的applicationContext service层,dao层, 提供公共组件 被整个应用共享
20 -->
21 <context-param>
22 <param-name>contextConfigLocation</param-name>
23 <param-value>classpath:applicationContent.xml</param-value>
24 </context-param>
25
26 <!-- 设计路径变量值
27 作用:用System.getProperty("webapp.root")来动态获项目的运行路径。
28 返回 /usr/local/tomcat6/webapps/
29
30 如果已经在log里配置监听org.springframework.web.util.Log4jConfigListener
31 则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能
32 -->
33 <context-param>
34 <param-name>webAppRootKey</param-name>
35 <param-value>webapp.root</param-value>
36 </context-param>
37
38
39 <!-- Spring字符集过滤器 -->
40 <filter>
41 <filter-name>SpringEncodingFilter</filter-name>
42 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
43 <init-param>
44 <param-name>encoding</param-name>
45 <param-value>UTF-8</param-value>
46 </init-param>
47 <init-param>
48 <param-name>forceEncoding</param-name>
49 <param-value>true</param-value>
50 </init-param>
51 </filter>
52 <filter-mapping>
53 <filter-name>SpringEncodingFilter</filter-name>
54 <url-pattern>/*</url-pattern>
55 </filter-mapping>
56
57 <!-- 日志记录 -->
58 <context-param>
59 <!-- 日志配置文件路径 -->
60 <param-name>log4jConfigLocation</param-name>
61 <param-value>classpath:log4j.properties</param-value>
62 </context-param>
63 <context-param>
64 <!-- 日志页面的刷新间隔 -->
65 <param-name>log4jRefreshInterval</param-name>
66 <param-value>6000</param-value>
67 </context-param>
68 <listener>
69 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
70 </listener>
71
72 <!-- springMVC核心配置 controller,viewResolver,HandlerMapping -->
73 <servlet>
74 <servlet-name>dispatcherServlet</servlet-name>
75 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
76 <init-param>
77 <param-name>contextConfigLocation</param-name>
78 <!--spingMVC的配置路径 -->
79 <param-value>classpath:springmvc/spring-mvc.xml</param-value>
80 </init-param>
81 <load-on-startup>1</load-on-startup>
82 </servlet>
83 <servlet-mapping>
84 <servlet-name>dispatcherServlet</servlet-name>
85 <url-pattern>/</url-pattern>
86 </servlet-mapping>
87
88 <!-- 错误跳转页面 -->
89 <error-page>
90 <!-- 路径不正确 -->
91 <error-code>404</error-code>
92 <location>/WEB-INF/errorpage/404.jsp</location>
93 </error-page>
94 <error-page>
95 <!-- 没有访问权限,访问被禁止 -->
96 <error-code>405</error-code>
97 <location>/WEB-INF/errorpage/405.jsp</location>
98 </error-page>
99 <error-page>
100 <!-- 内部错误 -->
101 <error-code>500</error-code>
102 <location>/WEB-INF/errorpage/500.jsp</location>
103 </error-page>
104 </web-app>
二、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" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
12
13 <context:annotation-config />
14
15 <!-- 引入jdbc配置文件
16 <bean id="propertyConfigurer"
17 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
18 <property name="locations">
19 <list>
20 <value>classpath:properties/*.properties</value>
21 要是有多个配置文件,只需在这里继续添加即可
22 </list>
23 </property>
24 </bean>
25 配置数据源
26 <bean id="dataSource"
27 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
28 不使用properties来配置
29 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
30 <property name="url" value="jdbc:mysql://localhost:3306/learning" /> <property
31 name="username" value="root" /> <property name="password" value="christmas258@"
32 />
33 使用properties来配置
34 <property name="driverClassName">
35 <value>${jdbc_driverClassName}</value>
36 </property>
37 <property name="url">
38 <value>${jdbc_url}</value>
39 </property>
40 <property name="username">
41 <value>${jdbc_username}</value>
42 </property>
43 <property name="password">
44 <value>${jdbc_password}</value>
45 </property>
46 </bean> -->
47
48 <!-- 定义连接数据库 -->
49 <context:property-placeholder file-encoding="utf-8" location="classpath:properties/jdbc.properties" />
50 <!-- 定义使用阿里巴巴Druid连接池的数据源 可以使用C3P0-->
51 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
52 <property name="url" value="${url}" />
53 <property name="username" value="${username}" />
54 <property name="password" value="${password}" />
55 <!-- 配置初始化大小、最小、最大 -->
56 <!-- 初始化连接大小 -->
57 <property name="initialSize" value="${initialSize}"></property>
58 <!-- 连接池最大数量 -->
59 <property name="maxActive" value="${maxActive}"></property>
60 <!-- 连接池最大空闲 -->
61 <property name="maxIdle" value="${maxIdle}"></property>
62 <!-- 连接池最小空闲 -->
63 <property name="minIdle" value="${minIdle}"></property>
64 <!-- 获取连接最大等待时间 -->
65 <property name="maxWait" value="${maxWait}"></property>
66 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
67 <property name="timeBetweenEvictionRunsMillis" value="${betweenTime}" />
68 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
69 <property name="minEvictableIdleTimeMillis" value="${minTime}" />
70 <property name="validationQuery" value="SELECT 'x'" />
71 <property name="testWhileIdle" value="true" />
72 <property name="testOnBorrow" value="false" />
73 <property name="testOnReturn" value="false" />
74 <!-- 打开PSCache,并且指定每个连接上PSCache的大小 ,注意:如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。 -->
75 <property name="poolPreparedStatements" value="false" />
76 <property name="maxPoolPreparedStatementPerConnectionSize"
77 value="20" />
78 </bean>
79
80
81 <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
82 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
83 <property name="basePackage" value="com.yuan.space.dao" />
84 </bean>
85
86 <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置 -->
87 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
88 <property name="dataSource" ref="dataSource" />
89 <property name="mapperLocations" value="classpath*:com/yuan/space/mapper/**/*.xml" />
90 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
91 <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
92 /> -->
93 </bean>
94
95 <!-- 自动扫描注解的bean -->
96 <context:component-scan base-package="com.yuan.space" />
97
98 </beans>
applicationContent.xml
1 #jdbc:mysql://localhost:3306/eportal?useUnicode=true&characterEncoding=utf-8 数据库名后加?...解决数据库安装非utf-8格式问题
2 #jdbc_driverClassName=com.mysql.jdbc.Driver
3 #jdbc_url=jdbc\:mysql\://localhost\:3306/PublicSpace
4 #jdbc_username=root
5 #jdbc_password=root
6
7 driver=com.mysql.jdbc.Driver
8 url=jdbc:mysql://localhost:3306/PublicSpace
9 username=root
10 password=root
11 #定义初始连接数
12 initialSize=0
13 #定义最大连接数
14 maxActive=20
15 #定义最大空闲
16 maxIdle=20
17 #定义最小空闲
18 minIdle=1
19 #定义最长等待时间
20 maxWait=60000
21 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
22 betweenTime=60000
23 #配置一个连接在池中最小生存的时间,单位是毫秒
24 minTime=300000
jdbc.properties
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
4 <configuration>
5 <!-- 需引入 cglib.jar
6 当你要使用one to one,many to one 就会碰到N+1的问题。
7 对象之间关联很多有A关联B,B关联C,C关联A这样的关系,如果不是采用延迟加载,很容易一下在出现成千上万对象,造成N+1的问题。
8
9 lazyLoadingEnabled:true使用延迟加载,false禁用延迟加载。默认为true
10 aggressiveLazyLoading:默认为true
11 true启用时,当延迟加载开启时访问对象中一个懒对象属性时,将完全加载这个对象的所有懒对象属性。
12 false,当延迟加载时,按需加载对象属性(即访问对象中一个懒对象属性,不会加载对象中其他的懒对象属性)。
13 -->
14 <settings>
15 <setting name="lazyLoadingEnabled" value="true" />
16 <setting name="aggressiveLazyLoading" value="true"/>
17 </settings>
18 </configuration>
spring-mvc.xml 主要处理controller事务。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="
7 http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context-3.2.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
13
14 <!-- 扫描controller(controller层注入) -->
15 <context:component-scan base-package="com.yuan.space.controller" />
16
17 <!-- 对模型视图添加前后缀 -->
18 <bean id="viewResolver"
19 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
20 p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
21 </beans>
springmvc-mvc.xml