读书人

关于Spring配备事物管理的疑惑

发布时间: 2012-08-09 15:59:21 作者: rapoo

关于Spring配置事物管理的疑惑
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<context:annotation-config />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>
classpath:org/login/config.xml
</value>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false"/>
<tx:method name="del*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>

<!--配置哪些类的方法需要进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* org.login.service.Impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
</aop:config>

<bean id="UserDAO" class="org.login.dao.Impl.UserDAO">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="userService" class="org.login.service.Impl.UserService">
<property name="userDAO">


<ref bean="UserDAO" />
</property>
</bean>
<bean name="logon" class="org.login.actions.Login">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
<context:component-scan base-package="org.login" />
</beans>


这是我的spring的配置文件,在此已经配置过了事物处理! 可是运行之后 ServiceImpl下的一个名为get...的方法
还是能调用dao进行insert等操作! 我要的效果就是需要匹配正确的方法名才可以进行数据库操作,
显然在ServiceImpl里面的get...方法没有匹配到能够进行数据库操作的权限!求指教!

[解决办法]
execution(* org.login.service.Impl.*.*(..))改为execution(public * org.login.service.Impl.*.*(..))看你get方法前的反问修饰符
[解决办法]
你的事务是加在service层吧, ServiceImpl下的一个名为get...的方法按照这个配置,事务是只读的。当然无法执行insert。
<tx:attributes>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false"/>
<tx:method name="del*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
[解决办法]

探讨

引用:

自己顶一个! 怎么没人来?


*不是匹配所有吗? 那么* 不是包含了public了吗? get..方法前用的是public

[解决办法]
* org.login.service.Impl.*.*(..))这个是不是你要加事物的包哦,还有你<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>这个设置本来就能insert的嘛,你改成true就不能插入了
[解决办法]
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
propagation属性改成propagation=“REQUIRED”
[解决办法]
探讨

引用:

引用:

引用:

引用:

引用:

自己顶一个! 怎么没人来?


*不是匹配所有吗? 那么* 不是包含了public了吗? get..方法前用的是public


是的我要的也是你说的这种效果, 可是insert是成功的



<aop:pointc……

[解决办法]
execution(* org.login.service.Impl..*.*(..))改成这样试试

读书人网 >J2EE开发

热点推荐