刚开始自学SSH,从网上找的demo里的配置文件里事务管理有几段不了解的地方,
在网上找不到,求热心人解释一下,最好稍微解释细点,标头和属性还有每一段的作用。。谢谢!
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.huake.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置hibernate模版,利用sessionFactory获取session -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置hibernate事务管理器-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置切面 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置切入点 -->
<aop:config>
<aop:pointcut id="bussinessService" expression="execution(public * com.huake.service.*.*(..))" />
<!-- 切入点用入通知中 -->
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
这个是spring的数据库会话工厂由hibernate引用,由hibernate处理数据库的存储操作。
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
这个是配置事务的引用
aop:config
expression="execution(public * com.huake.service.*.*(..))" />
/aop:config
通过aop的方式对指定的路径下的类使用事务
tx:attributes
/tx:attributes
/tx:advice
这个是spring的织网设置,仅对add开头的方法生效,每次会重新创建一个新的事务。
这是我以前写的,你可以对照看看
<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="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点, 以及把事务切入点和事务属性关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))"
id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
<!--其作用是匹配自己写的实体类-->
<property name="annotatedClasses">
<list>
<!-- 列出全部的标有hibernate注解的PO类 -->
<value>com.iflysse.model.UserInfo</value>
<value>com.iflysse.model.OperInfo</value>
<value>com.iflysse.model.FileInfo</value>
<value>com.iflysse.model.ReplyInfo</value>
<value>com.iflysse.model.NewsInfo</value>
</list>
</property>
<!-- 定义Hibernate SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<!-- SQL方言 -->
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="current_session_context_class">thread</prop>
<!-- 二级缓存 -->
<prop key="cache.use_second_level_cache">false</prop>
<prop key="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>
<!-- 查询缓存 -->
<prop key="cache.use_query_cache">false</prop>
<!-- 输出执行的SQL语句 -->
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
<!-- 是否根据映射创建数据表 -->
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 配置Hibernate局部事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!--name="sessionFactory"此处的name中的内容是根据上一个bean的id来的,上一个bean的id是什么这里的name中内容就是什么
,为了代码的严谨ref的内容是跟name的相同-->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务增强处理Bean,指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 所有已get开头的方法是只读的 -->
<tx:method name="get*" read-only="true" />
<!-- 其他方法使用默认的事务管理器 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--aop是一个面向切面编程思想,上面一个tx已经将切入点定义好,在下面的aop中链接切入点,就能完成代码之间的相互传递-->
<!-- AOP配置切入点元素 -->
<aop:config>
<!-- 此处是为了来搜寻你的在com.iflysse.service.impl该包目录下的所有的类-->
<aop:pointcut expression="execution(* com.iflysse.service.impl.*.*(..))"
id="txPoint" />
<!-- advice-ref="txAdvice" 将上面的定义的切入点用入通知中 ,pointcut-ref="txPoint"将搜索到的类作为切入点进行使用-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
这是我以前写过的一些代码,希望能帮到你
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--其作用是匹配自己写的实体类-->
<property name="annotatedClasses">
<list>
<!-- 列出全部的标有hibernate注解的PO类 -->
<value>com.iflysse.model.UserInfo</value>
<value>com.iflysse.model.OperInfo</value>
<value>com.iflysse.model.FileInfo</value>
<value>com.iflysse.model.ReplyInfo</value>
<value>com.iflysse.model.NewsInfo</value>
</list>
</property>