Spring Boot achieve automatic assembly

       Before writing a TLV codec library on Github unexpected harvest some of the stars, so take advantage of recent idle, simply to achieve a little comment assembly automatic assembly Spring Boot's and Spring. About Spring Boot automatic assembly-line information is still quite a few, simple steps are as follows:

1. POM introduction of automatic assembly of dependencies, provided <optional> is true, i.e., not transmitted dependence;

    <dependencies>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2. To achieve XXXAutoConfiguration class, and @Configuration indicia on the label;

3. If there is a corresponding configuration attributes, the configuration may be incorporated in the Configuration @EnableConfigurationProperties by class, and set the configuration prefix required by @ConfigurationProperties class configuration (in IDEA, the default value for the property and comments are entered in the application.yml when prompted):

@Configuration
@EnableConfigurationProperties(DreamTLVProperties.class)
public class DreamTLVAutoConfiguration {

    private DreamTLVProperties properties;

    public DreamTLVAutoConfiguration(DreamTLVProperties properties) {
        this.properties = properties;
    }

    @ConditionalOnMissingBean
    @Bean
    public TLVContext tlvContext() throws TLVInitException {
        return new TLVContext(properties.getBeanPackages());
    }

    @ConditionalOnMissingBean({TLVCodec.class, IHeaderCodec.class})
    @Bean("tlvCodec")
    public TLVCodec defaultTLVCodec() throws TLVInitException {
        return new TLVCodec(tlvContext());
    }

    @ConditionalOnBean(IHeaderCodec.class)
    @Bean("tlvCodec")
    public RawTLVCodec<?> customTLVCodec(IHeaderCodec<?> headerCodec) throws TLVInitException {
        return new RawTLVCodec<>(tlvContext(), headerCodec);
    }

}
@ConfigurationProperties(prefix = "dream.tlv")
public class DreamTLVProperties {

    /**
     * TLV bean packages for scanning
     */
    private String[] beanPackages;

    /**
     * @return TLV bean packages for scanning
     */
    public String[] getBeanPackages() {
        return beanPackages;
    }

    /**
     * Configure bean packages for TLVContext initialization
     *
     * @param beanPackages TLV bean packages for scanning to set
     */
    public void setBeanPackages(String[] beanPackages) {
        this.beanPackages = beanPackages;
    }
}

4. Create the resources / META_INF spring.factories file and configure the corresponding class autoconfiguration (s separated by commas, using newline "\"):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dream.codec.tlv.DreamTLVAutoConfiguration

As is the simple overall procedure is very simple. But there are many points of attention, first summarized as follows:

1. The need for flexibility in the use @ConditionalXXX a related note, injected or not injected to select relevant objects

2. sometimes necessary to define the sequence of the automatic assembly, at this time needs to be adjusted by means of @AutoConfigurationBefore or @AutoConfigurationAfter assembly sequences, e.g. SpringBoot want to replace the Executor carrying asynchronous thread pool, this time by direct injection @Bean find does not take effect taskExecutor , mainly due to the spring-boot-autoconfiguration in TaskExecutionAutoConfiguration will first execute (the normal order of execution for all Bean at first scan SpringBootApplication project, and then run the spring-boot ecological relevant AutoConfiguration, after each third party is the automatic assembly class).

@EnableAsync
@AutoConfigureBefore(TaskExecutionAutoConfiguration.class)
@Configuration
@EnableConfigurationProperties(DreamFrameworkProperties.class)
public class DreamFrameworkAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(name = "taskExecutor")
    public Executor taskExecutor() {
        ...
    }
}

3. Try not to use @ComponentScan labels on AutoConfiguration, although this is more personal feeling provincial thing, but still need to specify what type injection can be all-inclusive, @ ComponentScan about scanning by @Bean, and third-party libraries always felt a little excessive automated.

4. Following the above problems, without using @ComponentScan, for the Bean or like @Controller @Repository special features of how injection. In fact, you do not want to be too complex, or marked @Controller or @Repository label on the corresponding class, and by the way @Bean in the AutoConfiguration can be injected.

      Personal also implemented a Dream-the Spring-Framework , there are a lot of automatic assembly automation framework to do some default work. Compared to the traditional Spring comment @Import way to achieve a certain degree of automatic assembly, Spring Boot automatic configuration easier and more convenient to implement. (About @Import can also refer to my TLV codec tlv-spring project realization)

Published 42 original articles · won praise 9 · views 60000 +

Guess you like

Origin blog.csdn.net/jjxojm/article/details/104399918