SpringBoot常用注解

SpringBoot常用注解

前言

最近的面试中,不止一次的被问到了SpringBoot的常用注解。正好顺便来梳理一下,做个总结。

常用注解

1. @SpringBootApplication

@SpringBootApplication是SpringBoot的一个核心注解,作用在SpringBoot的启动类上。

当我们在创建一个SpringBoot项目时,会创建一个启动类。

java 复制代码
@SpringBootApplication
public class BaseApplication {

    public static void main(String[] args) {
        SpringApplication.run(BaseApplication.class, args);
    }
}

我们点进@SpringBootApplication注解,会发现他包含了三个注解@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan。

java 复制代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}

@SpringBootConfiguration:允许在 Spring 上下文中注册额外的 bean 或导入其他配置类

@EnableAutoConfiguration:启动SpringBoot自动配置

@ComponentScan:Bean扫描,扫描那些被Spring管理的Bean。如:@Controller, @Component等

2. Spring Bean相关的注解

@Component,@Controller,@Service,@Repository

@Component:通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。

@Repository : 对应持久层即 Dao 层,主要用于数据库相关操作。

@Service : 对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao 层。

@Controller : 对应 Spring MVC 控制层,主要用于接受用户请求并调用 Service 层返回数据给前端页面。

@Configuration

我们一般用来声明这个类是一个配置类。

@Bean

一般作用在方法上,用来注入一个Spring Bean。

java 复制代码
@Bean
@ConditionalOnClass(ThreadPoolExecutor.class)
public ThreadPoolExecutor myThreadPool() {
    return new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(100));
}

@RestController

@RestController可以看作是@Controller和@ResponseBody两个注解,表示这是个控制器 bean,并且是将函数的返回值直接填入 HTTP 响应体中,是 REST 风格的控制器。

@Scope

声明 Spring Bean 的作用域。

java 复制代码
@Bean
@Scope("singleton")
public User getUser() {
    return new User();
}

singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。

prototype : 每次请求都会创建一个新的 bean 实例。

request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。

session : 每一个 HTTP Session 会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

@Autowried

自动导入对象到类中,被注入进的类同样要被 Spring 容器管理比如:Service 类注入到 Controller 类中。

java 复制代码
@RestController
@RequestMapping("excel")
public class ExcelUserController {

    @Autowired
    private  UserService userService;

}

3. Http请求相关的注解

@RequestMapping,@PostMapping,@GetMapping,@PutMapping,@DeleteMapping

用来标识接口的请求路径。

@RequestMapping:作用在类上。

@PostMapping:post请求的

@GetMapping:get请求的

@PutMapping:update请求

@DeleteMapping:delete请求

@RequestBody

作用于post请求的参数体。

java 复制代码
@PostMapping("/add")
public String add(@RequestBody User user) {
    return "ok";
}

@RequestParam,@PathVariable

@PathVariable:用于获取路径参数,rest请求。

@RequestParam:用于获取查询参数,url?parm=。

java 复制代码
@GetMapping("/user/{userId}/list")
public List<User> userList(
         @PathVariable("userId") Long userId,
         @RequestParam(value = "type", required = false) String type ) {
...
}

4.全局异常处理

  1. @ControllerAdvice :注解定义全局异常处理类
  2. @ExceptionHandler :注解声明异常处理方法
java 复制代码
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 请求参数异常处理
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
       ......
    }
}

5. 事务相关@Transactional

在要开启事务的方法上使用@Transactional注解即可!

java 复制代码
@Transactional(rollbackFor = Exception.class)
public void save() {
  ......
}

6. 读取配置

@Value

使用 @Value("${property}") 读取比较简单的配置信息:

java 复制代码
@Value("${auth.url}")
private String authUrl;
@ConfigurationProperties

通过@ConfigurationProperties读取配置信息并与 bean 绑定.

java 复制代码
@Component
@ConfigurationProperties(prefix = "library")
class LibraryProperties {
    @NotEmpty
    private String location;
    private List<Book> books;

    @Setter
    @Getter
    @ToString
    static class Book {
        String name;
        String description;
    }
  省略getter/setter
  ......
}

@PropertySource

@PropertySource读取指定 properties 文件

java 复制代码
@Component
@PropertySource("classpath:website.properties")

class WebSite {
    @Value("${url}")
    private String url;

  省略getter/setter
  ......
}

总结

这里主要列举了一些比较常用的注解,可能不是很全面,希望对大家有帮助。

相关推荐
皮皮林55111 小时前
SpringBoot + Disruptor 实现特快高并发处理,支撑每秒 600 万订单无压力!
spring boot
阿丰资源11 小时前
基于SpringBoot的在线视频教育平台的设计与实现(附源码+数据库+文档,一键运行)
数据库·spring boot·后端
苍煜11 小时前
ThreadPoolExecutor线程池终极全解:同步异步判定+SpringBoot生产实战
java·开发语言·spring boot
阿丰资源19 小时前
基于SpringBoot的房产销售系统设计与实现(附源码+数据库+文档,一键运行)
数据库·spring boot·后端
aLTttY19 小时前
Spring Boot整合AI大模型实现智能问答系统实战
人工智能·spring boot·后端
Java成神之路-1 天前
面试题:@Controller 与 @RestController 区别
java·spring boot
aLTttY1 天前
Spring Boot 3.x 集成 AI 大模型实战指南
人工智能·spring boot·后端
凤山老林1 天前
Spring Boot 集成 TigerGraph 实现图谱分析技术方案
java·spring boot·后端·图谱分析·tigergraph
.生产的驴1 天前
SpringBoot 大文件分片上传 文件切片、断点续传与性能优化 切片技术与优化方案 文件高效上传
java·服务器·spring boot·后端·spring·spring cloud·状态模式
m0_380113842 天前
补单系统搭建及源码分享
数据库·spring boot·mybatis