八、カスタムstarter――尚シリコンバレーSpring Bootノート

4797 ワード

1、このシーンで使うべき依存は何ですか?
2、自動配置の編集方法
@Configuration  //           
@ConditionalOnXXX  //                  
@AutoConfigureAfter  //          
@Bean  //        

@ConfigurationPropertie //    xxxProperties         
@EnableConfigurationProperties // xxxProperties        

//         
//              ,   META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
3、モード:
           ;

            ;

         ;          (starter)

mybatis-spring-boot-starter;       -spring-boot-starter
ステップ:
  • ランチャーモジュール
  • 
    
        4.0.0
    
        com.atguigu.starter
        atguigu-spring-boot-starter
        1.0-SNAPSHOT
    
        
        
    
            
            
                com.atguigu.starter
                atguigu-spring-boot-starter-autoconfigurer
                0.0.1-SNAPSHOT
            
        
    
    
    
  • 自動構成モジュール
  • 
    
       4.0.0
    
       com.atguigu.starter
       atguigu-spring-boot-starter-autoconfigurer
       0.0.1-SNAPSHOT
       jar
    
       atguigu-spring-boot-starter-autoconfigurer
       Demo project for Spring Boot
    
       
          org.springframework.boot
          spring-boot-starter-parent
          1.5.10.RELEASE
           
       
    
       
          UTF-8
          UTF-8
          1.8
       
    
       
    
          
          
             org.springframework.boot
             spring-boot-starter
          
    
       
    
    
    
    
    package com.atguigu.starter;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "atguigu.hello")
    public class HelloProperties {
    
        private String prefix;
        private String suffix;
    
        public String getPrefix() {
            return prefix;
        }
    
        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }
    
        public String getSuffix() {
            return suffix;
        }
    
        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }
    }
    
    package com.atguigu.starter;
    
    public class HelloService {
    
        HelloProperties helloProperties;
    
        public HelloProperties getHelloProperties() {
            return helloProperties;
        }
    
        public void setHelloProperties(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    
        public String sayHellAtguigu(String name){
            return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();
        }
    }
    
    package com.atguigu.starter;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ConditionalOnWebApplication //web     
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloServiceAutoConfiguration {
    
        @Autowired
        HelloProperties helloProperties;
        @Bean
        public HelloService helloService(){
            HelloService service = new HelloService();
            service.setHelloProperties(helloProperties);
            return service;
        }
    }
    
    もっと多くのSpring Boot整合例
    https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples