直接给出示例
~~~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