Configuration is one of the key cores of the spring framework, so the spring application will certainly need to be configured to run normally, but the use of spring Boot after a lot of configuration is not done, then auto-configuration exactly how it happened? What is the order and procedure of the occurrence? How did that come about?
When using spring boot in an app, spring-boot-autoconfigure this jar package is introduced, and the jar contains some configuration classes. All of this package is used in the Claspath of the application, it is possible to use in the process of automatic configuration execution, so as to simplify the configuration work of the application. The contents of this jar are roughly the following:
The basic point of the arrows is useful in readinglist applications, which are used by annotations to load and configure beans at run time through a series of conditional judgments. Once these configurations are complete, an available app runtime environment is ready. The basis for implementing these configuration classes is the condition interface provided by spring 4.0, which is dynamically judged by implementing the matches method of the condition interface. Let's take a look at how the condition interface is used.
1. Spring 4.0 starts with the Condition interface.
Let me give you an example. It is necessary to create an Dbservice class in the application that operates the database, which uses an instance of JdbcTemplate. Then instantiate JdbcTemplate before, first to ensure that jdbctemplate in the application of classpath, then how to judge Classpath is there JdbcTemplate? How does this judgment be achieved with the condition interface?
Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classJdbctemplateconditionImplementsCondition {@Override Public Booleanmatches (conditioncontext context, Annotatedtypemetadata metadata) {Try{Context.getclassloader (). LoadClass ("Org.springframework.jdbc.core.JdbcTemplate"); return true; } Catch(Exception e) {return false; } }}
How do you use this judging condition when the judgment is fulfilled?
@Conditional (jdbctemplatecondition. Class) public dbservice Dbservice () { @AutoWire jdbctemplate jdbctemplate; // }
Dbservice before instantiating the first to determine the jdbctemplatecondition, return true to continue to instantiate, or the current class will be ignored, not instantiated.
2, Spring Boot implementation of the conditional** form of annotations.
Spring Boot implements a series of conditional Annotation. These are basically to be known. The following is a section of the common conditional annotations (Conditional Annotation).
Do not go to see all the conditions of the specific implementation of the annotations, but in order to be able to clarify the auto-configuration of the work of the process, the specific implementation of datasourceautoconfiguration write, deepen the understanding of automatic configuration, You can also learn to use @Conditional annotations and interface condition.
@Configuration @conditionalonclass ({DataSource. class, Embeddeddatabasetype. class }) @EnableConfigurationProperties ({datasourceproperties. class }) @Import ({registrar. class, Datasourcepoolmetadataprovidersconfiguration. class })publicclass datasourceautoconfiguration { ...}
1, the first note @Configuration this and the XML configuration bean is the same effect, this is not explained in detail.
2. The second note @ConditionalOnClass meaning: Returns True if the Classpath has a corresponding class on it, otherwise returns false.
3. The third annotation @EnableConfigurationProperties ({Datasourceproperties.class}) allows you to configure DataSource parameters using the Application.properties file.
4. The fourth annotation @Import introduce and initialize the classes in its arguments to a bean.
The real concern is the second annotation conditionalonclass, which determines whether the DataSource and Embededdatabasetype two classes are on classpath. If this condition is determined to be true, the subsequent decision is initiated. Then determine whether there are currently datasource instances, whether it is embedded data, whether there is embedded Tomcat and so on. Some columns run-time to determine the final decision whether to build a data source that is available for configuration.
3. The process of auto-configuration work in Readinglist project.
(1) Because H2 is on classpath, an embedded H2 database is instantiated, and the bean is of the javax.sql.DataSource type, so a JPA interface needs to be instantiated.
(2) because Hibernate Entity Manager is on classpath, it requires auto-configuration to instantiate Localcontainermanagerfactory if Hibernate is enabled. Beans and Jpavendoradapter.
(3) Because JPA is on classpath, repository instances are also created automatically.
(4) Because Spring MVC is on Classpath, Spring's dispatcherservlet is instantiated.
(5) because it is a Web application, the handler of a resource is registered to handle static resources under the resource directory.
(6) Because Tomcat is on the classpath, a built-in Tomcat container is started to host and serve.
4, actually do not know and do not affect the use of spring Boot to achieve business.
This one is not written.
Take a condition Example:
Importorg.springframework.boot.context.properties.ConfigurationProperties;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata;
//If you see Env.test=1 in the Applicaton.properties file, then the corresponding class will be loaded @configurationproperties ("Env.test") Public classTestenableconditionImplementsCondition { Public Booleanmatches (Conditioncontext Conditioncontext, Annotatedtypemetadata annotatedtypemetadata) {String test= Conditioncontext.getenvironment (). GetProperty ("Env.test"); intistest; Try{istest=integer.valueof (test); } Catch(Exception e) {istest= 0; } returnIstest==1; }}
@Conditional (testenablecondition. Class= "/{param}")publicclass TestController { ...}
Spring Boot. 3--How is Spring Boot auto_configuration implemented?