Spring AOP 自动代理类BeanNameAutoProxyCreator的配置如下:
<bean id="autoHibernateInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionAttributes">[align=left][/align][size=large][/size]
<props>
<prop key="save*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
<property name="transactionManager">
<ref local="hibernateTransactionManager"/>
</property>
</bean>
<bean id="hibernateTransactionProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>autoInterceptor</value>
<value>autoHibernateInterceptor</value>
</list>
</property>
</bean>
要注意的是:BeanNameAutoProxyCreator的 beanNames要写我们在配置文件中已经定义好的
Bean,如
<list>
<value>*Dao</value>
</list>
如果写成如下的形式,这不会自动创建事务代理。
<list>
<value>com.asc.test.dao.*Impl</value>
</list>
我们是想为DAO。IMPL包下的所有类创建代理,但是这样写的话不行,
你可以通过事务属性设置来测试有没有产生代理。写成包的形式,我把事务属性都设置成
PROPAGATION_REQUIRED,readOnly 到样还是增删。
改成已经定义好的Bean的话,你删除增加都会出不能写的异常的。
Hibernate 是样的,Jdbc不行,还不知道是怎么回是,我把事务属性也都设置成PROPAGATION_REQUIRED,readOnly ,还是能增删。希望那位高手看到了指点迷津。
[size=large]
续篇:呵呵
上面写的我今天测过了,JDBC也一样行的,也不知道是不是昨天测错了,还是没有到位,
Hibernate那样的自动代理配置同样适合JDBC。可能是我还没真正理解代理,呵呵,
现在明白,代理就是在方法的前后添加我们自己的语句,既然可以是Hibernate事务,那也可以是JDBC的事务吧。呵呵
[/size]
<bean id="autoHibernateInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionAttributes">[align=left][/align][size=large][/size]
<props>
<prop key="save*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
<property name="transactionManager">
<ref local="hibernateTransactionManager"/>
</property>
</bean>
<bean id="hibernateTransactionProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>autoInterceptor</value>
<value>autoHibernateInterceptor</value>
</list>
</property>
</bean>
要注意的是:BeanNameAutoProxyCreator的 beanNames要写我们在配置文件中已经定义好的
Bean,如
<list>
<value>*Dao</value>
</list>
如果写成如下的形式,这不会自动创建事务代理。
<list>
<value>com.asc.test.dao.*Impl</value>
</list>
我们是想为DAO。IMPL包下的所有类创建代理,但是这样写的话不行,
你可以通过事务属性设置来测试有没有产生代理。写成包的形式,我把事务属性都设置成
PROPAGATION_REQUIRED,readOnly 到样还是增删。
改成已经定义好的Bean的话,你删除增加都会出不能写的异常的。
Hibernate 是样的,Jdbc不行,还不知道是怎么回是,我把事务属性也都设置成PROPAGATION_REQUIRED,readOnly ,还是能增删。希望那位高手看到了指点迷津。
[size=large]
续篇:呵呵
上面写的我今天测过了,JDBC也一样行的,也不知道是不是昨天测错了,还是没有到位,
Hibernate那样的自动代理配置同样适合JDBC。可能是我还没真正理解代理,呵呵,
现在明白,代理就是在方法的前后添加我们自己的语句,既然可以是Hibernate事务,那也可以是JDBC的事务吧。呵呵
[/size]