自定义一个springboot starter

为什么想学

在使用了很多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

猜你喜欢

转载自www.cnblogs.com/sunleaf/p/10971702.html