1. SpringBoot profile
SpringBoot is a framework to simplify Spring application development. It integrates Spring’s technology stack to provide a variety of standardized defaults. This allows us to quickly develop Spring projects without the hassle of XML configuration. Reduce the cost of Spring projects.
2. Advantages and disadvantages of SpringBoot
Make it easy to code, configure and deploy. The downside may be auto-injected beans, which are not particularly clear and may conflict.
3. How to fix the SpringBoot version
One is directly fixed when parenet.
A dependcy option is if the current project already has a parent.
4. Usage of SpringBoot
The parent project in Maven imports the Springboot initiator. Then write a main program annotated with SpringBootApplication. It can be started using the run method.
5. Principle of SpringBoot automatic configuration
The first thing you can see is that the bootstrap class has a SpringBootApplication annotation, and when you click on it, the EnableAutoConfiguration annotation. Then go in, found a @ Import annotations, Import it is EnableAutoConfigurationImportSelector class. Look at the class, he is integrated with an abstract AutoConfigutationimportSelector. And then we have the ImportSelector interface. The selectImport interface, if implemented, then loads all the returned arrays into the Spring container. The implementation of the main places is getCandidateConfigurations method. The internal implementation of that method is to go back and read the Spring.Factories in meta-INF. So classes that are primarily configured in there are automatically loaded.
6. SpringBoot configuration file injection
Injected into the entity class, the entity class can use the @ConfigurationProperties(prefix = “person”) annotation.
7. @Value and @ConfigurationProperties
8. @PropertySource
Loads the specified configuration file. The following, if not declared, will be loaded from the master configuration file.
9. @ImportResource
Import the Spring configuration file and make the content in the configuration file take effect. This way, for example, a class that is not registered in a container with the @Componet annotation cannot be found using autowired. Once the file is imported, it can be found.
There is no Spring configuration file in Spring Boot, and the configuration file written by us cannot be automatically recognized.
If you want Spring configuration files to work, load them in. @**ImportResource** annotated on a configuration class
10. Springboot’s profile loads
In the configuration file, we can create multiple. End with -. Through the master profile, active of Profiles can be configured to specify the load file.
In the same file, yML files support the document block notation, which is –.
11. Several ways for SpringBoot to activate a specified profile
First: Specify it directly in the configuration file via active
Second: specified by a java-jar parameter
The third method is based on VM parameters
12. Load sequence of internal configuration files of the SpringBoot project
The order is as follows;
The configuration with a higher priority overwrites the configuration with a lower priority.
SpringBoot loads the master configuration file from all four locations; ** Complementary configuration **;
We can also change the default configuration file location via spring.config.location
Once the project is packaged, we can specify the new location of the configuration file when starting the project using command-line arguments. Specified profiles and default loaded profiles work together to form complementary configurations;
13. SpringBoot external configuration file loading sequence
There are a lot of them. We’ll just have a few important ones.
The high priority overrides the low priority.
The parameters configured on the command line have the highest priority.
Configuration file with profile outside the JAR package.
Jar package with profile profile.
Configuration files without profiles outside the JAR package.
Configuration file without profile in jar package.
14. Relationship between Springboot logs
The default SpringBoot uses SLF4J +logback. Logging-starter can be used. Can automatically adapt other logs. Simply remove commons-logging from the log. Additional logs are automatically introduced.
LoggerFactory is used to obtain a Logger. Logging is done using logger.
Some configuration of logging
Logging.level.com.atguigu=trace specified print level
Logging. file=G:/springboot.log Specifies the log generation path
Logging. path=/spring/log Specifies the relative log generation path
Logging.pattern. console=%d{YYYY-MM-DD} [%thread] %-5level % Logger {50} – % MSG %n Specifies the console output format
Logging. The pattern. The file = % d – the dd} {yyyy – MM = = = [% thread] – 5 level = = = = = = % % logger {50} = = = = log output in the specified file format
15. How does SpringBoot extend the SpringMVC configuration
Append @Configuration to the class by inheriting the WebMvcConfigurerAdapter from the class. And then rewrite the method inside. All the WebMvcConfigurer elements in the container work together.
If we don’t want to use MVC auto-configuration, use all our own. You can add the @enableWebMVC annotation to the configuration class. This annotation can import a webmvcconfigurationsupport class. And then the MVC auto-configuration class annotation has this sentence, there’s a conditional annotation, and it only works when there’s no class above it.
How to register filter, servlet, listener with SpringBoot
Once you’ve written a servlet, declare a class that returns a ServletRegistrationBean. Wrap the servlet with the methods inside and return.
Write a filter. Package through FilterRegister.
Write a listener to implement the registration bean.
17. SpringBoot switches to Undertow
Just rule out Tomcat and then introduce Undertow.
18. SpringBoot tasks
Asynchronous tasks are enabled by adding the @enableAsync annotation to the startup class. Then, for asynchronous methods, annotate @async.
Scheduled tasks, on the launch class, annotate @enablesCheduling. Scheduled annotations are used for timed methods. It’s divided into seconds, minutes, hours, days, months, weeks.
The mail task
Importing the mail task initiator. Perform the configuration in the configuration file. Send via javamailSenderimpl. You can send simple messages and complex messages SimpleMailMessage and MimeMessage.
19. SpringBoot hot deployment
Jrebal is charged.
Generally, springboot-dev-tool is used. Press CTRL + F9 to perform hot deployment. Enable automatic compilation of IDEA.
20. Monitoring SpringBoot
Actuatro monitors are introduced. The configuration file needs to be configured for security shutdown. Management. Security. Enabled = false. You can set the shutdown enable function of the endpoint to enable the shutdown function.
21. SpringBoot integrates redis
Import the redis initiator. Data – redis. Then configure the host of redis in the configuration file. This can then be done with the redistemplate. Will be garbled, the default is to use the JDK serialization json. It needs to be Jackson. Implement a RedisTmeplate yourself. The generics are object, object. Implement CacheManager. You’ll see that the cache is in JSON form.