Springmvc
<!-- 扫描 com.rsy.ctrl 及子包,自动实例化带@controller注释的实例 -->
<context:component-scan base-package="com.rsy.ctrl">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!— 视图解析器 页面前后缀 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="10240000" />
</bean>
<!-- 支持MVC注解,例如 @ResponseBody -->
<mvc:annotation-driven />
<!-- 配置/resources/目录下的文件不监听,通常这个目录存放css、图片、javascript等 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- springMVC拦截器配置 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截的请求路径 -->
<mvc:mapping path="/**"/>
<!-- 对应的拦截器 -->
<bean class="com.ruisiyuan.interceptor.AuthInterceptor">
<!-- 给拦截器传参 -->
<property name="name" value="拦截器名称" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
Spring(applicationContext.xml)
<!-- 支持注解 -->
<context:annotation-config />
<!--
Spring 管理的 Bean 不包含 @Controller
-->
<context:component-scan base-package="com.rsy">
<!-- 必须配置 exclude-filter 否则 @Controller 注解的类会被 spring 和 springMVC 分别实例化一次 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
常量属性文件注入
ignore-unresolvable 代表可以配置多个文件
-->
<context:property-placeholder location="classpath:constants.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:system.properties" ignore-unresolvable="true"/>
<!-- 数据源 -->
<import resource="applicationContext-db.xml"/>
<!-- mybatis 配置 -->
<import resource="applicationContext-myBaties.xml"/>
<!-- myBaties事物管理 -->
<import resource="applicationContext-txManagerMyBaties.xml"/>
下一页接上,,,整合mybatis
上一页中的applicationContext-db.XML
(可直接加入到上一页的配置里去)
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${datasource.driverClassName}" />
<property name="jdbcUrl" value="${datasource.url}" />
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxPoolSize}" ></property>
<property name="initialPoolSize" value="${initialPoolSize}" />
<property name="acquireIncrement" value="${acquireIncrement}" ></property>
<property name="maxIdleTime" value="${maxIdleTime}" />
<property name="maxStatements" value="${maxStatements}" />
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}" />
<property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}" />
<property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}" />
<property name="autoCommitOnClose" value="${autoCommitOnClose}"></property>
</bean>
下一页接上
ApplicationContext.xml
里的
applicationContext-mybatis.xml
<!-- 管理myBatis的sessionFactory, spring 集成 mybatis,需要导包:mybatis-spring-1.1.1.jar -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
映射实体类的路径
配置该属性之后,在mapper.xml 文件当中可以直接使用类名,不用再写 package.className
-->
<property name="typeAliasesPackage" value="com.rsy.pojo" />
<!--
mybatis的映射文件路径(可选)
如果没有配置该项,则必须在扫描范围包下写.xml文件 文件名称还必须与 dao 接口名称一致
-->
<property name="mapperLocations" value="classpath:com/rsy/dao/**/mapper/*.xml" />
</bean>
<!-- 映射扫描范围 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rsy.dao**" />
</bean>
下一页接上
ApplicationContext.xml
里的
applicationContext-txManagerMybatis.xml
开启事务
<bean id="txManagerMyBaties" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务增强处理 -->
<tx:advice id="txAdviceMyBaties" transaction-manager="txManagerMyBaties">
<!-- 配置事务属性 -->
<tx:attributes>
<tx:method name="add*" read-only="false"/>
<tx:method name="edit*" read-only="false"/>
<tx:method name="del*" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
开启aop
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="allServiceMethodMyBaties" expression="execution(public * com.rsy.service..*.*(..))"/>
<!-- 将事务增强处理与切入点进行关联 -->
<aop:advisor advice-ref="txAdviceMyBaties" pointcut-ref="allServiceMethodMyBaties"/>
</aop:config>
Execution的用法在请参考笔记文档
Web.xml
<!--指定spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!--指定日志配置文件-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 编码过滤器 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器,比如quartz -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring mvcservlet-->
<servlet>
<servlet-name>SpringMVC</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>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<!--为dispatchservlet建立映射-->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处也可以配置成 *.do 形式 -->
<!-- 拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 跨域支持 -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD,PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>3600</param-value><!--单位秒 -->
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Content-Range</param-value><!--允许客户端js访问的header -->
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- session配置 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>