Springboot中的注解有哪些 - 面试宝典

举报
皮牙子抓饭 发表于 2023/08/24 09:14:59 2023/08/24
726 1 1
【摘要】 当然可以!在Spring Boot中,有许多常用的注解用于不同的目的。以下是一些常见的Spring Boot注解:​​@SpringBootApplication​​:标识一个Spring Boot应用的主类,通常包含了启动类、自动配置和组件扫描。​​@Controller​​:将类标记为Spring MVC的控制器,处理HTTP请求。​​@RestController​​:与​​@Cont...

当然可以!在Spring Boot中,有许多常用的注解用于不同的目的。以下是一些常见的Spring Boot注解:

  1. ​@SpringBootApplication​​:标识一个Spring Boot应用的主类,通常包含了启动类、自动配置和组件扫描。
  2. ​@Controller​​:将类标记为Spring MVC的控制器,处理HTTP请求。
  3. ​@RestController​​:与​​@Controller​​类似,但是专门用于RESTful风格的控制器,其方法返回的是数据而不是视图。
  4. ​@RequestMapping​​:用于映射HTTP请求到控制器的处理方法,可用于类级别和方法级别。
  5. ​@GetMapping​​、​​@PostMapping​​、​​@PutMapping​​、​​@DeleteMapping​​等:与​​@RequestMapping​​类似,用于更具体的HTTP方法映射。
  6. ​@RequestParam​​:用于将请求参数绑定到方法的参数上。
  7. ​@PathVariable​​:用于从URL路径中提取变量值并绑定到方法参数上。
  8. ​@RequestBody​​:用于将HTTP请求的内容绑定到方法的参数上,通常用于处理POST请求的JSON数据。
  9. ​@ResponseBody​​:用于将方法的返回值直接作为HTTP响应的内容,通常用于返回JSON数据。
  10. ​@Autowired​​:用于自动装配Spring Bean,可以在属性、构造方法、方法参数上使用。
  11. ​@Qualifier​​:与​​@Autowired​​一起使用,指定要装配的具体Bean的名称。
  12. ​@Component​​:通用的组件注解,用于标识一个类为Spring管理的组件。
  13. ​@Service​​:用于标识一个类为业务逻辑层的组件。
  14. ​@Repository​​:用于标识一个类为数据访问层的组件。
  15. ​@Configuration​​:用于定义配置类,通常与​​@Bean​​一起使用来声明Spring Bean。
  16. ​@Value​​:用于注入外部配置文件中的值到类的属性中。
  17. ​@EnableAutoConfiguration​​:启用Spring Boot的自动配置机制。
  18. ​@Conditional​​:根据特定条件决定是否创建Bean的注解。
  19. ​@Profile​​:根据不同的配置环境选择性地加载Bean。
  20. ​@EnableAsync​​:启用异步方法的支持。 这只是一些常见的Spring Boot注解,实际上还有很多其他的注解可以用于不同的场景和需求。

当然,以下是一些常见注解的示例代码:

javaCopy codeimport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
@RestController
@RequestMapping("/api")
public class MyController {
    @Autowired
    private MyService myService;
    @GetMapping("/hello")
    public String sayHello() {
        return myService.getMessage();
    }
}
@Service
public class MyService {
    public String getMessage() {
        return "Hello, world!";
    }
}
@Configuration
public class MyConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}
@Component
public class MyBean {
    // Bean implementation here
}
@Repository
public class MyRepository {
    // Repository implementation here
}
@Profile("development")
@Service
public class DevSpecificService implements MyService {
    // Development-specific service implementation
}
@Profile("production")
@Service
public class ProdSpecificService implements MyService {
    // Production-specific service implementation
}
@EnableAsync
@Configuration
public class AsyncConfig {
    // Async configuration setup
}

请注意,上述代码仅为示例,实际使用时需要根据具体业务需求进行适当的调整。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(1

抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。