首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

用BeanNameAutoProxyCreator自动创办事务代理

2012-08-17 
用BeanNameAutoProxyCreator自动创建事务代理用BeanNameAutoProxyCreator自动创建事务代理下面介绍一种优

用BeanNameAutoProxyCreator自动创建事务代理
用BeanNameAutoProxyCreator自动创建事务代理
下面介绍一种优秀的事务代理配置策略:采用这种配置策略,完全可以避免增量式配置,所有的事务代理由系统自动创建。容器中的目标bean自动消失,避免需要使用嵌套bean来保证目标bean不可被访问。
这种配置方式依赖于Spring提供的bean后处理器,该后处理器用于为每个bean自动创建代理,此处的代理不仅可以是事务代理,也可以是任意的代理,只需要有合适的拦截器即可。

下面是采用BeanNameAutoProxyCreator配置事务代理的配置文件:
[xhtml] view plaincopy

    <?xml version="1.0" encoding="gb2312"?> 
    <!--  Spring配置文件的文件头,包含DTD等信息--> 
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
        "http://www.springframework.org/dtd/spring-beans.dtd"> 
    <beans> 
        <!--定义数据源--> 
        <bean id="dataSource" ref="transactionManager"/> 
            <property name="transactionAttributes"> 
                <!--  下面定义事务传播属性--> 
                <props> 
                    <prop key="insert*">PROPAGATION_REQUIRED</prop> 
                    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
                    <prop key="*">PROPAGATION_REQUIRED</prop> 
                </props> 
            </property> 
        </bean> 


<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理
[xhtml] view plaincopy

        <bean value="true" />
该属性值默认为false。

热点排行