為什么想學
在使用了很多springboot的starter之后,感覺這種形式很好用,如果我把自己平時使用的一些工具用starter的形式寫,以后在需要類似的工具時只需要直接拿來用或者做一些簡單的修改就可以了。
創建一個starter
開發工具使用eclipse,安裝sts插件。以我使用ueditor為例,創建一個叫ueditor-spring-boot-starter的spring boot maven項目。
定義starter需要的變量
我們使用springboot時,一般都會使用yml文件或者properties文件定義變量,如果我的springboot項目需要一些配置,也要在yml文件中定義,需要一些代碼才能實現在eclipse中的自定提醒。
@ConfigurationProperties(prefix="ueditor") public class ConfigOnProperties { private String rootPath; private String configPath; private String[] staticLocations; public String getRootPath() { return rootPath; } public void setRootPath(String rootPath) { File file = new File(rootPath); if(file.exists() && file.isDirectory()) this.rootPath = rootPath; else { String classPath = this.getClass().getResource("/").toString(); if(staticLocations != null) { for (String staticLocation : staticLocations) { File configFile = new File(staticLocation + "/" + configPath); if(configFile.exists()) { classPath = staticLocation + "/"; } } } if(classPath != null) { this.rootPath = classPath.replace("file:\\", "") .replace("file:/", "") + rootPath; }else { this.rootPath = rootPath; } } } public String getConfigPath() { return configPath; } public void setConfigPath(String configPath) { this.configPath = configPath; } public String[] getStaticLocations() { return staticLocations; } public void setStaticLocations(String[] staticLocations) { this.staticLocations = staticLocations; if(this.rootPath != null ) { setRootPath(this.rootPath); } } }
在上面的例子中,我定義了3個變量配置,分別是ueditor.rootPath、ueditor.configPath和ueditor.staticLocations。使用到了一個注解:@ConfigurationProperties(prefix="ueditor"),與后面的@EnableConfigurationProperties配合實現定義變量。
定義starter的默認bean配置
為了方便在后面的使用中自定義ueditor的各種操作,我定義了一個service接口來執行ueditor的各種操作,同時為這個接口寫了一個默認的實現。如果使用spring的@Service注解,在后面的使用中會出現一個關於重復bean的報錯,這里使用configuration文件,來定義缺失bean:
@Configuration @EnableConfigurationProperties(value=ConfigOnProperties.class) @ComponentScan(basePackages="xxxx.ctrl") public class UeditorAutoConfiguration { @Bean @ConditionalOnMissingBean(IUeditorService.class) public IUeditorService uEditorService() { return new DefaultUeditorService(); } }
@ConditionalOnMissingBean這個注解,定義了在缺失指定類型的bean時,使用這個bean來代替。
使spring掃描到這些配置
springboot默認的掃描bean范圍是@SpringBootApplication注解的類所在的package,也可以通過@ComponentScan來定義掃描范圍,但是一個starter如果需要這樣的定義才能使用也太不智能了,百度了一下,可以在starter的resources下,創建一個META-INF文件夾,然后創建文件spring.factories文件,里面配置掃描包。
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ xxxx.config.UeditorAutoConfiguration