spring整合hibernate中的web.xml问题

下面是网上看的别人spring整合hibernate的web。xml文件
可我搞不懂最后那个几个是什么意思:配置spring声明式事务,配置事务事务属性,配置事务切点,并把切点和事务属性关联起来。

求大神讲解讲解!

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/sch
ema/tx/spring-tx-4.1.xsd">

<context:component-scan base-package="com.demo.ssm"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/test" />
    <property name="username" value="root"></property>
    <property name="password" value="281889"></property>
</bean>  
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="false">
    <!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <!-- //加载实体类的映射文件位置及名称 -->
    <property name="mappingLocations" value="classpath:com/demo/ssm/po/*.hbm.xml"></property>
</bean>  

<!-- 配置Spring声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 
<!-- 配置事务事务属性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<!-- 配置事务切点,并把切点和事务属性关联起来 -->
<aop:config>
    <aop:pointcut expression="execution(* com.demo.ssm.daoImpl.*.*(..))" id="txPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

这么说吧,hibernate使用时肯定也会涉及到事务。事务的使用保证了操作一致性,错误的回滚等数据库操作基本属性。
那么Spring整合Hibernate时,配置的事务+事务属性+事务切点就是通过aop的方式将所有需要事务管理的方法统一用事务包裹起来。在实际代码中
只需要关注业务逻辑的实现,而不需要重复transaction.commit(),transaction.rollback()等操作。
所以
1、配置Spring声明式事务 就是通过配置的sessionFactory获取事务管理器。等同于Hibernate中session.beginTransaction()。
2、同样在Hibernate中使用事务,会在每个方法中重复写transaction.commit()之类的事务操作代码。
而使用Spring+Hibernate配置好事务管理器后,需要知道那些地方需要事务管理。
就是由
确定适用方法是com.demo.ssm.daoImpl下所有类的所有方法,再根据

tx:attributes


/tx:attributes
/tx:advice
确定是以get开头的方法使用只读事务,其他方法都正常使用事务。
所以是在切面com.demo.ssm.daoImpl下所有类的切点所有方法使用事务管理。
以上主要关于Spring aop和Spring 事务管理的知识,可以查阅相关资料了解。

这个貌似不是web.xml吧,应该是spring的配置文件,,

 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

这句话就是把事务和切点关联起来,,,通俗点就是,再切点附近干点什么,,给这些切点【txPointcut】,,前后加上事务处理,开启事务/提交事务/回滚事务等操作。

这里用的是《aop:advisor》,,Advisor是Pointcut和Advice的配置器,它包括Pointcut和Advice,是将Advice注入程序中Pointcut位置的代码,
【spring 中的aop:advisor和aop:aspect的区别】:http://www.cnblogs.com/tangmj/p/5181656.html


tx:attributes


/tx:attributes
/tx:advice
这个”get* “ read-only这个配置代表你的的所有方法以get开头命名的方法开启事物并且是只读,下面那个代表所有的方法都开启事物

aop:config


/aop:config

"execution(* com.demo.ssm.daoImpl.*.*(..))这个第一个*代表包com.demo.ssm.daoImpl下的任意类,第二个*代表任意方法,(。。)代表任意方法
这里的expression就是拦截这个包下所有,下面是把上面配置的事物属性配置进来。综合起来就是这个包名下的所有类所有方法中以get打头的方法开启事物,并且只读,其他的都开启事物,所以,如果你那里不配置*的话,就不是所有的方法都开启事物了

<!-- 配置Spring声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 
<!-- 配置事务事务属性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>满足get开头的方法,执行自动提交事务,只读
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<!-- 配置事务切点,并把切点和事务属性关联起来 -->
<aop:config>
    <aop:pointcut expression="execution(* com.demo.ssm.daoImpl.*.*(..))" id="txPointcut"/>定义切点,到了dao实现类开启事务
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

<?xml version="1.0" encoding="UTF-8"?>

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描除了controller之外的所有包 -->


/context:component-scan

<!-- 配置连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/tong201782?characterEncoding=GBK"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
<!-- 配置sqlsession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="mapperLocations">
        <list>
            <value>classpath:com/tongkai/dao/*Mapper.xml</value>
        </list>
    </property>
    <property name="typeAliasesPackage" value="com.tongkai.pojo"></property>
</bean>
<!-- 配置mapperScannerConigure -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.tongkai.dao"></property>
    <property name="sqlSessionFactory" ref="sqlSession"></property>
</bean>
<!-- 配置事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>