版本概况
Spring系列 4.0.2
Spring-Security-Cas-Client-3.0.8
Mybatis 3.2.2
hibernate-validator 5.2.2
jedis 2.8.0
velocity 1.6.4
数据库直接丢到服务器的lib中了.所以不列出来.
做一个项目前,一定要确认项目需要哪些功能,确定功能后才能才能在脑中确定要配置什么,其次你也需要知道你
所用的工具能配置什么.
这里需要的配置包括:
Spring ApplicationContext.xml的配置
包括单不限于:
定时调度 Timer的配置
数据库的配置 包括 数据库,事务,事务切面.
缓存的配置 Redis
Mybatis的配置
安全的配置
SpringMVC dispatcherServlet.xml的配置
视图解析器的配置
文件上传的配置
企业开发,大部分都是使用xml做集成,一般来说都是可以从web.xml开始来编写
第一步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>xxx-scaffold</display-name>
<!-- spring配置文件加载 配置jar包中的资源文件不要使用通配符 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:applicationContext-redisCache.xml
classpath:applicationContext-service.xml
/WEB-INF/applicationContext-cas-filter.xml
classpath:applicationContext-security-cas.xml
classpath:applicationContext-timer.xml
</param-value>
</context-param>
<!-- 载入Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 日志文件 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring字符编码过滤器,只过滤post -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring自带的垃圾回收 Listener -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 默认首页配置 -->
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- spring MVC配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<!-- 启动顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- spring security配置 -->
<filter>
<filter-name>securityFilter</filter-name>
<!-- 标准过滤器的代理,适用于复杂的过滤器实现. -->
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<!-- 远程调用 soap.. -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- 防止跨站脚本攻击 -->
<filter>
<filter-name>XSS</filter-name>
<display-name>XSS</display-name>
<description></description>
<filter-class>CrossScriptingFilter4Xss</filter-class>
</filter>
<filter-mapping>
<filter-name>XSS</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<!-- 数据源配置 jndi -->
<resource-ref>
<description>ATASOURCE</description>
<res-ref-name>jdbc/recharge</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
配置SpringMVC
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">
<!-- 扫描控制器 -->
<context:component-scan base-package="com.*.controller"></context:component-scan>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="fastJsonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
<!-- 出现异常信息处理 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="warnLogCategory" value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"></property>
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">common/error</prop>
<prop key="java.lang.Exception">common/error</prop>
<prop key="java.lang.Throwable">common/error</prop>
</props>
</property>
<property name="exceptionAttribute" value="error"></property>
</bean>
<!-- vm视图解析器 优先级 1 -->
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="exposeSessionAttributes" value="true" />
<property name="requestContextAttribute" value="rc" />
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="order" value="1" />
<property name="viewNames">
<list>
<value>*</value>
</list>
</property>
<property name="dateToolAttribute" value="dateTool" />
<property name="numberToolAttribute" value="numberTool" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="configLocation"
value="classpath:velocity.properties" />
<property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</bean>
<!-- jsp视图解析器 优先级2 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages"/>
<property name="suffix" value=".htm"/>
<property name="order" value="2" />
</bean>
<!-- 上传文件大小控制 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="524288000"/>
<property name="maxInMemorySize" value="524288000"/>
</bean>
</beans>
配置Spring ApplicationContext*.xml
配置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:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:annotation-config></context:annotation-config>
<!-- 扫描注解类,不允许重复配置此标签,否则哎was中会出现注解失败的问题 -->
<context:component-scan base-package="com.*"> </context:component-scan>
<!-- mybatis数据源 -->
<jee:jndi-lookup id="dataSource" resource-ref="true" jndi-name="jdbc/recharge" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.a.*.model,com.b.*.model" ></property>
<property name="mapperLocations" value="classpath:sqlmapper/*Mapper.xml" />
</bean>
<!-- 配置自动扫描sqlmapper并管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.a.*.dao,com.b.*.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 事务控制管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明事务规则 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<!-- 因为啥异常回滚 -->
<tx:method name="*" rollback-for="com.*.Exception" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置事务生效包类方法 -->
<aop:config>
<aop:pointcut id="serviceConfig" expression="execution(* com.*.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceConfig" />
</aop:config>
<!-- 配置Properties文件读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:cache.properties</value>
<value>classpath:configure.properties</value>
</list>
</property>
</bean>
</beans>
配置:applicationContext-timer.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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!--定时任务的配置 -->
<!-- 配置定时任务的线程池 -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="5" />
<!-- 启用注解驱动的定时任务 -->
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
timer这里使用的是注解的形式
配置: applicationContext-redisCache.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<-- redis缓存配置 -->
<bean id="jedisPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxIdle" value="${maxIdle}"></property>
<property name="maxTotal" value="${maxTotal}"></property>
<property name="testOnBorrow" value="${testOnBorrow}"></property>
<property name="maxWaitMillis" value="${maxWaitMillis}"></property>
</bean>
<bean id="jedisPoolUtil" class="JedisPoolUtil">
<constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg index="1" value="${ip}"></constructor-arg>
<constructor-arg index="2" value="${timeout}"></constructor-arg>
<property name="seconds" value="${seconds}"></property>
</bean>
</beans>
配置: applicationContext-security-filter.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"
default-lazy-init="true">
<http auto-config="false" entry-point-ref="casProcessingFilterEntryPoint">
<intercept-url pattern="/recharge/*.html*" access="permitAll" />
<intercept-url pattern="/order/*.html*" access="permitAll" />
<intercept-url pattern="/**/*.html*" access="fullyAuthenticated" />
<logout invalidate-session="true" />
<custom-filter after="CAS_FILTER" ref="casProcessingFilter"/>
</http>
</beans:beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- aggressiveLazyLoading:true启用时,
当延迟加载开启时访问对象中一个懒对象属性时,
将完全加载这个对象的所有懒对象属性。false,
当延迟加载时,按需加载对象属性(即访问对象中一个懒对象属性,不会加载对象中其他的懒对象属性)。默认为true
lazyLoadingEnabled true启动懒加载-->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>
后面还有一些log的配置.velocity模板的配置,很简单.
sql服务器的配置,nosql服务器的配置具有隐私性,就不列出来了.