applicationContext.xml

  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  11. <context:component-scan base-package="com.atguigu">
  12. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  13. </context:component-scan>
  14. <!--Spring的配置文件,这里主要配置和业务逻辑有关的-->
  15. <!-- 引入数据库的配置文件 -->
  16. <context:property-placeholder location="classpath:dbconfig.properties" />
  17. <!-- ================Spring用来控制业务逻辑。数据源、事务控制、aop============== -->
  18. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  19. <property name="driverClassName" value="${jdbc.driver}"/>
  20. <property name="url" value="${jdbc.url}"/>
  21. <property name="username" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23. </bean>
  24. <!--==================spring事务管理(事务控制的配置)====================-->
  25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  26. <!--控制数据源-->
  27. <property name="dataSource" ref="dataSource"></property>
  28. </bean>
  29. <!-- 开启基于注解的事务 使用xml配置形式的事务(必要主要的都是使用配置式)-->
  30. <aop:config>
  31. <!-- 切入点表达式 -->
  32. <aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
  33. <!-- 配置事务增强 -->
  34. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
  35. </aop:config>
  36. <!--配置事务增强,事务如何切入 -->
  37. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  38. <tx:attributes>
  39. <!-- 所有方法都是事务方法 -->
  40. <tx:method name="*"/>
  41. <!--以get开始的所有方法 -->
  42. <tx:method name="get*" read-only="true"/>
  43. </tx:attributes>
  44. </tx:advice>
  45. <!--===============配置和Mybatis的整合====================-->
  46. <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  47. <!--指定Mybatis全局配置文件的位置-->
  48. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  49. <property name="dataSource" ref="dataSource"></property>
  50. <!-- 指定mybatis,mapper文件的位置 -->
  51. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  52. </bean>
  53. <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
  54. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  55. <!--扫描所有dao接口的实现,加入到ioc容器中 -->
  56. <property name="basePackage" value="com.atguigu.crud.dao"></property>
  57. </bean>
  58. <!--配置一个可以执行批量的SqlSession-->
  59. <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
  60. <constructor-arg name="sqlSessionFactory" ref="sessionFactory"></constructor-arg>
  61. <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  62. </bean>
  63. <!--spring配置文件的核心点(数据源、与mybatis的整合,事务控制)-->
  64. </beans>