java – Spring中org.springframework.transaction.interceptor.TransactionProxyFactoryBean的用法是什么

在我的项目中,所有管理器类都实现了这种模式,

<bean id="companyManagerTxProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="proxyTargetClass"><value>true</value></property>
        <property name="transactionAttributes">
             <props>
                <prop key="create*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean id="companyAdminManager" parent="companyManagerTxProxy" scope="prototype">
        <property name="target">
            <bean class="lucky.src.bto.controllerImpl.CompanyAdminManagerImpl">
            </bean>
        </property>
    </bean>

你能解释一下为什么我们在创建所有经理的bean时使用org.springframework.transaction.interceptor.TransactionProxyFactoryBean.它的用途是什么?

最佳答案 这是一个广泛的问题,因为这里有概念性的解释.非常简短:TransactionProxyFactoryBean是Spring的Transaction支持的一部分,允许应用程序以与服务器无关的方式具有“事务”行为.换句话说:如果使用TransactionProxyFactoryBean配置服务,则可以替换EJB容器管理Bean.

Please refer to Spring Documentation

您可能需要了解整体交易的概念.但一个非常简短的解释是:

Transactions (read or write of information) must be 07001 and
ensure that we need to give “transactional” behavior the software
resources (code) which interact with the database (there may be other
transactions).

上面的解释涉及AOP(面向方面​​编程),其中,您希望为事务提供的行为是“方面”.

通常需要配置三件事:

transactionManager, target and transactionAttribute.

transactionAttribute是您为资源的读写提供事务行为的地方.在您的示例中,您的companyAdminManager被赋予了trnaactional行为.反过来,companyAdminManager必须配置数据源,该数据源将包含db url,user / pass和其他相关信息.

Here are two good explanations (explains each and every line)

1.) 07002

2.) I find the following blog post by Scot to be basic and easy to understand example with explanation. Please read 07003

点赞