Commonly used Spring Boot annotations and sample code

Introduction: Spring Boot is a tool for quickly building applications based on the Spring framework. By providing a series of annotations, it allows developers to more easily configure, manage and control various behaviors of the application. The following are some commonly used Spring Boot annotations, as well as their functions and sample code, which can help developers better understand how to use these annotations to build powerful applications.

  1. @SpringBootApplication

    • Function: Combines @Configuration, @EnableAutoConfiguration and @ComponentScan to identify a main Spring Boot application class.
    • Role: Used to enable automatic configuration, component scanning, and entry points for Spring Boot applications.
    • Sample code:
      @SpringBootApplication
      public class MyApp {
              
              
          public static void main(String[] args) {
              
              
              SpringApplication.run(MyApp.class, args);
          }
      }
      
  2. @RestController

    • Function: Combine @Controller and @ResponseBody to create a RESTful style controller.
    • Function: Automatically convert the return value of the controller method into JSON format, suitable for building RESTful API.
    • Sample code:
      @RestController
      public class MyController {
              
              
          @GetMapping("/hello")
          public String hello() {
              
              
              return "Hello, Spring Boot!";
          }
      }
      
  3. @RequestMapping

    • Function: Map HTTP requests to controller methods.
    • Function: Define the mapping relationship between URL paths and HTTP methods and controller methods, which can be used to build request processors.
    • Sample code:
      @RestController
      public class MyController {
              
              
          @RequestMapping(value = "/greet", method = RequestMethod.GET)
          public String greet() {
              
              
              return "Greetings from Spring Boot!";
          }
      }
      
  4. @Autowired

    • Function: Automatically assemble Spring Bean.
    • Function: Inject a dependent Bean into another Bean, eliminating the need to manually configure Bean dependencies.
    • Sample code:
      @Service
      public class MyService {
              
              
          // Autowiring a dependency
          @Autowired
          private MyRepository repository;
      }
      
  5. @Configuration

    • Function: Identify a class as a configuration class.
    • Role: Used to define Spring Bean configuration, usually used with @Bean.
    • Sample code:
      @Configuration
      public class MyConfig {
              
              
          @Bean
          public MyBean myBean() {
              
              
              return new MyBean();
          }
      }
      
  6. @EnableAutoConfiguration

    • Function: Enable Spring Boot's automatic configuration mechanism.
    • Function: Automatically configure Spring Bean based on dependencies and configurations on the classpath.
    • Sample code: Usually enabled implicitly in @SpringBootApplication.
  7. @ComponentScan

    • Function: Specify packages to scan for Spring components.
    • Function: Used to find Spring components, such as controllers, services and repositories, in specific packages.
    • Sample code:
      @SpringBootApplication
      @ComponentScan(basePackages = "com.example")
      public class MyApp {
              
              
          // ...
      }
      
  8. @Value

    • Function: Inject attribute values.
    • Function: Inject values ​​from external configuration files into Bean properties.
    • Sample code:
      @Service
      public class MyService {
              
              
          @Value("${myapp.api.key}")
          private String apiKey;
      }
      
  9. @ConfigurationProperties

    • Function: Bind properties to configuration classes.
    • Function: Bind the values ​​in the external configuration file to the properties of the configuration class to facilitate unified management of configuration.
    • Sample code:
      @Component
      @ConfigurationProperties(prefix = "myapp")
      public class MyAppProperties {
              
              
          private String apiKey;
          // getters and setters
      }
      

These are some commonly used core annotations in Spring Boot. Each annotation has different purposes and functions to simplify application development and configuration. Depending on the application needs, you can flexibly select appropriate annotations to achieve the desired effect.

Guess you like

Origin blog.csdn.net/laterstage/article/details/132618562