logo
🌟 必须掌握的 Spring / Spring Boot 注解 – 想的个人网站
🌟 必须掌握的 Spring / Spring Boot 注解
本文最后更新于11 天前,其中的信息可能已经过时,如有错误请发送邮件到2327470875@qq.com

1. 启动类注解

@SpringBootApplication

  • 作用:标记项目启动类
  • 包含了 @Configuration + @EnableAutoConfiguration + @ComponentScan
  • 告诉 Spring Boot:这是启动入口,自动扫描同包及子包的 Bean
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2. 组件声明注解(把类交给容器管理)

这些注解都会把类注册为 Spring Bean

  • @Component:通用组件
  • @Service:业务逻辑层组件
  • @Repository:数据访问层(DAO)组件,自动处理数据库异常转换
  • @Controller:MVC 控制器(返回视图)
  • @RestController:REST 风格控制器,返回 JSON(= @Controller + @ResponseBody
@Service
public class UserService { }

3. 依赖注入注解

  • @Autowired
    • Spring 专用,按 类型 找 Bean 注入
    • 可配合 @Qualifier("beanName") 精确指定
  • @Resource
    • JDK 标准注解,优先按 名字 找 Bean,再按类型
    • 跨框架可用
@Autowired
private UserService userService;

@Resource
private OrderService orderService;

4. 配置相关注解

  • @Configuration
    • 表示类是配置类,可以定义 @Bean
  • @Bean
    • 方法返回的对象会交给 Spring 管理,成为 Bean
@Configuration
public class AppConfig {
    @Bean
    public UserService userService() {
        return new UserService();
    }
}

5. 请求处理注解(Web 层)

这些注解用在 Controller 方法上,定义接口:

  • @RequestMapping("/path"):通用映射(可指定 GET、POST 等)
  • @GetMapping("/users"):GET 请求
  • @PostMapping("/users"):POST 请求
  • @PutMapping("/users/{id}"):PUT 请求
  • @DeleteMapping("/users/{id}"):DELETE 请求
  • @RequestParam:获取 URL 参数 ?name=xxx
  • @PathVariable:获取路径参数 /users/{id}
  • @RequestBody:获取请求体 JSON → 对象
@RestController
@RequestMapping("/api")
public class UserController {

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        return "登录用户:" + user.getUsername();
    }

    @GetMapping("/user/{id}")
    public String getUser(@PathVariable Long id,
                          @RequestParam String type) {
        return "用户ID=" + id + ",类型=" + type;
    }
}

6. 事务注解

  • @Transactional
    • 作用:方法出错时自动回滚事务
    • 一般用于 Service 层数据库操作
@Transactional
public void createOrder() {
    // 插入订单
    // 减库存
    // 出错时会自动回滚
}

7. 参数校验相关(常和 JSR-303 一起用)

  • @Valid:启用参数校验
  • @NotNull@Size@Email
@PostMapping("/register")
public String register(@Valid @RequestBody User user) {
    return "注册成功";
}

✅ 总结:必须掌握的注解清单

  1. 入口@SpringBootApplication
  2. 组件声明@Component@Service@Repository@Controller@RestController
  3. 依赖注入@Autowired@Resource@Qualifier
  4. 配置类@Configuration@Bean
  5. Web 接口@RequestMapping@GetMapping@PostMapping@PutMapping@DeleteMapping@RequestParam@PathVariable@RequestBody
  6. 事务@Transactional
  7. 校验@Valid@NotNull
文末附加内容
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇