Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Mysql

  • PHP

  • Python

  • java

    • java基础

    • java常用插件

    • spring

      • 快速入门
      • SpringBoot的常用注解
        • 使用在类名上的注解
          • @RestController 标明rest风格的控制器
          • @Controller 标明控制器
          • @Service 声明一个业务处理类
          • @Repository 声明数据库访问类
          • @Component
          • @Configuration 声明此类是一个配置类,常与@Bean配合使用
          • @ConfigurationProperties 把同类配置信息自动封装成一个实体类
          • @Resource 默认按 byName 自动注入
          • @Autowired 默认按 byType 自动注入
          • RequestMapping 请求映射
          • @Transactional 用于处理事务
          • @Qualifier 为Bean指定名称,随后在通过名字引用Bean
        • 使用在方法上的注解
          • @RequestBody 请求体
          • @PathVariable 用于获取路径中的参数
          • @Bean
          • @ResponseBody 标记响应体
        • 其他注解
          • @EnableAutoConfiguration 用来提供自动配置
          • @SpringBootApplication 用来启动入口类Application
          • @EnableScheduling 用来开启计划任务
          • @EnableAsync 用来开启异步注解功能
          • @ComponentScan 组件扫描
          • @Aspec 标注切面
          • @ControllerAdvice 统一处理异常
          • @ExceptionHandler 表示遇到这个异常就执行该方法
          • @Value 用于获取配置文件中的值
          • Mapping 定义路由映射
          • @RequestMapping 请求映射
          • @GetMapping GET路由请求
          • @PostMapping POST路由请求
          • @PutMapping PUT路由请求
          • @DeleteMapping DELETE路由请求
          • @PatchMapping PATCH路由请求
      • SpringBoot配置多环境
    • servlet

    • springboot

    • struts2基本使用及常见问题
    • Tomcat使用常见问题
    • IDEA基本使用
  • Go

  • 数据库

  • C#

  • ElasticSearch

  • Git

  • Gulp

  • Microsoft

  • Linux

  • 其它

  • 技术
  • java
  • spring
mrcdh
2022-07-25
目录

SpringBoot的常用注解

# 使用在类名上的注解

# @RestController 标明rest风格的控制器

使用位置:类名上

它用于返回json,xml等数据,但不能返回html页面,只提供Rest风格接口返回值,等同于@Controller + @ResponseBody

# @Controller 标明控制器

使用位置:类名上

针对需要返回页面的控制器使用该注解,需要配合视图解析器InternalResourceViewResolver, 如果需要返回xml、json或自定义枚举类型需要加上@ResponseBody

@Controller
public class TestController{}
1
2

# @Service 声明一个业务处理类

使用位置:类名上

它用于声明一个业务处理类(实现非接口类),用于标注服务层,处理业务层。

/**
 * Description: 标注为服务类
 */
@Service
public class ArticleServiceImpl implements ArticleService{
    @Autowired
    private ArticleRepository articleRepository;

    /** 重写service接口的实现,实现根据id查询对象功能 */
    @Override
    public Article findArticleById(long id){
        return articleRepository.findById(id);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# @Repository 声明数据库访问类

使用位置:类名上

# @Component

使用位置:类名上

它用于把普通POJO简单的java对象实例化到Spring容器中。当类不属于注解@Service、@Repository时,就可以使用注解@Component来标注这个类。它可以配合CommandLineRunner使用,一遍在程序启动后执行一些基础任务。

# @Configuration 声明此类是一个配置类,常与@Bean配合使用

使用位置:类名上

# @ConfigurationProperties 把同类配置信息自动封装成一个实体类

使用位置:类名上

personinfo:
  name: zhangdaobao
  age: 3
1
2
3

其属性 prefix 代表配置文件中配置项的前缀,如在配置文件中定义的 personinfo

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "personinfo")
public class GetPersonInfoProperties {
    private String name;
    private int age;

    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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

使用

@Autowired
private GetPersonInfoProperties getPersonInfoProperties;
@Test
public void getPerson(){
    System.out.println(getPersonInfoProperties.getName() + ",年龄:"+ getPersonInfoProperties.getAge());
}
1
2
3
4
5
6

解决idea提示springboot配置注解处理器(@ConfigurationProperties)没有找到

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
1
2
3
4
5

# @Resource 默认按 byName 自动注入

使用位置:类名上

# @Autowired 默认按 byType 自动注入

使用位置:类名上、属性或构造函数参数上

# RequestMapping 请求映射

使用位置:类名或方法上

如果用在类上,则表示所有响应请求的方法都是以该地址作为父路径的。该注解有6个属性:

  • Params: 指定Request中必须包含某些参数值,才让该方法处理。
  • Headers: 指定Request中必须包含魔偶写指定的header值,才能让该方法处理请求。
  • Value: 指定请求的实际地址,指定的地址可以是URI Template 模式。
  • Method: 指定请求的Method类型,如GET、POST、PUT、DELETE等。
  • Consumes: 指定处理请求的提交内容类型 Content-Type,如“application/json,text/html”。
  • Produces: 指定返回的内容类型。只有当Request请求头中的Accept类型中包含
@RestController
@RequestMapping("/v1/orders")
class ProductOrderController{}
1
2
3

# @Transactional 用于处理事务

使用位置:类名或方法上

它可以用在接口、接口方法、类及类方法上。但Spring不建议在接口或者接口方法上使用该注解,因为该注解只有在使用基于接口的代理时才会生效。如果异常被捕获(try/catch)了,则十五就不回滚了。如果想让事务回滚,则必须再往外抛出异常。

# @Qualifier 为Bean指定名称,随后在通过名字引用Bean

使用位置:类名或属性上

# 使用在方法上的注解

# @RequestBody 请求体

使用位置:方法参数前

他用来处理 json/xml 格式的数据。通过 @RequestBody 可以将请求体中的(json/xml)字符串绑定到相应的 Bean 上,也可以将其分别绑定到对应的字符串上。

@RequestMapping("/post") 
public void post(@RequestBody String name){}
1
2

# @PathVariable 用于获取路径中的参数

使用位置:方法参数前

# @Bean

使用位置:方法上

他代表产生一个 Bean ,并交给 Spring 管理。用于封装数据,一般有 Setter、 Getter 方法,MVC 模型中,对应的是M(模型)。

# @ResponseBody 标记响应体

使用位置:方法上

它的作用是通过转换器将控制器中方法返回的对象转换为指定的恪式,然后写入 Response 对象的 body 区,它常用来返回 JSON/XML 格式的数据。 使用此注解后,数据直接写入输入流中,不需要进行视图渲染。用法见以下代码:

@GetMapping("/test")
@ResponseBody
public String test(){
    return "test";
}
1
2
3
4
5

# 其他注解

# @EnableAutoConfiguration 用来提供自动配置

使用位置:入口类/类名上

# @SpringBootApplication 用来启动入口类Application

使用位置:入口类/类名上

# @EnableScheduling 用来开启计划任务

使用位置:入口类/类名上

Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。

# @EnableAsync 用来开启异步注解功能

使用位置:入口类/类名上

# @ComponentScan 组件扫描

使用位置:入口类/类名上

根据定义的扫描路径,把符合扫描规则的类装配到spring容器中

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan({"com.ruoyi.*", "com.v2.*"})
public class RuoYiApplication
{}
1
2
3
4

# @Aspec 标注切面

使用位置:入口类/类名上

可以用来配置事务、日志、权限验证,在用户请求时做一些处理等

# @ControllerAdvice 统一处理异常

使用位置:类名上

包含@Component,可以被扫描到。

# @ExceptionHandler 表示遇到这个异常就执行该方法

使用位置:方法上

# @Value 用于获取配置文件中的值

使用位置:属性上

@Value("${age}")
private int age;
1
2

application.yml

age: 12
1

# Mapping 定义路由映射

# @RequestMapping 请求映射

# @GetMapping GET路由请求

@RestController
@RequestMapping("/v1/orders")
class ProductOrderController{
    // 没有定义参数为默认访问/v1/orders
    @GetMapping
    public String getOrders() {
    }

    @GetMapping("{id}")
    public String getOrderById() {
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13

# @PostMapping POST路由请求

# @PutMapping PUT路由请求

# @DeleteMapping DELETE路由请求

# @PatchMapping PATCH路由请求

#SpringBoot
上次更新: 2023/09/22, 16:54:32
快速入门
SpringBoot配置多环境

← 快速入门 SpringBoot配置多环境→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×