Spring bootソース解析34-spring boot統合spring cache(EHcacheベース)


前言
本文は前の文章の基礎の上で修正をして、それにEhcacheを使用させます.
Ehcache統合
  • pomファイルには、
    <dependency>
        <groupId>net.sf.ehcachegroupId>
        <artifactId>ehcacheartifactId>
    dependency>
  • という構成が追加されています.
  • src/main/resourcesディレクトリの下にehcacheを加える.xml.コードは以下の通り:
    
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
    
    
    
    <diskStore path="java.io.tmpdir/ehcache" />
    
    
    
    
    
    
         
    <defaultCache maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" />
    
     
     <cache name="users"
           maxEntriesLocalHeap="200"
           timeToLiveSeconds="600">
     cache>
    ehcache>  
    Ehcacheの構成について、以下のリンクを参照することができる:Ehcache統合spring構成Ehcache構成の詳細とCacheManagerの使用この時、私たちは修正して、これもspring cacheの威力を体験することができて、下層の実現に依存しない.

  • じどうくみたて
    EhCacheCacheManagerの自動組立はEhCacheCacheConfigurationにある.
    ここでまず1つ質問ですが、spring bootでデフォルトでサポートされているのはConcurrentMapCacheManagerです.今からehcacheの依存に参加すると、2つあるのではないでしょうか.これで問題がありますか?
    答え:いいえ.理由は次のとおりです.
  • chcheの自動組立の総入口はCacheAutoConfigurationにあり、このクラスは@Import(CacheConfigurationImportSelector.class)を介してCacheConfigurationImportSelectorに導入する.このクラスはImportSelectorのタイプであるため、そのselectImportsメソッドが実行する、その戻り値をConfigurationClassParser#processImports処理に呼び出す.そのselectImportsコードは以下の通りである:
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
                CacheType[] types = CacheType.values();
                String[] imports = new String[types.length];
                for (int i = 0; i < types.length; i++) {
                    imports[i] = CacheConfigurations.getConfigurationClass(types[i]);
                }
                return imports;
    }
    CacheTypeの列挙集合を巡り、対応する構成クラスを順次取得し、importsに追加された最後の戻り値は以下の通りである.
  • GenericCacheConfiguration.class
  • JCacheCacheConfiguration.class
  • EhCacheCacheConfiguration.class
  • HazelcastCacheConfiguration.class
  • InfinispanCacheConfiguration.class
  • CouchbaseCacheConfiguration
  • RedisCacheConfiguration
  • CaffeineCacheConfiguration
  • GuavaCacheConfiguration
  • SimpleCacheConfiguration
  • NoOpCacheConfiguration

  • GenericCacheConfigurationでは、
    @Configuration
    @ConditionalOnBean(Cache.class)
    @ConditionalOnMissingBean(CacheManager.class)
    @Conditional(CacheCondition.class)
    のクラスが有効になります.
  • JCacheCacheConfiguration,HazelcastCacheConfiguration,...GuavaCacheConfigurationには、クラスパスの下に関連するクラスが存在する必要がある.したがって、デフォルトではPARSE_CONFIGURATIONフェーズで有効な構成は、GenericCacheConfiguration,EhCacheCacheConfiguration,SimpleCacheConfiguration,NoOpCacheConfigurationの
  • です.
  • 最後に、C o n f i g u r ationClassBeanDefinitionReader#loadBeanDefinitionsForConfigurationClass(C o n f i g u r ationClass,TrackedConditionEvealuator)を実行するとき:
  • GenericCacheConfiguration:@ConditionalOnBean(Cache.class)はREGISTER_BEANステージ時に実行する、beanFactoryにCacheタイプのbeanは存在しない.したがって、このクラスは登録する.証明:クエリー/autoconfig、GenericCacheConfigurationの結果は次のとおりです:
    GenericCacheConfiguration: {
    notMatched: [
    {
    condition: "OnBeanCondition",
    message: "@ConditionalOnBean (types: org.springframework.cache.Cache; SearchStrategy: all) did not find any beans"
    }
    ],
    matched: [
    {
    condition: "CacheCondition",
    message: "Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type"
    }
    ]
    }
  • RedisCacheConfiguration:同様に@ConditionalOnBean(RedisTemplate.class)の条件を満たさないため、登録は実行する.証明:クエリー/autoconfig、GenericCacheConfigurationの結果は次のとおりです:
    RedisCacheConfiguration: {
    notMatched: [
    {
    condition: "OnBeanCondition",
    message: "@ConditionalOnBean (types: org.springframework.data.redis.core.RedisTemplate; SearchStrategy: all) did not find any beans"
    }
    ],
    matched: [
    {
    condition: "CacheCondition",
    message: "Cache org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration automatic cache type"
    }
    ]
    }
  • SimpleCacheConfiguration->が@ConditionalOnMissingBean(CacheManager.class)条件を満たしていない(EhCacheCacheConfigurationで構成されているため)、証明:
    {
    notMatched: [
    {
    condition: "OnBeanCondition",
    message: "@ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) found bean 'cacheManager'"
    }
    ],
    matched: [
    {
    condition: "CacheCondition",
    message: "Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type"
    }
    ]
    }
  • NoOpCacheConfiguration->が@ConditionalOnMissingBean(CacheManager.class)条件を満たしていない(EhCacheCacheConfigurationで構成されているため)、証明:
    NoOpCacheConfiguration: {
    notMatched: [
    {
    condition: "OnBeanCondition",
    message: "@ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) found bean 'cacheManager'"
    }
    ],
    matched: [
    {
    condition: "CacheCondition",
    message: "Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration automatic cache type"
    }
    ]
  • したがって、最終的に有効になったのはEhCacheCacheManager です.
  • 本題に戻ります.EhCacheCacheConfigurationには、
    @Configuration
    @ConditionalOnClass({ Cache.class, EhCacheCacheManager.class })
    @ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
    @Conditional({ CacheCondition.class,
            EhCacheCacheConfiguration.ConfigAvailableCondition.class })
    という注釈があります.
  • @Configuration->構成クラス
  • @ConditionalOnClass({Cache.class,EhCacheCacheManager.class})->現在のクラスパスの下にCacheが存在する.class, EhCacheCacheManager.class時有効
  • @C o n d i tionalOnMissingBean(org.springframework.cache.CacheManager.class)->beanFactoryにorgは存在しません.springframework.cache.CacheManagerタイプのbeanで有効になる
  • @Conditional({ CacheCondition.class, EhCacheCacheConfiguration.ConfigAvailableCondition.class })–>
  • CacheCondition->デフォルトはtrueを返し、前の記事で
  • を分析しました.
  • EhCacheCacheConfiguration.ConfigAvailableCondition->springが構成されている場合.cache.ehcache.configはマッチングを返すか、クラスパスの下にehcacheが存在する場合を返す.xmlは一致を返す.クラスパスの下でehcacheを作成したからです.xmlであるため、一致を返す.


  • このクラスには、@Beanメソッドが2つ宣言されています.
  • ehCacheCacheManager、コード:
    @Bean
    @ConditionalOnMissingBean
    public CacheManager ehCacheCacheManager() {
        Resource location = this.cacheProperties
                .resolveConfigLocation(this.cacheProperties.getEhcache().getConfig());
        if (location != null) {
            return EhCacheManagerUtils.buildCacheManager(location);
        }
        return EhCacheManagerUtils.buildCacheManager();
    }
  • @Bean->ehCacheCacheManagerとしてID 1個を登録し、CacheManagerタイプのbean
  • @ConditionalOnMissingBean->BeanFactoryにCacheManagerタイプのbeanが存在しない場合、
  • が有効になります.
    このメソッドの論理は次のとおりです.
  • springを取得する.cache.ehcache.config構成のパス
  • 構成されている場合は、指定するパスをロードしてCacheManagerを作成します.そうでない場合は、デフォルトのEhCacheのファイルパス:/ehcacheを読み込みます.xml,/ehcache-failsafe.xml

  • 私たちはspringを構成していません.cache.ehcache.configであるためehcacheが読み出す.xml
  • cacheManager、コード:
    @Bean
    public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {
        return this.customizers.customize(new EhCacheCacheManager(ehCacheCacheManager));
    }
  • @Bean->登録ID 1個をcacheManager、タイプEhCacheCacheManagerのbean
  • メソッドの論理は次のとおりです.
  • インスタンス化EhCacheCacheManager
  • CacheManagerCustomizersに従ってパーソナライズされた構成を行う.Spring bootはCacheManagerCustomizerを組み立てるので、この方法は空の操作に相当する.