1. What are the commonly used annotations in springboot?
-
@springbootApplication : This annotation is the core annotation of Spring Boot. It is used on the main class of Spring Boot to identify that this is a Spring Boot application and is used to enable various capabilities of Spring Boot. In fact, this annotation is a combination of @Configuration , @EnableAutoConfiguration , @ComponentScan three annotations
-
@EnableAutoConfiguration : allows Spring Boot to automatically configure annotations. After enabling this annotation, Spring Boot can configure Spring Beans according to the packages or classes in the current classpath
-
@Configuration : used to define a configuration class, indicating that this class is the information source for Bean configuration, equivalent to a traditional xml configuration file, generally added to the main class
-
@ComponentScan : Component scan. Let spring Boot scan the Configuration class and add it to the program context. The @ComponentScan annotation will assemble the classes marked with @Controller , @Service , @Repository , @Component annotations into the spring container by default.
-
@Repository : used to annotate data access components, ie DAO components
-
@Service : Components used to decorate the service layer
-
@Component : Instantiate ordinary pojo into the spring container, which refers to components in general. When the components are not well classified, we can use this annotation to mark them
-
@Controller : used to mark control layer components
-
@RestController — used to annotate control layer components: equivalent to @Controller and @ResponseBody
-
@Bean : Equivalent to XML, placed above the method, not the class, which means to generate a bean and hand it over to spring for management
-
@AutoWired : byType way. Use the configured Bean to complete the assembly of attributes and methods. It can annotate class member variables, methods and constructors to complete the work of automatic assembly.
-
@Qualifier : When there are multiple beans of the same type, @Qualifier ("name") can be used to specify. Use with @Autowired
-
@Resource (): Default byName. Do something similar to @Autowired
-
@RequsetMapping ——-RequestMapping is an annotation used to handle request address mapping @GetMapping + @PostMapping + @PutMapping + @DeleteMapping and so on:
-
@Param : used in front of the parameters of the method. Generally, when there is only one parameter, it can be considered not to be used.
-
@RequestParam : used in front of method parameters
-
@PathVariable : Path variable. The parameter must be the same as the name in the curly brackets
-
@ConfigurationProperties : Spring Boot can use annotations to map custom properties files to entity beans, such as config.properties files
2. What is the difference between @Component and @bean?
-
@Component generally acts on classes, @bean acts on methods
-
@Component is used to auto-detect and auto-configure beans using classpath scanning. There is an implicit one-to-one mapping between annotated classes and beans (i.e. one bean per class).
-
@Bean is used to explicitly declare a single bean instead of letting Spring do it automatically as above. It separates the bean's declaration from the class definition and allows you to create and configure beans precisely.
3. What are the advantages of springboot relative to spring? ====== Asked +1
- All annotations are used, no cumbersome xml configuration
- Built-in http server, such as jetty, tomcat. No need for additional integration to download tomcat
- Quickly integrate third-party frameworks, such as redis, mybatis, etc. (can be understood as automatic configuration)
4. What is the principle of Spring Boot's automatic configuration? ====== Asked +2
-
First of all, we can see that there is a @SpringBootApplication annotation on the startup class of springboot. This annotation is a composite annotation or a derived annotation, which has an annotation @EnableAutoConfiguration , which is an annotation that enables automatic configuration in popular terms.
-
This annotation is also a derived annotation, and the key functions are provided by @Import. The selectImports () method of the AutoConfigurationImportSelector class it imports scans all jar packages with META-INF/spring.factories through SpringFactoriesLoader.loadFactoryNames(), which will have A spring.factories file.
-
This spring.factories file is also in the form of a set of key=value, one of which is the full class name of the EnableAutoConfiguration class, and its value is a list of xxxxAutoConfiguration class names
-
We click into the xxxxAutoConfiguration class, and there will actually be some annotations in it to determine whether this xxxxAutoConfiguration is in effect. If you look closely, you can also see a class of xxxxProperties. You can click on it to see some properties. When you need to configure some information for subsequent import, you can configure it according to these properties.
PS: To sum up my personal opinion: when we start Springboot, it will scan all the auto-configuration classes under spring.factories by automatically configuring this annotation, but not all of them take effect. There will be a judgment at the bottom, only We have imported the corresponding dependencies, which means that the automatic assembly takes effect, which means that the configuration is successful!
5. What is YAML? What are the advantages of YAML configuration?
- YAML is a human-readable data serialization language. It is usually used for configuration files. Compared to properties files, YAML files are more structured and less confusing if we want to add complex properties to configuration files. It can be seen that YAML has hierarchical configuration data
- Advantage:
- Orderly configuration. In some special scenarios, orderly configuration is critical
- Arrays are supported, and the elements in the array can be primitive data types or objects
- concise
6. How to solve cross-domain problems in Spring Boot?
- Solve the cross-domain problem through CORS, implement the WebMvcConfigurer interface and then rewrite the addCorsMappings method to solve the cross-domain problem
@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .maxAge(3600); } }
7. What is the difference between a jar made by Spring Boot and an ordinary jar?
-
The final packaged jar of the Spring Boot project is an executable jar. This jar can be run directly through the java -jar xxx.jar command. This jar cannot be depended on by other projects as a normal jar, and it cannot be used even if it depends. the type
-
Spring Boot's jar cannot be depended on by other projects, mainly because its structure is different from that of ordinary jars. Ordinary jar package, after decompression, is the package name directly, and the package is our code, and after the executable jar packaged by Spring Boot is decompressed, it is our code in the \BOOT-INF\classes directory, so it cannot be directly Quote. If you have to refer to it, you can add configuration to the pom.xml file and package the Spring Boot project into two jars, one executable and one referenced
8. How to implement scheduled tasks in Spring Boot?
- First add the annotation @EnableScheduling to the startup class
- Famous for adding cron expressions to the methods you want to execute periodically