Customizing a springboot starter

Why do you want to learn

After using a lot springboot the starter, feeling good with this form, if I put some of the tools used to write their usual form of starter of the future when you need similar tools need only be directly used or do some simple modify it.

Creating a starter

Development tools eclipse, plugin installation sts. In my use ueditor, for example, create a project called spring boot maven ueditor-spring-boot-starter's.

Defined variables starter needed

When we use springboot, usually using yml file or properties file defines variables, if my springboot project requires some configuration, but also defined in yml file, you need some code to achieve the eclipse custom alerts.

@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);
        }
    }
}

In the example above, I define three variables configuration, namely ueditor.rootPath, ueditor.configPath and ueditor.staticLocations. To use a comment: @ConfigurationProperties (prefix = "ueditor"), and with the realization of the latter @EnableConfigurationProperties define variables.

The default configuration bean definition starter

In order to facilitate a variety of custom ueditor operate in a later use, I define a service interface to execute various operations ueditor, but for this interface has written a default implementation. If you are using spring of @Service notes, there will be an error on a bean repeated in later use, where the use of configuration files to define the missing bean:

@Configuration
@EnableConfigurationProperties(value=ConfigOnProperties.class)
@ComponentScan(basePackages="xxxx.ctrl")
public class UeditorAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(IUeditorService.class)
    public IUeditorService uEditorService() {
        
        return new DefaultUeditorService();
    }
}

@ConditionalOnMissingBean this annotation is defined when the absence of the specified type in the bean, the bean used instead.

Scan to these configurations make the spring

springboot default scan range of package @SpringBootApplication bean annotated classes where, @ can also be defined scan range ComponentScan, but a starter if you need to use such a definition is not too smart, and Baidu a bit, can be a starter under resources, create a META-INF folder, and then create a file spring.factories file, which configure the scan package.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxxx.config.UeditorAutoConfiguration

 

Guess you like

Origin www.cnblogs.com/sunleaf/p/10971702.html