基於XML配置宣告式事務

2021-10-11 22:54:45 字數 2711 閱讀 1250

<

?xml version=

"1.0" encoding=

"utf-8"

?>

""xmlns:xsi=

""xmlns:context=

"" xmlns:tx=

""xmlns:aop=

""xsi:schemalocation=

" /spring-beans.xsd /spring-tx.xsd "

>

<

!-- 掃瞄包--

>

package

="com.lwt"

>

<

/context:component-scan>

"classpath:dbconfig.properties"

>

<

/context:property-placeholder>

<

!-- 配置德魯伊資料來源--

>

"datasource"

class

="com.alibaba.druid.pool.druiddatasource"

>

"username" value=

"$">

<

/property>

"password" value=

"$">

<

/property>

"url" value=

"$">

<

/property>

"driverclassname" value=

"$">

<

/property>

<

/bean>

<

!-- 配置jdbctemplate--

>

="org.springframework.jdbc.core.jdbctemplate"

>

"datasource" value=

"#">

<

/property>

<

/bean>

<

!-- 事務控制

1.配置事務管理器,匯入1個基礎版+

3個增強版的切面jar包

-->

"datasourcetransactionmanager"

class

="org.springframework.jdbc.datasource.datasourcetransactionmanager"

>

"datasource" ref=

"datasource"

>

<

/property>

<

/bean>

<

!-- 基於xml配置的事務,依賴tx、aop命名空間

1.spring提供事務管理器(切面),配置事務管理器bean

2.配置出事務方法

3.哪些方式是事務方法,事務切面按照切入點表示式切入事務方法

-->

"txpointcut" expression=

"execution( * com.lwt.xml_tx.*.*(..))"

/>

<

!-- advice-ref=

""指向事務管理器的配置--

>

"myadvice" pointcut-ref=

"txpointcut"

>

<

/aop:advisor>

<

/aop:config>

<

!-- 配置事務管理器

transaction-manager指定配置哪個事務管理器,相當於註解@transacional

tx:advice 事務建議|事務增強|事務屬性

-->

"myadvice" transaction-manager=

"datasourcetransactionmanager"

>

<

!-- 事務屬性tx:attributes--

>

<

!-- 指明哪些方法是事務方法tx:method,和切入點表示式的方法對比,哪些方法加事務用tx:method指定

*給所有方法都加事務

-->

"*"/

>

"checkout" propagation=

"required" timeout=

"-1"

>

<

/tx:method>

"get*" read-only=

"true"

>

<

/tx:method>

<

/tx:attributes>

<

/tx:advice>

<

/beans>

public

class

txtest

/** * 多事務

*/@test

public

void

test2()

throws filenotfoundexception

}

基於XML的宣告式事務控制

完整配置示例 由於事務控制基於aop,所以要匯入xml命名空間和約束xmlns aop。還要匯入事務的命名空間與約束xmlns tx xmlns xmlns xsi xmlns aop xmlns tx xsi schemalocation tx adviceid txadvice transact...

Spring宣告式事務(xml配置事務方式)

spring宣告式事務 xml配置事務方式 蕃薯耀 2016年5月24日 10 12 13 星期二 xlm配置如下 注意 當事務切點配置成這樣時 即expression中紅色標記的地方 expression execution com.lqy.spring.iwx.service.impl.配置成這...

Spring宣告式事務(xml配置事務方式)

spring宣告式事務 xml配置事務方式 蕃薯耀 2016年5月24日 10 12 13 星期二 xlm配置如下 注意 當事務切點配置成這樣時 即expression中紅色標記的地方 expression execution com.lqy.spring.iwx.service.impl.配置成這...