spring用BeanNameAutoProxyCreator自动创建事务代理

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

(1)数据源

1
2
3
4
5
6
<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
< bean id = "dataSource" class = "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" >
    < property name = "targetDataSource" >
        < ref local = "boneCPDataSource" />
    </ property >
</ bean >

(2)事务

1
2
3
4
<!-- 数据库 事务管理器 -->
< bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    < property name = "dataSource" ref = "dataSource" />
</ bean >

(3)事务拦截器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--   配置事务拦截器-->
< bean id = "transactionInterceptor"
      class = "org.springframework.transaction.interceptor.TransactionInterceptor" >
    <!--   事务拦截器bean需要依赖注入一个事务管理器 -->
    < property name = "transactionManager" 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 >

(4)定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- 自动创建事务代理  -->
< bean class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
    <!-- 指定对满足哪些bean name的bean自动生成业务代理  -->
    < property name = "beanNames" >
        <!-- 下面是所有需要自动创建事务代理的bean -->
        < list >
            < value >*Dao</ value >
            < value >*Manager</ value >
            < value >*Service</ value >
            < value >*Disp</ value >
            < value >*DispImpl</ value >
        </ list >
    </ property >
    <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->
    < property name = "interceptorNames" >
        < list >
            < value >transactionInterceptor</ value >
        </ list >
    </ property >
</ bean >

定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理

TranscationInterceptor是一个事务拦截器bean,需要传入一个TransactionManager的引用。配置中使用Spring依赖注入该属性,事务拦截器的事务属性通过transactionAttributes来指定,该属性有props子元素,配置文件中定义了三个事务传播规则:
所有以insert开始的方法,采用PROPAGATION_REQUIRED的事务传播规则。程序抛出MyException异常及其子异常时,自动回滚事务。所有以find开头的方法,采用PROPAGATION_REQUIRED事务传播规则,并且只读。其他方法,则采用PROPAGATION_REQUIRED的事务传播规则。
BeanNameAutoProxyCreator是个根据bean名生成自动代理的代理创建器,该bean通常需要接受两个参数。第一个是beanNames属性,该属性用来设置哪些bean需要自动生成代理。另一个属性是interceptorNames,该属性则指定事务拦截器,自动创建事务代理时,系统会根据这些事务拦截器的属性来生成对应的事务代理。


本文出自 “十年一剑” 博客,请务必保留此出处http://sauron.blog.51cto.com/5231038/1272267

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值