Spring-bootカスタムstarter


カスタムスターターSpringBootを学習する過程で、redisを統合してもRabbitMQを統合しても、前の統合mybatisはすでに多くのスターターを学習して、これらのスターターはspringbootが私たちに提供したいくつかのパッケージで、これらのスターターは非常に便利で速く機能を増加することができて、多くの配置を必要としなくてもapplicationにあります.propertiesは少し配置すればいいです.では次に自分のスターを作る方法を学びます
redis-starterプラグインの前にspring-boot-starter-data-redisが使用されています.このstarterはredisを統合するために使用されています.では、次にstarterを完成します.このstarterもredisを統合します.
Web機能を必要としないプロジェクトを新規作成
pomファイルの内容は以下の通りです.


    4.0.0

    enjoy
    redis-starter
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
    
    
        UTF-8
        1.8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            redis.clients
            jedis
            3.0.1
        
    


RedisPropertiesを作成し、Redisをロードするために必要な構成を作成します.ここでは簡単にするためにパスワードは設定されていません.
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
    private String host;
    private int port;

    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
}

構成をロードし、Jedisクライアントをインスタンス化する構成クラスを作成します.
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

@Configuration //    
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(RedisProperties.class) //          
@ConditionalOnProperty//                
 (
 prefix = "redis",//      redis
 value = "enabled",//  
 matchIfMissing = true//    
 )
public class RedisAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public Jedis jedis(RedisProperties redisProperties){
        return new Jedis(redisProperties.getHost(), redisProperties.getPort());
    }
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------@E n a b l e ConfigurationPropertiesを使用しています.これは構成パラメータの使用を開始する注釈です.value値は、構成エンティティパラメータマッピングのClassTypeを構成し、構成エンティティを構成ソースとします.SpringBoot内蔵条件注記@ConditionalOnXxxに関する注記ここではシステム的に言えば、これは私たちの構成の鍵であるため、名前によって私たちはXxx条件を持っていると理解することができます.もちろん、実際の意味もそうです.条件注記はシリーズです.@ConditionalOnBean:SpringIocコンテナ内に指定Beanが存在する場合@ConditionalOnClass:SpringIocコンテナ内に指定Classが存在する場合@ConditionalOnExpression:判断条件としてSpEL式に基づく@ConditionalOnJava:判断条件としてJVMバージョンに基づく@ConditionalOnMissingBean:SpringIocコンテナ内に存在しない場合Beanを指定する条件@ConditionalOnMissingClass:SpringIocコンテナ内に指定するClassが存在しない場合@ConditionalOnNotWebApplication:現在のプロジェクトがWebプロジェクトではない条件@ConditionalOnProperty:指定した属性に指定した値があるかどうか@ConditionalOnResource:クラスパスに指定した値があるかどうか@ConditionalOnSingleCandidate:指定したBeanがSpringIocで許容される場合器内に1つしかないか、複数あるが優先するBean@ConditionalOnWebApplicationを指定:現在のプロジェクトがWebプロジェクトである条件以上の注釈はメタ注釈@Conditionalから進化したものであり、使用しない条件に応じて、以上の具体的な条件注記を作成します./---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------これまで自動化された構成starterは完了していません.SpringBootの動作原理を理解してから、後続の符号化を完了する必要があります.Starter自動化動作原理自動化構成を完了するために、注釈@SpringBootApplicationに自動化構成を開く注釈@EnableAutoConfigurationがあります.注釈ソースコードは以下の通りです.
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    Class>[] exclude() default {};
    String[] excludeName() default {};
}

@EntableAutoConfiguration注記では@import注記を使用してインポート構成を完了しますが、E n a b l e AutoConfigurationImportSelector内部ではSpringFactoriesLoaderを使用しています.loadFactoryNameメソッドによるスキャンはMETA-INF/springを有する.factoriesファイルのjarパッケージ.まずspring-boot-autoconfigureパッケージ内のspringを見てみましょう.factoriesファイルの内容は、以下の通りです.
構成の構造形式はKey=>Value形式であることがわかるが、複数のValueを使用する場合には分離され、カスタムstarter内でもこの形式で完了することができる.自動化構成を完了するために、ここでKeyはorgを使用する必要がある.springframework.boot.autoconfigure.EnableAutoConfiguration
カスタムspring.factories src/main/resourceディレクトリの下にMETA-INFディレクトリを作成し、ディレクトリにファイルspringを追加します.factories、具体的な内容は以下の通りです.
#カスタムスターターの自動化構成orgを構成する.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.enjoy.redis.RedisAutoConfiguration
これまでカスタムスターが開発済み
3.4.2. 新規プロジェクトテストstartserは、前に作成したredis-starterをテストするための新しいプロジェクトを作成します.
Pomファイルの構成は次のとおりです.


    4.0.0

    enjoy
    testRedisStarter
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
    
    
        UTF-8
        1.8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            enjoy
            redis-starter
            1.0-SNAPSHOT
        
    


新しいspringboot起動クラス
package cn.enjoy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

新しいアプリケーションpropertiesはredis接続に関する情報を構成します
redis.port=6379
redis.host=127.0.0.1

これらの準備ができたらredisを起動し、新しいテストクラスを確立し、テスト方法を実行します.
package cn.enjoy.test;
import cn.enjoy.App;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class RedisTest {
    @Resource
    private Jedis jedis;

    @Test
    public  void test() {
        jedis.set("enjoy","enjoy");
        String enjoy = jedis.get("enjoy");
        System.out.println(enjoy);
    }
}

ここまで来たらredis-staterをカスタマイズして