Preface
Reader: Have a certain understanding of spring, Spring boot
Difficulty: Junior level, designed to be applied quickly to projects
References: Web, books, official documents, any errors welcome you to take a message.
actual combat simulation Source: Https://github.com/yhqnh/demo-springcloud , using Spring-cloud Dalston version
1. Spring Boot1.1. Dependent Relationships
Spring Cloud strong reliance on spring boot
Spring Boot strong reliance on spring
Weak weak say a strong dependence is not it does not work, it is necessary to spring or spring boot some features for a brief introduction
1.2. meta Annotations and combined annotations
Meta-annotations: Annotations can be annotated on other annotations and annotated annotations are called combined annotations.
For example, @springbootapplication is a combination of annotations, which group and @springbootconfiguration
@EnableAutoConfiguration and so on.
1.3. Automatic Configuration
Spring Boot The beans are automatically configured for the classes in the jar package based on the jar packages and classes in the classpath, which greatly reduces the configuration we want to use. Spring boot via Spring4. X provides the conditional annotation condition to implement. Spring4.x advocates the use of a combination of Java configuration and annotation configurations that does not require any XML configuration to implement all spring configurations. You can also use @importresource ({"Application1.xml", "Application2.xml"}) to support XML configuration.
Turn off a specific automatic configuration using @springbootconfiguration's Exclude property, such as Exclude = {Datasourceautoconfiguration.class} to turn off automatic configuration of data sources
@condition create a specific bean based on satisfying a particular condition, such as automatically configuring one or more beans when a jar package exists in the classpath.
@ConditionalOnBean: when the container has the specified bean in the condition
@ConditionalOnClass: when the class path has the specified class under the condition
@ConditionalOnMissingBean: when no bean is specified in the container
@ConditionalOnResource: whether the classpath has a specified value
For example Rabbitautoconfiguration when discovering a bean with no connectionfactory type under the Classpath, create a
Let's take a look at the source code of enableautoconfiguration annotations
Here the most critical @import feature is imported into the configuration, Enableautoconfigurationimportselector uses the Springfactoriesloader.loadfactories method to scan with meta-inf/ The jar package for the Spring.factories file, and the Spring-boot-autoconfiguration jar contains a spring.fatories file containing all the automatic configuration.
1.4. Multi-environment configuration
In spring boot, the multi-environment configuration file name needs to meet the application-{profile}.properties format, where {profile} corresponds to your environment identity. {profile} is set by the Spring.profiles.active=test property to load the application-test.properties configuration file.
1.5. Load order
Spring Boot Loads the order of the attributes, the smaller the value the higher the priority
1) parameters in the command line
2) properties in the Spring_application_json. Spring_application_json is configured in the system environment variable in JSON format
3) JNDI Properties in java:comp/env
4) Java System Properties, content that can be obtained through System.getproperties ()
5) Environment variables of the operating system
6) random attributes via random.* configuration
7) In the current application Jar package, for different {profile} environment configuration file contents, for example application-{ Profile}.properties yaml defined profile
8) In the current application Jar within the package, for different {profile} environment configuration file contents, for example application-{ Profile}.properties yaml defined profile
9) application.properties and YAML configuration content outside of the current app jar package
application.properties and YAML configuration content within the current app jar package
One) @Configuration annotations in the modified class, by @PropertySource annotations defined by the properties
Apply default properties, use springapplication.setdefaultproperties defined content
For exampleSpring Cloud Configfromgitand other external acquisition configurations to replaceJarThe local configuration file within the package so that even the same name as the local debug profile for the otherDemo,Staging,prodnor any effect, because7priority is higher than8or,9priority is higher thanTen
1.6. Actuatorwith the native endpoint
Spring-boot-starter-actuator the implementation of the module can effectively eliminate or reduce the amount of development of the monitoring system when collecting the application indicators. It provides some raw endpoints.
Dry Goods sharing microservices spring-cloud (1. Preliminary discussion)