直接給出示例
~~~java
// the service class that we want to make transactional
@Transactional
public class DefaultFooService implements FooService {
Foo getFoo(String fooName);
Foo getFoo(String fooName, String barName);
void insertFoo(Foo foo);
void updateFoo(Foo foo);
}
~~~
把這個service添加到spring的xml配置中即可.
>` @EnableTransactionManagement`注解添加到`@Configuration`類中即等效xml的bean配置.
>
:-: @Transactional注解方法的可見性
當使用代理時,要吧 @Transactional注解用在public的方法上.如果用在其他可見范圍的方法,不會有錯誤,但是也不會應用事務的配置.如果一定要這么做,參考AspectJ .
`@Transactional`可以放在接口,接口方法,類,類方法上.但只用`@Transactional`不足以啟動事務的行為.`@Transactional`注解只是簡單的元數據,被基礎的構件在運行時感知事務,配置事務的行為.在以前的例子中`<tx:annotation-driven/>`元素開關事務的行為.
>spring建議在具體類和方法上使用`@Transactional`,而不要在接口上.
>如果類的代理是基于類的,那么加在接口上的`@Transactional`就不會起作用.
>`@EnableTransactionManagement` 和 `<tx:annotation-driven/>` 僅僅在相同應用級別下查找`@Transactional`注解的bean.也就是說,如果為了`DispatcherServlet`在`WebApplicationContext`上添加驅動注解,只會在controller層查找`@Transactional`,而不會在service層查找.
>
事務是有優先級的,如下示例,`DefaultFooService `類級別的事務是只讀的,但是在類方法`updateFoo(Foo) `上的事務級別是高于類級別的
~~~java
@Transactional(readOnly = true)
public class DefaultFooService implements FooService {
public Foo getFoo(String fooName) {
// do something
}
// 優先級更好
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo) {
// do something
}
}
~~~
## @Transactional settings
默認配置
* 傳播行為 `PROPAGATION_REQUIRED`
* 隔離級別` ISOLATION_DEFAULT`
* 事務 `read/write`.
* 超時是依賴系統環境的,或者不支持的
* 任何`RuntimeException `回滾,未檢查異常不回滾
| 屬性 | 類型 | 說明 |
| --- | --- | --- |
| value | String | 指明使用哪種事務管理 |
| propagation | enum: Propagation | 設置傳播行為 |
| isolation | enum: Isolation | 設置隔離級別,只有在傳播行為是 REQUIRED 或 REQUIRES_NEW.才有效 |
| timeout | int 單位秒 | 設置事務超時時間,只有在傳播行為是 REQUIRED 或 REQUIRES_NEW.才有效 |
| readOnly | boolean | 事務的讀寫屬性, 只有在傳播行為是 REQUIRED 或 REQUIRES_NEW.才有效 |
| rollbackFor | Array of Class objects,Throwable的派生類 | 導致回滾的異常 |
| rollbackForClassName | Array of class names. Throwable的派生類 | 導致回滾的異常類名稱 |
| noRollbackFor | Array of Class objects,Throwable的派生類 | 不回滾的異常 |
| noRollbackForClassName | Array of class names. Throwable的派生類 | 不回滾的異常類名稱 |
目前,您無法明確控制事務的名稱,其中“name”表示將在事務監視器中顯示的事務名稱(如果適用)(例如,WebLogic的事務監視器)和日志記錄輸出。 對于聲明性事務,事務名稱始終是完全限定的類名+“.” +事務建議類的方法名稱。 例如,如果BusinessService類的handlePayment(..)方法啟動了事務,則事務的名稱將為:`com.foo.BusinessService.handlePayment`。
## Multiple Transaction Managers with @Transactional
當一個應用有多個事務管理時,屬性value可以指定使用哪種`PlatformTransactionManager `,可以指定bean的名稱或者是qualifier 的值
如下
~~~java
public class TransactionalService {
@Transactional("order")
public void setSomething(String name) { ... }
@Transactional("account")
public void doSomething() { ... }
}
~~~
結合下面的聲明事務
~~~xml
<tx:annotation-driven/>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
...
<qualifier value="order"/>
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
...
<qualifier value="account"/>
</bean>
~~~
## Custom shortcut annotations
可自定義事務簡寫模式
~~~java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("order")
public @interface OrderTx {
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("account")
public @interface AccountTx {
}
~~~
上面的例子就可以重寫
~~~java
public class TransactionalService {
@OrderTx
public void setSomething(String name) { ... }
@AccountTx
public void doSomething() { ... }
}
~~~
- 正確打開本書的姿勢
- 第一部分 Core
- 1. Ioc container
- 1.1. Introduction to the Spring IoC container and beans
- 1.2. Container overview
- 1.2.1. Configuration metadata
- 1.2.2. Instantiating a container
- 1.2.3. Using the container
- 1.3. Bean overview
- 1.3.1. Naming beans
- 1.3.2. Instantiating beans
- 1.4. Dependencies
- 1.4.1. Dependency Injection
- 1.4.2. Dependencies and configuration in detail
- 1.4.3. Using depends-on
- 1.4.4. Lazy-initialized beans
- 1.4.5. Autowiring collaborators
- 1.4.6. Method injection
- 1.5 Bean Scopes
- 1.6. Customizing the nature of a bean TODO
- 1.7. Bean definition inheritance TODO
- 1.8. Container Extension Points TODO
- 1.9. Annotation-based container configuration
- 1.9.1. @Required
- 1.9.2. @Autowired
- 1.9.3. Fine-tuning annotation-based autowiring with @Primary
- 1.9.4. Fine-tuning annotation-based autowiring with qualifiers TODO
- 1.9.5. Using generics as autowiring qualifiers TODO
- 1.9.6. CustomAutowireConfigurer TODO
- 1.10. Classpath scanning and managed components
- 1.10.1. @Component and further stereotype annotations
- 1.11. Using JSR 330 Standard Annotations TODO
- 1.12. Java-based container configuration
- 1.12.1. Basic concepts: @Bean and @Configuration
- 1.12.2. Instantiating the Spring container using AnnotationConfigApplicationContext
- 2. Resources
- 2.1. Introduction
- 2.2. The Resource interface
- 2.3. Built-in Resource implementations
- 2.3.1. UrlResource
- 2.3.2. ClassPathResource
- 2.3.3. FileSystemResource
- 2.3.4. ServletContextResource
- 2.3.5. InputStreamResource
- 2.3.6. ByteArrayResource
- 2.4. The ResourceLoader
- 2.5. The ResourceLoaderAware interface
- 2.6. Resources as dependencies
- 2.7. Application contexts and Resource paths
- 2.7.1. Constructing application contexts
- 2.7.2. Wildcards in application context constructor resource paths
- 2.7.3. FileSystemResource caveats
- 3. Validation, Data Binding, and Type Conversion
- 4. Spring Expression Language (SpEL)
- 5. Aspect Oriented Programming with Spring
- 5.1. Introduction
- 5.1.1. AOP concepts
- 5.1.2. Spring AOP capabilities and goals
- 5.1.3. AOP Proxies
- 5.2. @AspectJ support
- 5.2.1. Enabling @AspectJ Support
- 5.2.2. Declaring an aspect
- 5.2.3. Declaring a pointcut
- 5.2.4. Declaring advice
- 5.2.5. Introductions TODO
- 5.2.6. Aspect instantiation models TODO
- 5.2.7. Example
- 5.3. Schema-based AOP support TODO
- 5.4. Choosing which AOP declaration style to use TODO
- 5.5. Mixing aspect types TODO
- 5.6. Proxying mechanisms
- 5.6.1. Understanding AOP proxies
- 5.7. Programmatic creation of @AspectJ Proxies
- 5.8. Using AspectJ with Spring applications
- 5.8.1. Using AspectJ to dependency inject domain objects with Spring
- 5.8.2. Other Spring aspects for AspectJ
- 第二部分 Testing
- 第三部分 Data Access
- 1. Transaction Management
- 1.1. Introduction to Spring Framework transaction management
- 1.2 Advantages of the Spring Framework’s transaction support model
- 1.2.1. Global transactions
- 1.2.2. Local transactions
- 1.2.3. Spring Framework’s consistent programming model
- 1.3. Understanding the Spring Framework transaction abstraction
- 1.4. Synchronizing resources with transactions
- 1.4.1. High-level synchronization approach
- 1.4.2. Low-level synchronization approach
- 1.4.3. TransactionAwareDataSourceProxy
- 1.5. Declarative transaction management
- 1.5.1. Understanding the Spring Framework’s declarative transaction implementation
- 1.5.2. Example of declarative transaction implementation
- 1.5.3. Rolling back a declarative transaction
- 1.5.4. Configuring different transactional semantics for different beans
- 1.5.5. tx:advice元素的 settings
- 1.5.6. Using @Transactional
- 1.5.7. Transaction propagation
- 1.5.8. Advising transactional operations
- 1.5.9. Using @Transactional with AspectJ TODO
- 第四部分 web servlet
- 第五部分 Web Reactive
- 第六部分 Integration
- 第七部分 Languages