Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Spring Boot comments?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are Spring Boot annotations". In daily operation, I believe many people have doubts about Spring Boot annotations. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are Spring Boot annotations?" Next, please follow the editor to study!

Notes and introduction

1. @ SpringBootApplication

This is the most core annotation of Spring Boot, which is used on the Spring Boot main class to identify that this is a Spring Boot application that is used to open the capabilities of Spring Boot.

In fact, this annotation is a combination of @ SpringBootConfiguration, @ EnableAutoConfiguration, and @ ComponentScan, which can also be used instead of @ SpringBootApplication annotations. @ SpringBootApplication scans all packages and subpackages of this class at one level by default

2. @ EnableAutoConfiguration

Allow Spring Boot to configure annotations automatically, and when this annotation is enabled, Spring Boot can configure Spring Bean based on the package or class under the current classpath.

For example, if the JAR package Mybatis is available in the current classpath, MybatisAutoConfiguration annotations can configure each Spring Bean of Mybatis according to the relevant parameters.

3. @ Configuration

This is a note added by Spring 3.0 to replace the applicationContext.xml configuration file, and everything that can be done in this configuration file can be registered through the class in which the annotation is located. Instead of the configuration file in the form of xml, it is developed with configuration class

4. @ SpringBootConfiguration

This annotation is a variation of the @ Configuration annotation and is only used to modify the Spring Boot configuration, or to facilitate subsequent extensions to Spring Boot.

5. @ ComponentScan

This is an annotation added by Spring 3.1to replace the component-scan configuration in the configuration file and enable component scanning, that is, automatically scan the @ Component annotation under the package path to register the bean instance to context.

6. @ Conditional

This is a new note added by Spring 4.0 to identify a Spring Bean or Configuration configuration file that opens the configuration when the specified conditions are met.

7, @ ConditionalOnBean

Combine the @ Conditional annotation to enable the configuration when there is a specified Bean in the container.

8. @ ConditionalOnMissingBean

Combining the @ Conditional annotation, as opposed to the @ ConditionalOnBean annotation, enables the configuration when there is no specified Bean in the container.

9, @ ConditionalOnClass

Combine the @ Conditional annotation to enable the configuration when there is a specified Class in the container.

10, @ ConditionalOnMissingClass

Combining the @ Conditional annotation, as opposed to the @ ConditionalOnMissingClass annotation, enables the configuration when there is no specified Class in the container.

11, @ ConditionalOnWebApplication

To combine the @ Conditional annotation, the configuration is enabled only if the current project type is WEB project.

There are three types of current projects.

Enum Type {/ * matches all web application * / ANY, / * only matches servlet web applicaiton * / SERVLET, / * only matches reactice-based web application * / REACTIVE}

12, @ ConditionalOnNotWebApplication

Combine the @ Conditional annotation, as opposed to the @ ConditionalOnWebApplication annotation, the configuration is enabled only if the current project type is not a WEB project.

13, @ ConditionalOnProperty

Combines the @ Conditional annotation to enable configuration when the specified property has a specified value.

14, @ ConditionalOnExpression

Combine the @ Conditional annotation to enable configuration only when the SpEL expression is true.

15, @ ConditionalOnJava

Combine the @ Conditional annotation to enable configuration only when the running Java JVM is in the specified version range.

16, @ ConditionalOnResource

Combine the @ Conditional annotation to enable configuration only when there are specified resources in the classpath.

17, @ ConditionalOnJndi

Combine the @ Conditional annotation to enable the configuration when the specified JNDI exists. JDNI (Java naming and directory interface Java Naming and Directory Interface)

18, @ ConditionalOnCloudPlatform

Combine the @ Conditional annotation to enable the configuration when the specified cloud platform is activated.

19, @ ConditionalOnSingleCandidate

Combine the @ Conditional annotation to enable configuration when the specified class has only one Bean in the container, or multiple preferred ones at the same time.

20, @ ConfigurationProperties

Used to load additional configurations (such as .properties files), which can be used in @ Configuration annotation classes or @ Bean annotation methods.

21, @ EnableConfigurationProperties

It is generally used with the @ ConfigurationProperties annotation to enable support for the @ ConfigurationProperties annotation configuration Bean.

22, @ AutoConfigureAfter

Used on the autoconfiguration class, indicating that the autoconfiguration class needs to be configured after another specified autoconfiguration class has been configured.

For example, the automatic configuration class of Mybatis needs to be done after the data source automatically configures the class.

@ AutoConfigureAfter (DataSourceAutoConfiguration.class) public class MybatisAutoConfiguration {}

23, @ AutoConfigureBefore

This is the opposite of the @ AutoConfigureAfter annotation, indicating that the autoconfiguration class needs to be configured before another specified autoconfiguration class.

24, @ Import

This is a new annotation added by Spring 3.0 to import one or more classes modified by the @ Configuration annotation, which is widely used in Spring Boot.

25, @ ImportResource

This is a new note added by Spring 3.0 to import one or more Spring configuration files, which is useful for Spring Boot compatibility with older projects, because some configurations cannot be configured through Java Config and can only be imported with this annotation.

Read configuration mode summary

1. Read application file

Add to the application.yml or properties file:

User.name = Wolf King user.age = 25 user.sex = man

@ value mode

@ Component public class User {@ Value ("${user.name}") private String name; @ Value ("${user.age}") private int age; @ Value ("${user.sex}") private boolean sex; public User () {} public User (String name, int age, boolean man) {this.name = name; this.age = age; this.man = man } public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public boolean isMan () {return man } public void setMan (boolean man) {this.man = man;}}

2.@ConfigurationProperties comment reading mode

Component @ ConfigurationProperties (prefix = "user") public class User2 {private String name; private int age; private boolean sex; public User2 () {} public User2 (String name, int age, boolean man) {this.name = name; this.age = age; this.man = man;}}

3. Read the specified file

Create a config/myConfig.properties under the resources directory:

Db.username=root db.password=123

@ PropertySource+@Value comment reading method

@ Component @ PropertySource (value = {"config/myConfig.properties"}) public class User2 {@ Value ("${db.userName}") private String userName; @ Value ("${db.userName}") private String passWord; public User2 () {} public User2 (String userName,String passWord) {this.userName = userName; this.passWord = passWord;}}

It can also be read through @ PropertySource+@ConfigurationProperties annotation.

At this point, the study of "what are the Spring Boot comments" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report