您的位置:首页 > 编程语言 > Java开发

电商网站开发记录(三) 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.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://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.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://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>




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐