zoukankan      html  css  js  c++  java
  • Annotation Type EnableTransactionManagement

    http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html

    org.springframework.transaction.annotation

    Annotation Type EnableTransactionManagement



    • @Target(value=TYPE)
       @Retention(value=RUNTIME)
       @Documented
       @Import(value=TransactionManagementConfigurationSelector.class)
      public @interface EnableTransactionManagement
      Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. To be used on @Configuration classes as follows:
       @Configuration
       @EnableTransactionManagement
       public class AppConfig {
      
           @Bean
           public FooRepository fooRepository() {
               // configure and return a class having @Transactional methods
               return new JdbcFooRepository(dataSource());
           }
      
           @Bean
           public DataSource dataSource() {
               // configure and return the necessary JDBC DataSource
           }
      
           @Bean
           public PlatformTransactionManager txManager() {
               return new DataSourceTransactionManager(dataSource());
           }
       }

      For reference, the example above can be compared to the following Spring XML configuration:

       <beans>
      
           <tx:annotation-driven/>
      
           <bean id="fooRepository" class="com.foo.JdbcFooRepository">
               <constructor-arg ref="dataSource"/>
           </bean>
      
           <bean id="dataSource" class="com.vendor.VendorDataSource"/>
      
           <bean id="transactionManager" class="org.sfwk...DataSourceTransactionManager">
               <constructor-arg ref="dataSource"/>
           </bean>
      
       </beans>
       
      In both of the scenarios above, @EnableTransactionManagement and <tx:annotation-driven/> are responsible for registering the necessary Spring components that power annotation-driven transaction management, such as the TransactionInterceptor and the proxy- or AspectJ-based advice that weave the interceptor into the call stack when JdbcFooRepository's @Transactional methods are invoked.

      A minor difference between the two examples lies in the naming of the PlatformTransactionManager bean: In the @Bean case, the name is "txManager" (per the name of the method); in the XML case, the name is"transactionManager". The <tx:annotation-driven/> is hard-wired to look for a bean named "transactionManager" by default, however @EnableTransactionManagement is more flexible; it will fall back to a by-type lookup for any PlatformTransactionManager bean in the container. Thus the name can be "txManager", "transactionManager", or "tm": it simply does not matter.

      For those that wish to establish a more direct relationship between @EnableTransactionManagement and the exact transaction manager bean to be used, the TransactionManagementConfigurer callback interface may be implemented - notice the implements clause and the @Override-annotated method below:

       @Configuration
       @EnableTransactionManagement
       public class AppConfig implements TransactionManagementConfigurer {
      
           @Bean
           public FooRepository fooRepository() {
               // configure and return a class having @Transactional methods
               return new JdbcFooRepository(dataSource());
           }
      
           @Bean
           public DataSource dataSource() {
               // configure and return the necessary JDBC DataSource
           }
      
           @Bean
           public PlatformTransactionManager txManager() {
               return new DataSourceTransactionManager(dataSource());
           }
      
           @Override
           public PlatformTransactionManager annotationDrivenTransactionManager() {
               return txManager();
           }
       }
      This approach may be desirable simply because it is more explicit, or it may be necessary in order to distinguish between two PlatformTransactionManager beans present in the same container. As the name suggests, the annotationDrivenTransactionManager() will be the one used for processing @Transactional methods. See TransactionManagementConfigurer Javadoc for further details.

      The mode() attribute controls how advice is applied; if the mode is AdviceMode.PROXY (the default), then the other attributes control the behavior of the proxying.

      If the mode() is set to AdviceMode.ASPECTJ, then the proxyTargetClass() attribute is obsolete. Note also that in this case the spring-aspects module JAR must be present on the classpath.

      Since:
      3.1
      Author:
      Chris Beams
      See Also:
      TransactionManagementConfigurerTransactionManagementConfigurationSelectorProxyTransactionManagementConfigurationAspectJTransactionManagementConfiguration
  • 相关阅读:
    安装IDM扩展
    Go_数组&切片
    Mycat概念&安装
    IDEA自定义主题
    完全卸载Oracle11g
    创建型模式——单例模式(Singleton)
    设计模式统计
    PHP解压带密码的zip文件
    Win推荐软件
    如何设置线程池的线程数?
  • 原文地址:https://www.cnblogs.com/wuyifu/p/6382655.html
Copyright © 2011-2022 走看看