Spring-boot-starterの原理と実現方法


目次
spring-boot-starter
げんり
インプリメンテーション
テスト
ソースコード
spring-boot-starter
Spring-bootは多くの煩雑な構成を省略することができ、その多くのstarterは功を奏していると言える.例えばspring-bootにredisを統合するにはpomだけが必要である.xmlにspring-boot-starter-data-redisを導入し、プロファイルにspringを加える.redis.Databaseなどいくつかの重要な構成項目でよいが、一般的なstarterにはspring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-jdbcがあり、従来のxml構成に比べて統合の作業量を大幅に削減していると言える.
げんり
starterによる自動化構成には、maven依存、プロファイルの2つの条件が必要です.ここでは、starterによる自動化構成のプロセスについて簡単に説明します.導入mavenは実質的にjarパッケージを導入するもので、spring-bootが起動するとstarter jarパッケージのresources/META-INF/springが見つかる.factoriesファイル、spring.factoriesファイルの構成で、自動構成が必要なクラスを見つけます.次のようにします.
@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnBean({DataSource.class})
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class MybatisAutoConfiguration {
//....
}

これはmybatis-spring-boot-autoconfigureの自動構成クラスです.注釈を簡単に説明します.
注記説明@Configurationはプロファイルであることを示します.注記されたクラスはbean構成クラス@ConditionalOnClass classクラスがclasspathで発見された場合に自動構成@ConditionalOnBeanクラスが発見された場合に自動構成@EnableConfigurationProperties@ConfigurationProperties注記を有効にする@AutoConfigureAfterが自動構成を完了した後にインスタンス化されます個のbean
インプリメンテーション
pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.ouyanglol
    starter-demo
    0.0.1-SNAPSHOT
    starter-demo
    spring-boot-starter demo

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-source-plugin
                
                    
                        package
                        
                            jar-no-fork
                        
                    
                
            
        
    


Spring-boot-starterはもちろん、spring-boot-configuration-processorの役割はコンパイル時にspring-configuration-metadataを生成することである.json、IDEでプロファイルを編集するとプロンプトが表示されます.パッケージはjar-no-forkを選択します.main関数は必要ないからです.
EnableDemoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface EnableDemoConfiguration {
}

これは必須ではありません.関連するプロパティを自動的に構成するエントリとして、このようなコメントが推奨されます.
DemoProperties
@Data
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String name;
    private Integer age;
}

nameとageはアプリケーションに対応する.propertiesの中のdemo.名前とdemo.age
DemoAutoConfiguration
@Configuration
@ConditionalOnBean(annotation = EnableDemoConfiguration.class)
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    DemoService demoService (){
        return new DemoService();
    }

}

ここでは自動構成の関連条件と関連操作を設定し、ここでは最も簡単なdemoを書きたいだけなので、ここでは簡単にbeanを注入するだけで、複雑な論理はなく、実際の開発では、このクラスが最も重要です.
DemoService
public class DemoService {

    @Autowired
    private DemoProperties demoProperties;

    public void print() {
        System.out.println(demoProperties.getName());
        System.out.println(demoProperties.getAge());
    }
}

ここに@Serviceは必要ありません.DemoAutoConfigurationでspringコンテナに注入されているからです.
spring.factoriesはresources/META-INF/でspringを作成します.factoriesファイル:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ouyanglol.starterdemo.config.DemoAutoConfiguration

spring-bootに、起動時にスキャンするクラスを教えます.
テスト
pom.xmlローカルmvn installの後、新しいspring-bootプロジェクトに導入
        
            com.ouyanglol
            starter-demo
            0.0.1-SNAPSHOT
        

プロファイル
demo.name = ooo
demo.age = 11

IDEAを使用している場合、編集時にプロンプトが表示されます.テスト
@SpringBootApplication
@EnableDemoConfiguration
public class Demo1Application {

    @Autowired
    private DemoService demoService;

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @PostConstruct
    public void test() {
        demoService.print();
    }

}

main関数を起動すると、コンソールからプロファイルのnameとageが印刷され、簡単なspring-boot-starterが書き終わります.
ソースコード
コードクラウド:https://gitee.com/mianshiti_question/spring-boot-starter-demo.git————————————————著作権声明:本文はCSDNブロガー「Mr_OOO」のオリジナル文章で、CC 4.0 BY-SA著作権協定に従い、転載は原文の出典リンクと本声明を添付してください.テキストリンク:https://blog.csdn.net/Mr_OOO/article/details/89477948