电商网站开发记录(三) Spring的引入,以及配置详解
2018-05-06 17:19
633 查看
1.web.xml配置注解 <?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5"> <display-name>ArchetypeCreatedWebApplication</display-name> <!--将所有请求全部转编码为UTF-8--> <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> <!--web容器监听器,只监听其他web容器的启动和关停-->
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
<!--将Spring容器与其他容器进行整合-->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param>
<!--一加载完后直接启动DispatcherServlet-->
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<!--一拦截所有.do请求--> <servlet-mapping>
<servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 2.applicationContext.xml配置注解
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> <!--扫描com.mall下的注解--> <context:component-scanbase-package="com.mall"annotation-config="true"/> <!--<context:annotation-config/>--> <aop:aspectj-autoproxy/> <!--将配置包分离,引入applicationContext-datasource.xml这个配置>
<importresource="applicationContext-datasource.xml"/> </beans> 3.applicationContext-datasource.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> <!--扫描com.mall下的注解> <context:component-scanbase-package="com.mall"annotation-config="true"/> <!--属性配置,将一些属性常量放在datasource.properties中> <beanid="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <propertyname="order"value="2"/> <propertyname="ignoreUnresolvablePlaceholders"value="true"/> <propertyname="locations"> <list> <value>classpath:datasource.properties</value> </list> </property> <propertyname="fileEncoding"value="utf-8"/> </bean> <!--数据库用到的连接池的配置--> <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <propertyname="driverClassName"value="${db.driverClassName}"/> <propertyname="url"value="${db.url}"/> <propertyname="username"value="${db.username}"/> <propertyname="password"value="${db.password}"/> <!--连接池启动时的初始值--> <propertyname="initialSize"value="${db.initialSize}"/> <!--连接池的最大值--> <propertyname="maxActive"value="${db.maxActive}"/> <!--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止--> <propertyname="maxIdle"value="${db.maxIdle}"/> <!--最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请--> <propertyname="minIdle"value="${db.minIdle}"/> <!--最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制--> <propertyname="maxWait"value="${db.maxWait}"/> <!--#给出一条简单的sql语句进行验证--> <!--<propertyname="validationQuery"value="selectgetdate()"/>--> <propertyname="defaultAutoCommit"value="${db.defaultAutoCommit}"/> <!--回收被遗弃的(一般是忘了释放的)数据库连接到连接池中--> <!--<propertyname="removeAbandoned"value="true"/>--> <!--数据库连接过多长时间不用将被视为被遗弃而收回连接池中--> <!--<propertyname="removeAbandonedTimeout"value="120"/>--> <!--#连接的超时时间,默认为半小时。--> <propertyname="minEvictableIdleTimeMillis"value="${db.minEvictableIdleTimeMillis}"/> <!--#失效检查线程运行时间间隔,要小于MySQL默认--> <propertyname="timeBetweenEvictionRunsMillis"value="40000"/> <!--#检查连接是否有效--> <propertyname="testWhileIdle"value="true"/> <!--#检查连接有效性的SQL语句--> <propertyname="validationQuery"value="SELECT1FROMdual"/> </bean> <!--读取mybatis的sql实现> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <propertyname="mapperLocations"value="classpath*:mappers/*Mapper.xml"></property> <!--分页插件--> <propertyname="plugins"> <array> <beanclass="com.github.pagehelper.PageHelper"> <propertyname="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean> <!--对dao层进行一个扫描--> <beanname="mapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <propertyname="basePackage"value="com.mmall.dao"/> </bean> <!--使用@Transactional进行声明式事务管理需要声明下面这行--> <tx:annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/> <!--事务管理--> <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <propertyname="dataSource"ref="dataSource"/> <propertyname="rollbackOnCommitFailure"value="true"/> </bean> </beans> 4.dispacher-servlet.xml配置
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--扫描带注解的包--> <context:component-scanbase-package="com.mall"annotation-config="true"/> <mvc:annotation-driven> <mvc:message-converters> <!--编码配置--> <beanclass="org.springframework.http.converter.StringHttpMessageConverter"> <propertyname="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!--SpringMVC反序列化配置--> <beanclass="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <propertyname="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--文件上传--> <beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <propertyname="maxUploadSize"value="10485760"/><!--10m--> <propertyname="maxInMemorySize"value="4096"/> <propertyname="defaultEncoding"value="UTF-8"></property> </bean> </beans>
相关文章推荐
- 电商网站开发记录(二) mybatis三剑客的引入
- JAVAWEB开发之mybatis详解(二)——高级映射、查询缓存、mybatis与Spring整合以及懒加载的配置和逆向工程
- JAVAWEB开发之mybatis详解(二)——高级映射、查询缓存、mybatis与Spring整合以及懒加载的配置和逆向工程
- Spring开发环境配置以及入门案例
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
- IOS开发 支持https请求以及ssl证书配置详解
- spring注解开发中常用注解以及简单配置
- Zend framework框架开发配置以及目录结构详解
- JAVAWEB开发之Struts2详解(二)——Action接受请求参数、类型转换器、使用Struts2的输入校验、以及遵守约定规则实现Struts2的零配置
- JAVAWEB开发之Hibernate详解(一)——Hibernate的框架概述、开发流程、CURD操作和核心配置与API以及Hibernate日志的使用
- Spring学习----------AOP以及Spring配置文件详解
- JavaEE开发之Spring中的多线程编程以及任务定时器详解
- eclipse rcp导出乱码以及引入spring配置文件
- Java开发框架--spring-servlet.xml 配置详解
- JAVAWEB开发之Tomcat内置连接池的配置和使用、DbUtils的使用详解和案例、以及元数据详解
- Spring学习笔记(十七):spring配置文件详解以及beans:beans标签
- Spring中的注入注解Resource/Autowired以及配置文件中default-autowire参数详解
- 关于Spring 配置文件头的一些记录和解释(以备开发时方便查阅)
- Spring的配置以及详解
- JAVAWEB开发之Struts2详解(一)——Struts2框架介绍与快速入门、流程分析与工具配置以及Struts2的配置以及Action和Result的详细使用