Automatic configuration of principle SpringBoot

Spring Boot operation is provided by the annotation @EnableAutoConfiguration
its key features is @Import comment.
        EnableAutoConfigurationImportSelector use SpringFactoriesLoader.loadFactoryNames method to scan the jar package has MEAT-INF / spring.factories file

before understand the principles we need to know the following few notes:
        @ConditionalOnBean: When the container has designated as Bean to true
        @ConditionalOnClass: When the class there at the path specified for the class to true
        @ConditionalOnMissingBean: when the container is not specified Bean to true
        @ConditionalOnProperty: is there a specified data value specified

after we know annotation can follow the process to understand the principle of automatic configuration of Spring Boot

1, custom Starter (before customizing Starter must first fill in the dependent Maven)

2, to complete a test class, defined in the class default attribute values, as such, only one attribute value Age, default is 18. @ConfigurationProperties annotation defines a match, if you want to modify an attribute value, can use the "matching attribute value = modified" in the application.properties modification

3, a complete service class service. That is the main function of the class, if not SpringBoot, these service classes in the Spring are required to configure their own generation.
        As SpringMVC in DispatcherServlet, Mybatis like the DataSource

4, a complete auto-configuration class. Automatic configuration class main role is to configure the core SpringBoot, it will write MEAT-INF / spring.factories, telling SpringBoot when you start to read the class and configure it according to the rules of the class.
        The annotation @EnableConfigurationProperties TestProperties class attributes injection opening, which allows to modify the property value in application.properties.
        @ConditionOnClass detects the presence of TestService class
        @ConditionOnProperty would like to see whether to open the automatic configuration. Enabled by default (true).
        @ConditionOnMissingBean will detect whether there is an object class TestService container, if not then generate a

5, the last step, do not forget to create files in spring.factories MEAT-INF folder. Very simple, tell SpringBoot to read TestServiceAutoConfiguration class org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ cn.TestServiceAutoConfiguration

More technical information may be concerned about: itheimaGZ get

Guess you like

Origin www.cnblogs.com/zhuxiaopijingjing/p/12297816.html