E-commerce website development record (3) Introduction of Spring, and detailed configuration

1.web.xml配置注解
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>Archetype Created Web Application</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 container listener, only monitor the startup and shutdown of other web containers -->
    <listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
 <!--Integrate Spring container with other containers-->
  <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>
<!--Start DispatcherServlet--> directly after loading

    <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!--Intercept all .do requests-->    
<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.applicationContext.xml配置注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--Scan annotations under com.mall-->
<context :component-scan base-package="com.mall" annotation-config="true"/>
<!--<context:annotation-config/>-->
<aop:aspectj-autoproxy/>
<!--will The configuration package is separated, and the configuration of applicationContext-datasource.xml is introduced>
    <import resource="applicationContext-datasource.xml"/>
</beans>
3.
applicationContext-datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描com.mall下的注解>
<context:component-scan base-package="com.mall" annotation-config="true"/>
<!--属性配置,将一些属性常量放在datasource.properties中>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:datasource.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
<!--数据库用到的连接池的配置-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${db.initialSize}"/>
<!-- The maximum value of the connection pool--> <!-- The maximum idle value. After a peak time, the connection pool can slowly Slowly release a part of the connection that has not been used until it is reduced to maxIdle -->
<property name="maxActive" value="${db.maxActive}"/>

<property name="maxIdle" value="${db.maxIdle}"/>
<!-- Minimum idle value. When the number of idle connections is less than the threshold, the connection pool will pre-apply for some connections to avoid flooding It 's too late to apply -->
<property name="minIdle" value="${db.minIdle}"/>
<!-- The maximum waiting time for establishing a connection. If this time is exceeded, an exception will be received. Set to -1 for unlimited -->
<property name="maxWait" value="${db.maxWait}"/>
<!--# Give a simple sql statement to verify -->
<!-- <property name="validationQuery" value="select getdate()" />-->
<property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/>
<!-- Recycle abandoned (usually Forgot to release) database connection to the connection pool -->
<!--<property name="removeAbandoned" value="true"


<!-- #Connection timeout, the default is half an hour. -->
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>

<!--# Failure check thread running time interval, which is less than MySQL default -->
<property name="timeBetweenEvictionRunsMillis" value= "40000"/>
<!--# Check if the connection is valid -->
<property name="testWhileIdle" value="true"/>
<!--# SQL statement to check the validity of the connection -->
<property name= "validationQuery" value="SELECT 1 FROM dual"/>
</bean>
<!--Read the sql implementation of mybatis>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name ="


<!-- 分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>

</bean>
<!--对dao层进行一个扫描-->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mmall.dao"/> <!-- Declarative transaction management using @Transactional requires declaring the following line -->
</bean>


<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="rollbackOnCommitFailure" value="true"/>
</bean>


</beans>
4.dispacher-servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描带注解的包-->
<context:component-scan base-package="com.mall" annotation-config="true"/>

<mvc:annotation-driven>
<mvc:message-converters>
<!--编码配置-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!--SpringMVC反序列化配置-->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>



<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/> <!-- 10m -->
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>


</beans>


 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346154&siteId=291194637