Spring AOPソースコードの詳細な分析:いくつかのアノテーションでトランザクションを制御できますか?それはとても魔法ですか?


宣言型トランザクションは非常に便利で、特に純粋なアノテーションモードでは、トランザクションを制御できるアノテーションはごくわずかです。

思考:これらの注釈は何をしましたか?本当に素晴らしい!

@EnableTransactionManagement @Transactional

一、@ EnableTransactionManagement

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
    
    

}

@EnableTransactionManagementアノテーションは@ImportタグTransactionManagementConfigurationSelector类参照され、このクラスは2つの重要なコンポーネントをコンテナにインポートします


この記事は、アシスタントVXを追加する必要がある学生である「SpringAdvancedSourceNotes」に言及しています。C18173184271,备注一下CSDN+工作年限!無料で入手してください。

2つ目は、トランザクション制御コンポーネントをロードすることです。

  • AutoProxyRegistrar
    AutoProxyRegistrar 类registerBeanDefinitions方法が、コンポーネントを登録し、

    中にAopConfigUtils.registerAutoProxyCreatorIfNecessary本方法は

    、最終的に見つかったと呼ばれる登録InfrastructureAdvisorAutoProxyCreatorBean、このクラスはAbstractAutoProxyCreatorサブクラス実装SmartInstantiationAwareBeanPostProcessorインタフェース
public class InfrastructureAdvisorAutoProxyCreator extends
AbstractAdvisorAutoProxyCreator

public abstract class AbstractAdvisorAutoProxyCreator extends
AbstractAutoProxyCreator

public abstract class AbstractAutoProxyCreator extends
ProxyProcessorSupport
 	implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware

以下継承アーキテクチャ図

それ実装SmartInstantiationAwareBeanPostProcessorこれはポストプロセッサであるが、であると述べ、spring AOPオープン@EnableAspectJAutoProxy登録時にAnnotationAwareAspectJProxyCreator実装ように、同じインタフェースである宣言トランザクションがあるspringAOPアプリケーション思考

  • ProxyTransactionManagementConfiguration 成分
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.transaction.annotation;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.interceptor.BeanFactoryTransactionAttribut eSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
* {@code @Configuration} class that registers the Spring infrastructure
beans
* necessary to enable proxy-based annotation-driven transaction
management.
*
* @author Chris Beams
* @since 3.1
* @see EnableTransactionManagement
* @see TransactionManagementConfigurationSelector
*/
@Configuration
public class ProxyTransactionManagementConfiguration extends
AbstractTransactionManagementConfiguration {
    
    
 
 	@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
 	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
 	public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(){
    
    
 		// 事务增强器
 		BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
 		// 向事务增强器中注入 属性解析器 transactionAttributeSource
 		advisor.setTransactionAttributeSource(transactionAttributeSource());
 		// 向事务增强器中注入 事务拦截器 transactionInterceptor
 		advisor.setAdvice(transactionInterceptor());
 		if (this.enableTx != null) {
    
    
 			advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
 		}
 		return advisor;
 	}
 	
 	@Bean
 	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
 	// 属性解析器 transactionAttributeSource
 	public TransactionAttributeSource transactionAttributeSource() {
    
    
 		return new AnnotationTransactionAttributeSource();
 	}
	@Bean
 	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
 	// 事务拦截器 transactionInterceptor
 	public TransactionInterceptor transactionInterceptor() {
    
    
 		TransactionInterceptor interceptor = new TransactionInterceptor();
 
		interceptor.setTransactionAttributeSource(transactionAttributeSource());
 		if (this.txManager != null) {
    
    
 			interceptor.setTransactionManager(this.txManager);
 		}
 		return interceptor;
 	} 
}

ProxyTransactionManagementConfiguration構成クラスは、トランザクションブースターtransactionAdvisorと呼ばれるコンテナー、コンポーネントレジスタであり、トランザクションエンハンサーは2つのプロパティを注入しました。つまり、パーサートランザクションインターセプターの属性です。transactionAttributeSource transactionAttributeSource transactionInterceptor

  • AnnotationTransactionAttributeSourceソースコードの属性パーサー部分は次のとおりです


属性パーサーにannotationParsersは、コレクションであるメンバー変数があります。さまざまなアノテーションパーサーを追加できます
TransactionAnnotationParser。Springのアノテーションパーサーに注目します

ソースコードの一部は次のとおりです。プロパティパーサーの機能の1つは次のとおりです。@Transactionアノテーションを解析します。

  • TransactionInterceptor トランザクションインターセプター、ソースコードの一部は次のとおりです

  • 上記のコンポーネントはどのように関連していますか?
    • トランザクションインターセプターはMethodInterceptorインターフェイスを実装し、上記のInfrastructureAdvisorAutoProxyCreatorポストプロセッサーを振り返ってみると、ターゲットのプロキシオブジェクト実行メソッドのときにインターセプターチェーンを取得しますが、インターセプターはこのチェーンTransactionInterceptorであり、これら2つのコンポーネントをリンクします。
    • 構築メソッドはPlatformTransactionManagerトランザクションマネージャー)とTransactionAttributeSource属性パーサー)に渡されますProxyTransactionManagementConfiguration上記のソースコードをトレースすると、トランザクションインターセプターを登録するときに、パラメーター付きのこの構築メソッドは呼び出されませんが、パラメーターなしの構築メソッドが呼び出されます。これらの2つのプロパティを注入するsetメソッドの場合、効果は同じです。
  • invokeWithinTransaction 方法、ソースコードの一部は次のとおりです(ラベル1、2、3、4に注意してください)


このフルバージョンが必要な場合は《Spring高级源码笔记》、この記事で私をサポートするだけで済みます。

多くのサポート、あなたは無料で情報を得ることができます-3年連続で(約束:100%無料)

クイックスタートチャンネル:アシスタントVXを追加:C18173184271,备注一下CSDN+工作年限!無料で入手!誠意あふれる!

おすすめ

転載: blog.csdn.net/Java_Caiyo/article/details/113180674