Spring Boot 常用注解

Spring Boot 中有许多常用的注解,它们用于简化开发工作、配置应用程序、定义 Bean 等。以下是一些常用的 Spring Boot 注解及其简要介绍:

  1. @SpringBootApplication

    该注解用于标记 Spring Boot 应用的主类,通常放在主类上,用于启动 Spring Boot 应用程序。
  2. 使用方法:

    1. 首先,在你的 Spring Boot 项目中找到主类(通常是含有main方法的类)。

    2. 在主类上添加`@SpringBootApplication`注解。例如:

    ```java
    @SpringBootApplication
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    ```

    3. `@SpringBootApplication`注解实际上是一个组

  3. @RestController

    • 该注解用于标记一个控制器类,并将其方法的返回值直接作为 HTTP 响应体返回。
    • 使用方法:在Spring Boot中,@RestController注解用于定义RESTful风格的控制器(Controller)。使用该注解的类会将其方法的返回值直接作为HTTP响应的内容,而不是视图渲染。下面是一个简单的@RestController的使用示例:
    • ```java
    • import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;

      @RestController
      public class HelloController {

          @GetMapping("/hello")
          public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
              return "Hello, " + name + "!";
          }
      }
      ```

      在上面的示例中,我们创建了一个HelloController类,并使用@RestController注解标记为RESTful控制器。在该类中,我们定义了一个使用@GetMapping注解的hello方法,用于处理HTTP GET请求。该方法接受一个名为name的请求参数,如果没有提供参数,则默认为"World"。方法的返回值直接作为HTTP响应的内容。

      当向应用程序发送GET请求`/hello`时,如果不指定name参数,将会得到"Hello, World!"的响应;如果指定了name参数,比如`/hello?name=John`,将会得到"Hello, John!"的响应。

      总之,@RestController注解的类中的方法可以方便地处理HTTP请求,并直接返回数据给客户端,非常适合构建RESTful风格的API。

  4. @RequestMapping

    • 用于将 HTTP 请求映射到控制器的处理方法上,可以指定处理的 URL 路径和请求方法。
    • 使用方法:

      @RequestMapping 是 Spring MVC 中用来映射 HTTP 请求路径的注解,它可以用在控制器(Controller)的方法上,用来指定处理请求的 URL 路径。

      @RequestMapping 注解有多个参数,最常用的是 value,用来指定请求的路径。另外,它还可以接受 method、params、headers 等参数,用于更精确地匹配请求。

      下面是 @RequestMapping 注解的使用方法举例:

      ```java
      @Controller
      @RequestMapping("/users")
      public class UserController {

          @RequestMapping(value = "/{id}", method = RequestMethod.GET)
          public String getUserById(@PathVariable("id") Long id, Model model) {
              // 处理获取用户信息的业务逻辑
              User user = userService.getUserById(id);
              model.addAttribute("user", user);
              return "userDetail";
          }

          @RequestMapping(value = "/create", method = RequestMethod.POST)
          public String createUser(User user) {
              // 处理创建用户的业务逻辑
              userService.createUser(user);
              return "redirect:/users/" + user.getId();
          }
      }
      ```

      在这个示例中,@RequestMapping 注解被用在 UserController 类和它的两个方法上。在类级别使用 @RequestMapping 注解,指定了该控制器处理的所有请求路径的共同父路径("/users")。而在方法级别使用 @RequestMapping 注解,分别指定了处理 GET 请求和 POST 请求的路径("/{id}" 和 "/create")。

      在第一个方法中,@PathVariable 注解用来将 URL 中的 id 参数绑定到方法的 id 参数上,实现了路径变量的获取。在第二个方法中,方法的参数 User 对象通过表单提交的方式获取,并且将用户创建成功后重定向到用户详情页面。

      总之,@RequestMapping 注解是 Spring MVC 中非常常用的注解,用于定义控制器处理请求的路径和方法。

  5. @Autowired

    • 用于自动装配 Bean,可以标记在字段、构造方法、Setter 方法上,Spring Boot 会自动注入匹配的 Bean。
    • 使用方法:

      在Spring Boot中,@Autowired注解用于自动装配(依赖注入)Bean。它可以标记在字段、构造方法和Setter方法上,让Spring Boot自动查找匹配的Bean并将其注入。

      以下是@Autowired注解的使用方法示例:

      1. 字段注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class UserService {
          @Autowired
          private UserRepository userRepository;
          
          // ...
      }
      ```

      在上面的示例中,通过@Autowired注解将UserRepository类型的Bean自动装配到UserService类的userRepository字段上。

      2. 构造方法注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class UserService {
          private UserRepository userRepository;
          
          @Autowired
          public UserService(UserRepository userRepository) {
              this.userRepository = userRepository;
          }
          
          // ...
      }
      ```

      在上面的示例中,通过@Autowired注解将UserRepository类型的Bean自动装配到UserService类的构造方法参数上。

      3. Setter方法注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class UserService {
          private UserRepository userRepository;
          
          @Autowired
          public void setUserRepository(UserRepository userRepository) {
              this.userRepository = userRepository;
          }

          // ...
      }
      ```

      在上面的示例中,通过@Autowired注解将UserRepository类型的Bean自动装配到UserService类的setter方法参数上。

      无论是字段注入、构造方法注入还是Setter方法注入,@Autowired都可以自动查找并注入匹配的Bean。当有多个匹配的Bean时,可以使用@Qualifier注解或使用@Autowired和@Bean搭配使用的方式进行更精确的控制。

      需要注意的是,为了使@Autowired注解生效,还需要在Spring Boot应用程序中启用自动装配(如@SpringBootApplication或@EnableAutoConfiguration注解)。

      总之,@Autowired注解在Spring Boot中是一种方便的方式,用于实现依赖注入,让Spring Boot负责查找并注入所需的Bean。

  6. @Component

    • 通用的 Spring 组件标识注解,一般作为父类注解,用于标记一个类为 Spring 的组件,让 Spring 扫描并管理。
    • 使用方法:

      在Spring Boot中,@Component注解用于标记一个类为Spring组件。被@Component注解标记的类将由Spring框架进行扫描,并将其实例化为Bean,可以在其他地方使用@Autowired注解进行依赖注入。

      以下是@Component注解的使用方法示例:

      ```java
      import org.springframework.stereotype.Component;

      @Component
      public class MyComponent {
          public void doSomething() {
              // 执行一些操作
          }
      }
      ```

      在上面的示例中,我们创建了一个名为MyComponent的类,并使用@Component注解将其标记为Spring组件。被@Component注解标记的类会自动被Spring框架扫描并创建对应的Bean。

      在其他类中,可以通过@Autowired注解将MyComponent类的实例注入进来,实现依赖注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class MyService {

          private final MyComponent myComponent;

          @Autowired
          public MyService(MyComponent myComponent) {
              this.myComponent = myComponent;
          }

          // 使用myComponent执行某些操作
      }
      ```

      在上面的示例中,我们创建了一个名为MyService的服务类,通过@Autowired注解将MyComponent类的实例注入到MyService类中的构造方法参数中。

      总之,@Component注解用于标记一个类为Spring组件,并由Spring框架进行自动扫描和实例化。被标记的类可以与其他组件进行依赖注入,提供更灵活和可扩展的应用程序开发方式。

  7. @Service

    • 用于标记一个业务逻辑组件,通常作用与@Service注解功能相同。
    • 使用方法:

      在Spring Boot中,@Service注解用于标记一个类为服务层组件,通常用于业务逻辑的处理。被@Service注解标记的类会被Spring框架自动扫描并注册为Bean,可以通过@Autowired注解进行依赖注入。

      以下是@Service注解的使用方法示例:

      ```java
      import org.springframework.stereotype.Service;

      @Service
      public class MyService {
          public void doSomething() {
              // 执行一些业务逻辑操作
          }
      }
      ```

      在上面的示例中,我们创建了一个名为MyService的服务类,并使用@Service注解将其标记为服务层组件。被@Service注解标记的类会被Spring框架识别为服务层组件,并进行相应的扫描和实例化。

      在其他类中,可以通过@Autowired注解将MyService类的实例注入进来,实现依赖注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;

      @Controller
      public class MyController {

          private final MyService myService;

          @Autowired
          public MyController(MyService myService) {
              this.myService = myService;
          }

          // 使用myService调用服务层方法
      }
      ```

      在上面的示例中,我们创建了一个名为MyController的控制器类,通过@Autowired注解将MyService类的实例注入到MyController类的构造方法参数中。

      总之,@Service注解用于标记一个类为服务层组件,被Spring框架自动扫描和实例化。通过@Autowired注解可以将服务层组件注入到其他组件中,实现依赖注入,使得业务逻辑的处理更加灵活和可维护。

  8. @Repository

    • 用于标记一个数据访问组件,通常与持久层(如DAO)相关的类上使用。
    • 使用方法:

      在Spring Boot中,@Repository注解用于标记一个类为数据访问层组件,通常用于数据库操作。被@Repository注解标记的类会被Spring框架自动扫描并注册为Bean,可以通过@Autowired注解进行依赖注入。

      以下是@Repository注解的使用方法示例:

      ```java
      import org.springframework.stereotype.Repository;

      @Repository
      public class UserRepository {
          public User getUserById(Long id) {
              // 从数据库中获取用户信息的逻辑
          }

          public void saveUser(User user) {
              // 将用户信息保存到数据库的逻辑
          }
      }
      ```

      在上面的示例中,我们创建了一个名为UserRepository的数据访问层类,并使用@Repository注解将其标记为数据访问层组件。被@Repository注解标记的类会被Spring框架识别为数据访问层组件,并进行相应的扫描和实例化。

      在其他类中,可以通过@Autowired注解将UserRepository类的实例注入进来,实现依赖注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class UserService {

          private final UserRepository userRepository;

          @Autowired
          public UserService(UserRepository userRepository) {
              this.userRepository = userRepository;
          }

          // 使用userRepository进行数据库操作
      }
      ```

      在上面的示例中,我们创建了一个名为UserService的服务类,通过@Autowired注解将UserRepository类的实例注入到UserService类的构造方法参数中,以便在服务类中使用数据访问层组件的功能。

      总之,@Repository注解用于标记一个类为数据访问层组件,被Spring框架自动扫描和实例化。通过@Autowired注解可以将数据访问层组件注入到其他组件中,实现依赖注入,使得数据库操作更加灵活和可维护。

  9. @Configuration

    • 用于定义配置类,配合@Bean注解用于定义 Bean。
    • 使用方法:

      在Spring Boot中,@Configuration注解用于标记一个类为配置类,通常用于定义和组织Bean的创建和配置。被@Configuration注解标记的类会被Spring框架自动扫描并作为配置类使用。

      以下是@Configuration注解的使用方法示例:

      ```java
      import org.springframework.context.annotation.Configuration;

      @Configuration
      public class AppConfig {
          // 在这里定义和配置Bean
      }
      ```

      在上面的示例中,我们创建了一个名为AppConfig的配置类,并使用@Configuration注解将其标记为配置类。被@Configuration注解标记的类会被Spring框架识别为配置类,并进行相应的扫描和实例化。

      在配置类中,可以使用其他注解如@Bean、@ComponentScan、@Import等来定义和配置Bean。例如,使用@Bean注解定义一个Bean:

      ```java
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;

      @Configuration
      public class AppConfig {
          
          @Bean
          public MyService myService() {
              return new MyService();
       

  10. @Bean

    • 用于定义一个 Bean,通常与@Configuration结合使用。
    • 使用方法:

      在Spring框架中,@Bean注解通常用于配置类中的方法,用于声明将由Spring容器管理的bean。这种方式是Java配置的一部分,与基于XML的配置方式相对应。

      以下是@Bean注解的使用方法示例:

      ```java
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;

      @Configuration
      public class AppConfig {

          @Bean
          public MyService myService() {
              return new MyService();
          }
      }
      ```

      在上面的示例中,我们创建了一个名为AppConfig的配置类,并使用@Configuration注解标记该类为配置类,表示它包含了bean的配置信息。然后,通过@Bean注解将myService方法声明为一个bean的定义方法。在这个方法中,我们返回了一个MyService的实例,Spring容器会将其注册为一个bean,并进行管理。

      在其他类中,可以通过@Autowired注解将MyService bean注入进来,实现依赖注入:

      ```java
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class MyServiceClient {

          private final MyService myService;

          @Autowired
          public MyServiceClient(MyService myService) {
              this.myService = myService;
          }

          // 使用myService进行业务操作
      }
      ```

      在上面的示例中,我们创建了一个名为MyServiceClient的服务类,通过@Autowired注解将MyService bean注入到MyServiceClient类的构造方法参数中,以便在服务类中使用MyService的功能。

      总之,@Bean注解用于声明一个由Spring容器管理的bean的定义方法,被@Configuration注解的配置类中的@Bean方法会被Spring容器识别并注册为bean。通过@Autowired注解可以将声明的bean注入到其他组件中,实现依赖注入。

  11. @Value

    • 用于从配置文件中读取属性值,可以注入到对应的字段或方法参数中。
    • 使用方法:

      在Spring框架中,@Value注解用于将值注入到类的字段或方法参数中。它可以用于注入配置文件中的属性值、系统环境变量和其他Spring管理的bean的属性值。

      以下是@Value注解的使用方法示例:

      1. 注入配置文件中的属性值:

      ```java
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;

      @Component
      public class MyComponent {

          @Value("${my.property}")
          private String myProperty;

          // 使用myProperty进行操作
      }
      ```

      在上面的示例中,我们使用@Value注解将配置文件中的属性值`${my.property}`注入到MyComponent类的myProperty字段中。需要确保在配置文件中存在名为`my.property`的属性。

      2. 注入系统环境变量的值:

      ```java
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;

      @Component
      public class MyComponent {

          @Value("${JAVA_HOME}")
          private String javaHome;

          // 使用javaHome进行操作
      }
      ```

      在上面的示例中,我们使用@Value注解将系统环境变量`JAVA_HOME`的值注入到MyComponent类的javaHome字段中。

      3. 注入其他Spring管理的bean的属性值:

      ```java
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;

      @Component
      public class MyComponent {

          private final AnotherBean anotherBean;

          @Value("#{anotherBean.someProperty}")
          private int someValue;

          public MyComponent(AnotherBean anotherBean) {
              this.anotherBean = anotherBean;
          }

          // 使用anotherBean和someValue进行操作
      }
      ```

      在上面的示例中,我们使用@Value注解将AnotherBean bean的属性值`someProperty`注入到MyComponent类的someValue字段中。需要确保AnotherBean被正确地注入到MyComponent中。

      总之,@Value注解提供了一种方便的方式来将值注入到类的字段或方法参数中。它可以用于注入配置文件中的属性值、系统环境变量和其他Spring管理的bean的属性值。

  12. @EnableAutoConfiguration

    • 用于启用自动配置,Spring Boot 根据当前的 classpath 设置、其他 bean 和各种属性设置来推断应用程序该如何配置。
    • 使用方法:

      @EnableAutoConfiguration是Spring Boot框架中的一个注解,用于启用自动配置特性。它会根据项目的依赖和配置,自动配置Spring应用程序所需的bean和组件,使得开发者能够更快地搭建Spring应用程序而无需手动配置大量的组件。

      通常情况下,我们在Spring Boot应用程序的主配置类上使用@EnableAutoConfiguration注解来启用自动配置。下面是一个简单的示例:

      ```java
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

      @EnableAutoConfiguration
      @SpringBootApplication
      public class MyApplication {
          public static void main(String[] args) {
              SpringApplication.run(MyApplication.class, args);
          }
      }
      ```

      在上面的示例中,@EnableAutoConfiguration注解被用于启用自动配置特性。同时,@SpringBootApplication注解用于标记该类为Spring Boot应用程序的入口点。

      @EnableAutoConfiguration注解的工作原理是基于Spring Boot的条件化配置特性。它会根据classpath中的jar包、已经存在的bean以及各种属性来推断并自动配置Spring应用程序所需的bean和组件。开发者可以在application.properties或application.yml文件中设置各种属性来影响自动配置的行为。

      需要注意的是,虽然@EnableAutoConfiguration可以大幅简化Spring Boot应用程序的搭建过程,但在某些情况下可能会导致意外的行为,尤其是当应用程序的依赖关系比较复杂时。因此,在实际开发中,开发者需要对自动配置的行为有一定的了解,并在必要时进行手动配置来覆盖自动配置的结果。

      总之,@EnableAutoConfiguration注解是Spring Boot框架中用于启用自动配置特性的注解,它会根据项目的依赖和配置,自动配置Spring应用程序所需的bean和组件,从而简化开发流程。

这些注解只是 Spring Boot 中常用的一部分,Spring Boot 还提供了许多其他有用的注解,用以简化开发工作并提高代码的可读性和可维护性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值