Table of contents
- Preface
- 1. Talk about your understanding of springBoot
- 2. Advantages of why springBoot or springBoot
- 3. The difference between springBoot and springCloud
- What are the core configuration files and what are their functions
- There are several types of configuration files and what are the differences
- 6.What is hot deployment? How to implement hot deployment in springBoot
- 7.What is JavaConfig?
- Which is the core annotation of Boot? Which annotations are mainly composed of or introduce the @SpringBootApplication annotation
- starter starter
- How to start the project
- 11. How to run some specific code when Spring Boot is started?
- Global exception handling
- Common Boot Annotations
Preface
The latest java interview questions (java basics, collections, multithreading, jvm, locks, algorithms, CAS, Redis, databases, mybatis, spring, springMVC, springBoot, microservices)
1. Talk about your understanding of springBoot
It is a one-stop solution that simplifies the heavy configuration of spring and provides various launchers to enable developers to get started.
2. Advantages of why springBoot or springBoot
(1) Version lock:Resolve maven dependency version conflict problem
(2) Starting dependencies:Solve too many jar problems
(3) Automatic configuration:Solve too many configuration files
(4) Built-in tomcat:With built-in tomcat, you can run javaEE without any external tomcat.
3. The difference between springBoot and springCloud
SpringBoot is a fast-developed Spring framework, SpringCloud is a complete microservice framework, and SpringCloud relies on SpringBoot.
What are the core configuration files and what are their functions
applicationandbootstrapConfiguration file.
application:Automatic configuration for projects.
bootstrap:Some properties and encrypted or decrypted scenarios that cannot be overwritten. Loading first than application.
There are several types of configuration files and what are the differences
.yml and .properties
The main difference is that the writing method is different, and the .properties loading priority is greater than .yml.
= 8088 //.properties method
server://.yml method
port: 8088
6.What is hot deployment? How to implement hot deployment in springBoot
Hot deployment:Modifying the code without restarting it can be compiled and deployed to the server. (1) Modifying the code during development does not require restarting the service, saving time. (2) The programs deployed on the server can be upgraded without stopping.
① Use devtools. Set to true in the configuration file. It takes a build every time.
②Idea settings to achieve hot deployment.
7.What is JavaConfig?
JavaConfig is a product of the Spring community that provides pure Java methods for configuring Spring IoC containers, helping to avoid XML configurations.
Which is the core annotation of Boot? Which annotations are mainly composed of or introduce the @SpringBootApplication annotation
@SpringBootApplication: contains three annotations.
①SpringBootConfiguration:Implement the @Configuration annotation to implement the function of the configuration file.
②@EnableAutoConfiguration:Turn on the automatic configuration function, or turn off a certain automatic configuration option, Example
For example: java If you turn off the automatic configuration function of data source: @SpringBootApplication(exclude = { }).
③@ComponentScan:Spring component scan.
starter starter
Other technologies can be integrated through the launcher: web, redis, mybatis, etc.
Configure spring-boot-starter-web in pom and you can do web development.
Principle: These configuration classes will be loaded and placed into the spring container, so that these classes can be obtained from the container.
How to start the project
Pack
maven plugin runs
Main Start
11. How to run some specific code when Spring Boot is started?
The interfaces ApplicationRunner or CommandLineRunner can be implemented. These two interfaces are implemented in the same way (execute after the project starts up).
All of them only provide a run method, and write your business logic in the run method.
Global exception handling
@ControllerAdvice: Enables the capture of global exceptions. Then customize a method using the @ExceptionHandler annotation and then define the type of capture exceptions to uniformly handle these captured exceptions.
@ControllerAdvice
public class ExceptionUtil{
@ExceptionHandler(value =Exception.class)
@ResponseBody
public String exceptionHandler(Exception e){
System.out.println("Global Exception Catch:"+e);
return "Global exception catch, error reason:"+e.getMessage();
}
}
Common Boot Annotations
@SpringBootApplication:SpringBootConfiguration configuration class, componentScan scanning package, EnableAutoConfiguration import other configuration classes
@RestController:The role of @ResponseBody and @Controller.
@Component,@Service,@Controller,@Repository:Inject the class into the container.
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping:Mapping requests can only receive corresponding requests.
@AutoWired:Inject by type.
@Qualifier:Used in conjunction with AutoWired, and match by name based on type matching.
@Resource:Match dependency injection by name.
@Bean:Used to put the method return value object into the container.
@RequestParam:Get query parameters. That is, url?name=This form
@RequestBody:This annotation is used to obtain the request body data (body). Get has no request body, so it is generally used for post requests.@PathVariable:Get path parameters. That is, the form url/{id}.
@Value:Dynamically inject external values into the bean.
@Value("${}"): You can get the value of the configuration file.
@Value("#{}"): means that SpEl(Spring Expression Language is a Spring expression language that can query and manipulate data at runtime.) Expressions are usually used to obtain the properties of a bean, or to call a method of a bean.