一。web.xml处理请求,启动spring
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- 启动 Spring 的IOC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context*.xml</param-value>
</context-param>
<!-- 配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml
作为spring容器的配置文件,该文件里可以初始化一些bean -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//声明应用范围内的初始化参数
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
//对所有请求 都由servlet 分发 启动 Spring MVC 的IOC容器
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
标记容器是否在启动的时候就加载这个servlet。当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
正数的值越小,启动该servlet的优先级越高。当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/stream/login/404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/wechat/500.html</location>
</error-page>
<!-- Shiro Filter -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
//request.setCharacterEncoding("UTF-8");
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
//response.setCharacterEncoding("UTF-8");
<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-app>
二。spring.xml配置 主要配置web层以外的配置
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.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
">
<!-- 配置数据源 -->
// 1.1 spring 自带 不需要额外包 不提供池化链接,每次链接都创建新链接 最 low的
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 不使用properties来配置 -->
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost/shirotest" />
<property name="username" value="postgres" />
<property name="password" value="178528" />
</bean>
// 1.2 dbcp 连接池 某些情况会产生空链接,hibernate3.0放弃对其支持
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driverr" />
<property name="url" value="jdbc:postgresql://localhost/shirotest" />
<property name="username" value="postgres" />
<property name="password" value="178528" />
//常用属性
<property name="initialSize" value="15"></property><!-- 初始连接数 -->
<property name="maxActive" value="50"></property><!-- 最大连接数 -->
<property name="minIdle" value="10"></property><!-- 最小空闲数 -->
<property name="maxIdle" value="20"></property><!-- 最大空闲数 -->
<property name="removeAbandonedTimeout" value="10"></property><!--
超时时间10秒 -->
<property name="defaultAutoCommit" value="false"></property>
<property name="removeAbandoned" value="true"></property><!-- 是否回收超时连接 -->
<property name="maxWait" value="1000"></property><!-- 单位毫秒,超时等待时间 -->
<!-- 连接被抛弃时是否打印输出到日志中 -->
<property name="logAbandoned" value="true" />
<!-- 每次空闲连接被回收时,需要检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="15" />
<!-- 验证查询,获取当前连接数据库的时间 -->
<property name="validationQuery" value="SELECT NOW() FROM DUAL"></property>
</bean>
//1.3 c3po连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driverr" />
<property name="url" value="jdbc:postgresql://localhost/shirotest" />
<property name="username" value="postgres" />
<property name="password" value="178528" />
//常用属性
<!-- 设置初始化连接数 -->
<property name="initialPoolSize" value="15"></property>
<!-- 设置池中最小连接数量 -->
<property name="minPoolSize" value="15"></property>
<!-- 设置池中最大连接数量 -->
<property name="maxPoolSize" value="50"></property>
<!-- 当连接池中连接不够时,需要自动获取的连接数量 -->
<property name="acquireIncrement" value="5"></property>
<!-- 设置池中允许出现的最大预编译对象的数量 -->
<property name="maxStatements" value="200"></property>
<!-- 设置最长空闲时间,单位:秒 -->
<property name="maxIdleTime" value="120"></property>
<!-- 设置间隔多长时间,扫描一次空闲连接,单位:秒 -->
<property name="idleConnectionTestPeriod" value="60"></property>
<!-- 设置当数据库连接无法正常获取的情况下,间隔一定的去重复获取的次数,单位:次数 -->
<property name="acquireRetryAttempts" value="20"></property>
<!-- 设置间隔多长时间, 回收一次超时连接 -->
<property name="checkoutTimeout" value="60"></property>
<!-- 取消自动提交 -->
<property name="autoCommitOnClose" value="false"></property>
<!-- 连接池初始化完毕连接之后,将发送一条SQL语句,目的主要是用于验证连接是可用的,是正常的 -->
<property name="preferredTestQuery" value="select now() from dual"></property>
</bean>
2.1 mybatis 配置
<!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="A2C.Db.Dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:A2C/Db/Mapping/*.xml"/>
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
2.2 hibernate 配置
<!-- 配置SessionFactory,来自于:spring-orm.jar -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- hibernateProperties 该属性主要用于配置hibernate相关信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.POSTGRESQLDialect<!-- 来自于:hibernate-core.jar -->
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 扫描使用的注解的ORM实体类 -->
<property name="packagesToScan">
<list>
<value>A2C.Msg.Entity</value>
<!-- <value></value> 有多个beans包就多写 -->
</list>
</property>
</bean>
<!-- 缓存设置 -->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-setting.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"></property>
</bean>
<!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" read-only="true" />
<tx:method name="save*" read-only="false" isolation="DEFAULT"
<tx:method name="update*" read-only="false" isolation="DEFAULT"
propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="modify*" read-only="false" isolation="DEFAULT"
propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="change*" read-only="false" isolation="DEFAULT"
propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="del*" read-only="false" isolation="DEFAULT"
propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="remove*" read-only="false" isolation="DEFAULT"
propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
// 事物所需切面
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* org.framestudy.ssm.*mag.service.impl.*ServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>
<bean id="initconfig" class="A2C.Component.InitConfiguration">
<property name="ip" value="192.168.1.4"/>
<!-- 172.16.255.80 -->
<property name="port" value="10000"/>
</bean>
<context:component-scan base-package="A2C.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
三。MVC.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<mvc:default-servlet-handler />
<!-- @Controller注解来指定Controller对象,用@RequestMapping来指定某方法映射某路径 -->
<mvc:annotation-driven>
<!-- 设置不使用默认的消息转换器 -->
<mvc:message-converters register-defaults="false">
<!-- 配置Spring的转换器 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="A2C.Web.MsgConverter.A2CStreamConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 开启springmvc的注解支持 -->
<mvc:annotation-driven enable-matrix-variables="true" conversion-service="tc"/>
<!-- 将自己写的全局类型转换规则,添加到spring容器的转换规则中 -->
<bean id="tc" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="a2c.converter.GolbalUtilDateConverter"></bean>
</set>
</property>
</bean>
<context:component-scan base-package="A2C.*"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
//等同于 个人认为上面这种好些 用那取哪
<!-- 表示容器在扫描的时候,只允许使用@Controller以及@Component的JAVA类,纳入容器管理 -->
<context:component-scan base-package="org.framestudy.sh">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<!-- 开启aop注解方式 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,
并在必要时进行安全逻辑验证 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<!-- <property name="proxyTargetClass" value="true" /> -->
</bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
<!-- 配置文件上传解析器,ID必须固定为:multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- maxUploadSize 上传文件的文件容量大小,-1代表无限大,但是如果上传比如说:10M=1024*1024*10 -->
<property name="maxUploadSize" value="-1"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 每次读取的时候,每一次最多可以读取多少个字节到内存中,byte[] bytes = new byte[1024] -->
<property name="maxInMemorySize" value="1024"></property>
</bean>
<!-- 配置静态资源包,目的是:告诉前端控制器,这个静态资源包下的资源请求,不需要
去HandleMapping中问询,直接去相应的包下面获取就可以了 -->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
<!--jsp视图解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.InternalResourceView" />
<property name="order" value="0" />
</bean>
</beans>