对于SPRING事务的配置,小弟有多处不是太明白的地方.一下全说出来觉得会使得问题更难理解.现在分解说出来,待有回答后进一步发文.
请问以上2段代码,这样的配置后,是否sysCodeDAO中的所有方法都得到了事务效果呢>???
[b]问题补充:[/b]
补充一下问题,如若这是相当于没有为sysCodeDAO配置事务.那在sysCodeDAO bean中注入的这个sessionFacrory又是起什么作用呢?
我知道可以在XML中用声明式配置service的事务.假设现在service中有1个方法M1,M1中调用了2个DAO的方法,D1,D2的方法任何方法也可以.声明式配置足以配置D1,D2中的方法都在一个事务中执行.
但我现在有个不明白的地方,DAO中需要配置事务吗?一般为每个DAO中买个方法配置事务的方法是什么呢?我的DAO是myeclipse自动生成的
sysCodeDAO bean中注入的这个sessionFacrory是hibernate里面封装一个数据源(数据库连接)的一个工厂对象
应该配置事务的是service层(业务逻辑层),而不是给dao层(数据访问层)配置事务,
你对于N层架构的知识还不太熟,建议好好去了解下web应用的N层架构
spring2.0的切面写法 com.press.service.impl 这个包下面的所有类的所有方法全部事务
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
classpath:hibernate.cfg.xml
<!-- 配置事务管理器 -->
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="a*" propagation="REQUIRED" />
<tx:method name="b*" propagation="REQUIRED" />
<tx:method name="c*" propagation="REQUIRED" />
<tx:method name="d*" propagation="REQUIRED" />
<tx:method name="e*" propagation="REQUIRED" />
<tx:method name="f*" propagation="REQUIRED" />
<tx:method name="g*" propagation="REQUIRED" />
<tx:method name="h*" propagation="REQUIRED" />
<tx:method name="i*" propagation="REQUIRED" />
<tx:method name="j*" propagation="REQUIRED" />
<tx:method name="k*" propagation="REQUIRED" />
<tx:method name="l*" propagation="REQUIRED" />
<tx:method name="m*" propagation="REQUIRED" />
<tx:method name="n*" propagation="REQUIRED" />
<tx:method name="o*" propagation="REQUIRED" />
<tx:method name="p*" propagation="REQUIRED" />
<tx:method name="q*" propagation="REQUIRED" />
<tx:method name="r*" propagation="REQUIRED" />
<tx:method name="s*" propagation="REQUIRED" />
<tx:method name="t*" propagation="REQUIRED" />
<tx:method name="u*" propagation="REQUIRED" />
<tx:method name="v*" propagation="REQUIRED" />
<tx:method name="w*" propagation="REQUIRED" />
<tx:method name="x*" propagation="REQUIRED" />
<tx:method name="y*" propagation="REQUIRED" />
<tx:method name="z*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的哪些方法使用事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.press.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
</aop:config>
[/code]
如果只是上面的这两个bean,相当于没有配置事务属性,
只不过是配置了一个事务管理器,HibernateTransactionManager