SpringBoot Framework

Thirteen, SpringBoot framework

1. What is Spring Boot?

Over the years, spring has become more complex as new features are added. Just visit the https://spring.io/projects page and we will see the different features of all the Spring projects that can be used in our application. If we have to start a new Spring project, we have to add build path or add maven dependency

relationship, configure application server, add spring configuration. So starting a new spring project takes a lot of effort as we now have to do everything from scratch.
Spring Boot is the solution to this problem. Spring Boot has been built on top of the existing spring framework. With spring boot, we avoid all the boilerplate code and configuration that we had to do before. Therefore, Spring Boot can help us use existing Spring features more robustly with minimal effort.

2. What are the features of Spring Boot?

a. Provide a faster and broader onboarding experience for Spring development.
b, out of the box, away from cumbersome configuration.
c. Provides a series of non-business functions common to large-scale projects, such as: embedded server, security management, operation data monitoring, health check and externalized configuration, etc.
d. Absolutely no code generation and no XML configuration required.

3. What are the advantages of Spring Boot?

a. Reduce development and testing time and effort.
b, the use of JavaConfig helps to avoid the use of XML. c. Avoid a large number of Maven imports and various version conflicts.
d. Quickly start development by providing default values. No separate web server required. This means you no longer need to start Tomcat, Glassfish or anything else.
e. Less configuration is required because there is no web.xml file. Just add the class annotated with @Configuration, then add the method annotated with @Bean, and Spring will automatically load the object and manage it as before. You can even add @Autowired to a bean method to have Spring autowire the required dependencies. Environment-based configuration uses these properties, you can pass the environment you are using to the application: -Dspring.profiles.active=
{enviornment}. After loading the main application properties file, Spring will load subsequent application properties files in (application{environment}.properties).

4. What is the difference between Spring Boot, Spring MVC and Spring?

a. The most important feature of Spring is dependency injection. All SpringModules are either Dependency Injection or IOC Inversion of Control.
When we properly use DI or IOC, we can develop loosely coupled applications. Unit testing of loosely coupled applications can be done easily.
b, Spring MVC provides a separate method to develop Web applications. By using some simple concepts like DispatcherServelet, MoudlAndView and ViewResolver, developing Web applications will become very simple.
The problem with c, Spring and SpringMVC is that a large number of parameters need to be configured. Spring Boot solves this problem with an auto-configured and started project. To build production-ready applications faster, Spring Boot provides some non-functional features.

5. What is the core annotation of Spring Boot? Which annotations are it mainly composed of?

The annotation on the startup class is @SpringBootApplication, which is also the core annotation of Spring Boot. The main combination includes the following three annotations: @SpringBootConfiguration: Combines the @Configuration annotation to implement the function of the configuration file. @EnableAutoConfiguration: Turn on the automatic configuration function, or turn off an automatic configuration option, such as turning off the data source automatic configuration function: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }).
@ComponentScan: Spring component scan.

6. Does Spring Boot need a separate container to run?

No need, built-in containers such as Tomcat/Jetty.

7. How do you understand Starters in Spring Boot?

Starters can be understood as starters, which contain a series of dependency packages that can be integrated into the application. You can integrate Spring and other technologies in one stop without looking for sample code and dependency packages everywhere. If you want to use Spring JPA to access the database, just add the spring-boot-starter-data-jpa starter dependency and you can use it.

8. Can Spring Boot use XML configuration?

Spring Boot recommends using Java configuration instead of XML configuration, but XML configuration can also be used in Spring Boot, and an XML configuration can be introduced through the @ImportResource annotation.

9. What is the difference between bootstrap.properties and application.properties ?

It may not be easy to encounter the bootstrap.properties configuration file for Spring Boot development alone, but when combined with Spring Cloud, this configuration is often encountered, especially when some remote configuration files need to be loaded. bootstrap.properties is loaded before application.properties and the configuration takes effect during the bootstrap phase of the application context. Generally speaking, we will use it in Spring Cloud Config or Nacos. bootstrap.properties is loaded by the Spring ApplicationContext parent class, which is started before the Application.properties loaded ApplicatonContext. Of course, the properties in the previous description can also be modified to yaml.

10. The principle of automatic assembly

The key to the implementation of @EnableAutoConfiguration is the introduction of AutoConfigurationImportSelector, whose core logic is the selectImports method. The logic is roughly as follows:
Load all possible auto-configuration classes from the META-INF/spring.factories file under the spring-boot-autoconfigure package; go to Heavy, and exclude the classes carried by the exclude and excludeName attributes;
filter, return the automatic configuration classes that meet the condition (@Conditional);

11. How to reload changes on Spring Boot without restarting the server?

This can be achieved using the DEV tool. With this dependency you can save any changes and the embedded tomcat will restart. Spring Boot has a Development Tools (DevTools) module that helps improve developer productivity. A major challenge for Java developers is to automatically deploy file changes to the server and automatically restart the server. Developers can reload changes on Spring Boot without restarting the server. This will eliminate the need to manually deploy changes each time. Spring Boot did not have this feature when it released its first version. This is the most requested feature for developers. DevTools modules fully meet the needs of developers. This module will be disabled in production environment. It also provides the H2 database console for better application testing.

2 org.springframework.boot 3 spring-boot-devtools 4 true 5
12. How to run Spring Boot application on custom port?

In order to run a Spring Boot application on a custom port, you can specify the port in application.properties.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325177835&siteId=291194637