SpringMVC 配置

  1. web.xml配置  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.                              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.     version="2.5">  
  7.   
  8.     <!-- 区分项目名称,防止默认重名 -->  
  9.     <context-param>  
  10.         <param-name>webAppRootKey</param-name>  
  11.         <param-value>com.km.root</param-value>  
  12.     </context-param>  
  13.   
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>classpath:spring/applicationContext-*.xml</param-value>  
  17.     </context-param>  
  18.   
  19.     <!-- Spring的log4j监听器 -->  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  22.     </listener>  
  23.     <listener>  
  24.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  25.     </listener>  
  26.   
  27.     <!-- 字符集 过滤器 -->  
  28.     <filter>  
  29.         <filter-name>CharacterEncodingFilter</filter-name>  
  30.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  31.         <init-param>  
  32.             <param-name>encoding</param-name>  
  33.             <param-value>UTF-8</param-value>  
  34.         </init-param>  
  35.         <init-param>  
  36.             <param-name>forceEncoding</param-name>  
  37.             <param-value>true</param-value>  
  38.         </init-param>  
  39.     </filter>  
  40.     <filter-mapping>  
  41.         <filter-name>CharacterEncodingFilter</filter-name>  
  42.         <url-pattern>/*</url-pattern>  
  43.     </filter-mapping>  
  44.   
  45.     <!-- Spring view分发器 -->  
  46.     <servlet>  
  47.         <servlet-name>dispatcher</servlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  49.         <init-param>  
  50.             <param-name>contextConfigLocation</param-name>  
  51.             <param-value>classpath:spring/dispatcher-servlet.xml</param-value>  
  52.         </init-param>  
  53.         <load-on-startup>1</load-on-startup>  
  54.     </servlet>  
  55.     <servlet-mapping>  
  56.         <servlet-name>dispatcher</servlet-name>  
  57.         <url-pattern>*.do</url-pattern>  
  58.     </servlet-mapping>  
  59.   
  60. </web-app>  


SpringMVC配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="   
  7.            http://www.springframework.org/schema/beans   
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.            http://www.springframework.org/schema/mvc   
  12.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
  13.     default-autowire="byName">  
  14.     <mvc:annotation-driven/>  
  15.   
  16.     <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->  
  17.     <mvc:resources mapping="/img/**" location="/img/" />  
  18.     <mvc:resources mapping="/js/**" location="/js/" />  
  19.     <mvc:resources mapping="/css/**" location="/css/" />  
  20.     <mvc:resources mapping="/plugin/**" location="/plugin/" />  
  21.     <mvc:resources mapping="/upload/**" location="/upload/" />  
  22.   
  23.     <!-- 扫描注解 -->  
  24.     <context:component-scan base-package="com">  
  25.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
  26.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
  27.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />  
  28.     </context:component-scan>  
  29.       
  30.     <!-- InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 -->  
  31.     <bean  
  32.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  33.         <property name="prefix" value="/WEB-INF/view/"></property>  
  34.         <property name="suffix" value=".jsp"></property>  
  35.     </bean>  
  36.       
  37.     <!-- 上传组件-->  
  38.     <bean id="multipartResolver"   
  39.           class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />    
  40.        
  41.      <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->   
  42.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  43.   
  44.   
  45. </beans>   

Spring + Mybatis的配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="   
  6.           http://www.springframework.org/schema/beans   
  7.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.           http://www.springframework.org/schema/tx   
  9.           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.           http://www.springframework.org/schema/context   
  11.           http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  12.           http://www.springframework.org/schema/aop   
  13.           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  14.     default-autowire="byType">  
  15.     <!-- 扫描注解 -->  
  16.     <!-- 约定优于配置,约定优于配置 -->  
  17.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  18.         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  19.         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>  
  20.         <property name="user" value="root"></property>  
  21.         <property name="password" value=""></property>  
  22.     </bean>  
  23.   
  24.     <!-- MyBatis 配置 -->  
  25.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  26.         <property name="dataSource" ref="dataSource" />  
  27.         <property name="configLocation" value="classpath:mybatis/Configuration.xml" />  
  28.     </bean>  
  29.   
  30.     <context:component-scan base-package="com">  
  31.         <context:exclude-filter type="annotation"  
  32.             expression="org.springframework.stereotype.Controller" />  
  33.     </context:component-scan>  
  34.   
  35.     <!-- 配置声明式事务管理的 -->  
  36.     <bean id="txManager"  
  37.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  38.         <property name="dataSource" ref="dataSource" />  
  39.     </bean>  
  40.   
  41.     <aop:config>  
  42.         <aop:advisor pointcut="execution(* com.xxxx.service.*.*(..))"  
  43.             advice-ref="txAdvice" />  
  44.     </aop:config>  
  45.   
  46.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  47.         <tx:attributes>  
  48.             <tx:method name="get*" read-only="true" />  
  49.             <tx:method name="query*" read-only="true" />  
  50.             <tx:method name="find*" read-only="true" />  
  51.             <tx:method name="load*" read-only="true" />  
  52.             <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />  
  53.         </tx:attributes>  
  54.     </tx:advice>  
  55.   
  56.     <!-- 配置Mapper,相当于dao   
  57.     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  58.         <property name="mapperInterface" value="com.xxx.dao.UserMapper" />  
  59.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  60.     </bean>-->  
  61.   
  62.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  63.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  64.         <property name="basePackage" value="com.xxx.dao"></property>  
  65.     </bean>  
  66. </beans>  

Spring + Hibernate 的配置
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="   
  6.           http://www.springframework.org/schema/beans   
  7.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.           http://www.springframework.org/schema/tx   
  9.           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.           http://www.springframework.org/schema/context   
  11.           http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  12.           http://www.springframework.org/schema/aop   
  13.           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  14.     default-autowire="byName">  
  15.         <!-- 扫描注解 -->  
  16.      <context:component-scan base-package="net.mukia.repository"/>  
  17.       
  18.     <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->  
  19.     <!-- 约定优于配置,约定优于配置 -->  
  20.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  21.         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  22.         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/reader"></property>  
  23.         <property name="user" value="root"></property>  
  24.         <property name="password" value=""></property>  
  25.     </bean>  
  26.   
  27.     <!-- Hibernate 配置 -->  
  28.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  29.         <property name="dataSource" ref="dataSource"/>  
  30.         <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  
  31.         <property name="packagesToScan" value="com.xxx.model"/>  
  32.     </bean>  
  33.   
  34.     <!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 -->  
  35.     <context:component-scan base-package="net.mukia.web">  
  36.         <context:exclude-filter type="regex" expression="com.xxx.web.*" />  
  37.     </context:component-scan>  
  38.   
  39.     <!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 -->  
  40.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  41.         <property name="sessionFactory" ref="sessionFactory"></property>  
  42.     </bean>  
  43.   
  44.     <aop:config>  
  45.         <aop:advisor pointcut="execution(* com.xxxx.*Service.*(..))" advice-ref="txAdvice" />  
  46.     </aop:config>  
  47.   
  48.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  49.         <tx:attributes>  
  50.             <tx:method name="get*" read-only="true" />  
  51.             <tx:method name="query*" read-only="true" />  
  52.             <tx:method name="find*" read-only="true" />  
  53.             <tx:method name="load*" read-only="true" />  
  54.             <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />  
  55.         </tx:attributes>  
  56.     </tx:advice>  
  57. </beans> 
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值