<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 声明数据源DataSource,作用是连接数据库的-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- set注入给DruidDataSource提供连接数据信息-->
<property name="url" value="${jdbc.url}"/> <!-- seturl()-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.max}"/>
</bean>
<!-- 声明的是mybatis中提供的sqlSessionFactorybean类,这个类的内部创建SQLSessionFactoryde-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- set注入,把数据库连接池赋给了dataSource属性-->
<property name="dataSource" ref="myDataSource"/>
<!-- mybatis主配置文件的位置 configLocation属性是Resource类型,读取配置文件的赋值,使用value ,指定文件的路径,使用classpath表示文件的位置-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 创建dao对象,使用sqlSession的getMapper(StudentDao.class)
MapperScannerConfigurer:在内部调用getMapper()生成每个dao接的代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定SqlSessionFactory对象的ID-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,包名是dao接口所在的包名。MapperScannerConfigurer会扫描这个保重的所有接口,
把每个接口都执行一次getMappper()方法,得到每个接口的dao对象。
创建好的dao对象放入到Spring的容器中
-->
<property name="basePackage" value="org.qiang.dao"/>
</bean>
<!-- 声明service-->
<bean id="buyService" class="org.qiang.service.Impl.BuyGoodsServiceImpl">
<property name="goodsDao" ref="goodsDao"/>
<property name="saleDao" ref="saleDao"/>
</bean>
<!-- 声明事务处理:和源代码完全分离的-->
<!-- 1.声明事务管理器的对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 2.声明业务方法的事务属性(隔离级别,传播行为,超时时间)-->
<!-- id:自定义名称,表示:<tx:advice>和</tx:advice> 之间的配置内容-->
<!-- transaction-manager:事务管理对象的id-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性-->
<tx:attributes>
<!-- <tx:method :给具体的方法配置事务属性,method可以有多个,分别给不同的方法设置事务属性-->
<!-- name:方法的名称 1)完整的方法名称,不带有包和类-->
<!-- 2)方法可以使用通配符* 表示任意字符-->
<!-- propagation:传播行为,枚举值-->
<!-- isolation:隔离级别-->
<!-- rollback-for:你指定的异常类名,权限定类名,发生异常一定回滚-->
<tx:method name="buy" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.NullPointerException,org.qiang.excep.NotEnoughException"/>
<!-- 使用通配符,指定很多方法-->
<tx:method name="add" propagation="REQUIRES_NEW"/>
<!-- 指定修改事务-->
<tx:method name="modify" />
<!-- 删除方法-->
<tx:method name="remove"/>
<!-- 查询方法,query,search,find-->
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop-->
<aop:config>
<!-- 配置切入点表达式:指定那些包中类要使用事务-->
<!-- id:qierudian表示名称,唯一值-->
<!-- expression:切入点表达式,指定那些类要使用事务,aspectj会创建代理对象-->
<aop:pointcut id="service" expression="execution(* *..service..*.*(..)) "/>
<!-- 配置增强器:关联adivce 和pointcut
advice-ref通知,上面tx:advice哪里配置
pointcut-ref:切入点表达式的id
-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="service"/>
</aop:config>
</beans>