如何让服务在Spring中使用特定的txAdvice?

发布于 2024-11-07 02:13:54 字数 1452 浏览 10 评论 0原文

我正在尝试升级我的项目,因此我开始进行交易。到目前为止我就是这样做的。

<bean id="userServiceTarget" class="com.forgin.service.UserServiceImpl">
    <property name="userDAO" ref="userDAO" />
</bean>

<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target" ref="userServiceTarget" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_SUPPORTS</prop>
                <prop key="is*">PROPAGATION_SUPPORTS</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
</bean>

我像下面这样更改了事务属性,但我不太确定如何将服务与这个 txAdvice 链接起来。因为我通常对不同的服务有不同的交易属性,所以我想应该有多个 txAdvice。有没有办法说 @Transactional 来使用这个特定的 txAdvice?

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="is*" read-only="true" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

I'm trying to upgrade my project and so I've come to transactions. This is how I did it up to now.

<bean id="userServiceTarget" class="com.forgin.service.UserServiceImpl">
    <property name="userDAO" ref="userDAO" />
</bean>

<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target" ref="userServiceTarget" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_SUPPORTS</prop>
                <prop key="is*">PROPAGATION_SUPPORTS</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
</bean>

I changed the transaction attributes like this bellow, but I'm not quite sure how could I link the service with exactly this txAdvice. Cause I generally have different transaction attributes for different services so there I guess should be more than one txAdvice. Is there a way how to say the @Transactional to use this particular txAdvice?

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="is*" read-only="true" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-11-14 02:13:54

好吧,我明白了..它就在我身边。哈哈..我只需要提供额外的aop:advisoraop:pointcut。就这么简单。

<aop:config>
    <aop:pointcut id="userOperation"
            expression="execution(* com.forgin.service.UserServiceImpl.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
        <aop:pointcut id="differentOperation"
            expression="execution(* com.forgin.service.DifferentServiceImpl.*(..))" />
    <aop:advisor advice-ref="txAdviceDifferent" pointcut-ref="differentOperation" />
</aop:config>

Alright, I figured out it.. it just came to me. Haha.. I just need to provide additional aop:advisor and aop:pointcut. As simple as that.

<aop:config>
    <aop:pointcut id="userOperation"
            expression="execution(* com.forgin.service.UserServiceImpl.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
        <aop:pointcut id="differentOperation"
            expression="execution(* com.forgin.service.DifferentServiceImpl.*(..))" />
    <aop:advisor advice-ref="txAdviceDifferent" pointcut-ref="differentOperation" />
</aop:config>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文