SpringBoot Starter緣起

SpringBoot通過SpringBoot Starter零配置自動加載第三方模塊,只需要引入模塊的jar包不需要任何配置就可以啟用模塊,遵循約定大于配置的思想 。那么如何編寫一個SpringBoot Starter呢?我們需要考慮如下幾個問題:

  1. 如何讓SpringBoot發現我們編寫的模塊?
  2. 如何讓模塊讀取SpringBoot的配置文件?
  3. 如果用戶沒有在配置文件中配置必要的配置項,如何默認禁用模塊?
  4. 如何讓SpringBoot知道模塊有哪些配置項目,方便用戶配置配置文件?
一.模塊的發現由于SpringBoot默認的包掃描路徑是主程序所在包及其下面的所有子包里面的組件,引入的jar包下的類是不會被掃描的 。那么如何去加載Starter的配置類呢?SpringBoot約定會掃描Starter jar包META-INF目錄下的spring.factories文件,只需要在spring.factories文件里配置要加載的類就可以了 。下面的是Arthas的配置文件(arthas-spring-boot-starter-3.6.3.jar,Arthas是Alibaba開源的Java診斷工具 。) 。org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.alibaba.arthas.spring.ArthasConfiguration,\com.alibaba.arthas.spring.endpoints.ArthasEndPointAutoConfiguration
這樣SpringBoot在啟動的時候就會自動加載這些類存放到容器中管理 。
二.模塊讀取SpringBoot的配置文件ArthasConfiguration的源碼:
@ConditionalOnProperty(name = {"spring.arthas.enabled"},matchIfMissing = true)@EnableConfigurationProperties({ArthasProperties.class})public class ArthasConfiguration {@ConfigurationProperties(prefix = "arthas")@ConditionalOnMissingBean(name = {"arthasConfigMap"})@Beanpublic HashMap<String, String> arthasConfigMap() {return new HashMap();}@ConditionalOnMissingBean@Beanpublic ArthasAgent arthasAgent(@Autowired @Qualifier("arthasConfigMap") Map<String, String> arthasConfigMap, @Autowired ArthasProperties arthasProperties) throws Throwable {arthasConfigMap = StringUtils.removeDashKey(arthasConfigMap);ArthasProperties.updateArthasConfigMapDefaultValue(arthasConfigMap);String appName = this.environment.getProperty("spring.application.name");if (arthasConfigMap.get("appName") == null && appName != null) {arthasConfigMap.put("appName", appName);}Map<String, String> mapWithPrefix = new HashMap(arthasConfigMap.size());Iterator var5 = arthasConfigMap.entrySet().iterator();while(var5.hasNext()) {Map.Entry<String, String> entry = (Map.Entry)var5.next();mapWithPrefix.put("arthas." + (String)entry.getKey(), entry.getValue());}ArthasAgent arthasAgent = new ArthasAgent(mapWithPrefix, arthasProperties.getHome(), arthasProperties.isSlientInit(), (Instrumentation)null);arthasAgent.init();logger.info("Arthas agent start success.");return arthasAgent;}}我們發現類上面兩個注解EnableConfigurationProperties和ConditionalOnProperty 。這兩個注解的作用如下:
  • EnableConfigurationProperties用來指定要加載的配置類 , 配置類用來加載SpringBoot的配置文件,SpringBoot配置文件中可以指定Arthas的啟動參數 。如果你不需要任何參數,則可以不指定EnableConfigurationProperties 。
@ConfigurationProperties(prefix = "arthas")public class ArthasProperties {private String ip;private int telnetPort;private int httpPort;private String tunnelServer;private String agentId;private String appName;private String statUrl;}
  • ConditionalOnProperty通過讀取SpringBoot配置文件的指定參數判斷是否啟用組件,如果判斷為False,ArthasConfiguration里的Bean就不會被加載到容器中,即組件的開關 。Arthas讀取spring.arthas.enabled來判斷是否加載組件 。如果你想默認啟動 , 沒有開關,則可以不指定ConditionalOnProperty 。
三.編寫自己的Starter我們寫個簡單的Starter , 不加載配置文件,并且默認啟動的Starter 。IDE用的是是IDEA社區版 。
3.1創建一個SpringBootStarter項目3.1.1 新建項目 , 選擇使用Maven構建 。
SpringBoot Starter緣起

文章插圖
3.1.2 然后創建spring.factories文件和配置類 。
SpringBoot Starter緣起

文章插圖
3.1.3 spring.factories寫入配置類的全稱 。org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bjgoodwil.SpringBootStarterConfiguration
3.1.4 Maven打包使用Maven的site命令打包jar到自己的本地倉庫中 。
3.2在SpringBoot項目中引入jar打開一個SpringBoot項目,在pom文件中引入Jar(直接拷貝Starter pom文件中的參數),啟動SpringBoot項目,控制臺就會打印配置類的打印語句 。
<!--自定義Starter--><dependency><groupId>com.bjgoodwill</groupId><artifactId>springbootstarter</artifactId><version>1.0-SNAPSHOT</version></dependency>

推薦閱讀