一文掌握 Spring Boot 中最常用的 20 个注解,涵盖开发、配置、Web、数据库、测试等场景,配合示例讲解,一站式掌握!
@SpringBootApplication
@Configuration
、@EnableAutoConfiguration
和 @ComponentScan
。@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@Component
@Component
public class MyUtil {
// ...
}
@Service
@Component
。@Service
public class UserService {
// ...
}
@Repository
@Repository
public class UserDao {
// ...
}
@Controller
@RequestMapping
、@GetMapping
等使用。@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "index";
}
}
@RestController
@Controller + @ResponseBody
,返回 JSON 数据。@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public List<User> getUsers() {
return userService.getAll();
}
}
@RequestMapping
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User getUser() {
return user;
}
@GetMapping
/ @PostMapping
/ @PutMapping
/ @DeleteMapping
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return userService.find(id);
}
@Autowired
@Autowired
private UserService userService;
@Qualifier
@Autowired
使用。@Autowired
@Qualifier("myImpl")
private MyService service;
@RequestParam
@GetMapping("/search")
public List<User> search(@RequestParam String keyword) {
return userService.search(keyword);
}
@PathVariable
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
@RequestBody
@PostMapping("/user")
public void saveUser(@RequestBody User user) {
userService.save(user);
}
@Value
application.yml
或 .properties
中的配置项。@Value("${server.port}")
private int port;
@Configuration
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
@Bean
@Configuration
使用。@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@SpringBootTest
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
}
@MockBean
@MockBean
private UserRepository userRepository;
@Valid
/ @Validated
@NotNull
、@Size
等使用。@PostMapping("/register")
public void register(@Valid @RequestBody User user) {
// ...
}
@Transactional
rollbackFor
, readOnly
, propagation
。@Transactional(rollbackFor = Exception.class)
public void saveData() {
// ...
}
注解 | 类型 | 场景 | 特点 |
---|---|---|---|
@SpringBootApplication |
启动注解 | 启动类 | 三合一复合注解 |
@Component |
通用组件 | 工具类等 | 可被扫描 |
@Service |
业务组件 | Service 层 | 本质是 @Component |
@Repository |
DAO组件 | 数据访问 | 自动异常封装 |
@Controller |
Web 控制器 | 页面跳转 | 搭配模板引擎 |
@RestController |
API 控制器 | 返回 JSON | = @Controller + @ResponseBody |
@Autowired |
注入 | 自动注入 | 构造/字段均可 |
@RequestMapping |
请求映射 | 多 HTTP 方法 | 可用于类/方法 |
@GetMapping 等 |
请求映射 | 精准 HTTP 方法 | 推荐使用 |
@Value |
配置读取 | yml、prop 值注入 | 字符串解析 |
@Transactional |
事务管理 | Service 层 | 原子性保障 |
@Bean |
手动注册 | 第三方类 | 自定义 Bean |
@Valid / @Validated |
参数校验 | 表单、实体 | 搭配校验注解 |
@SpringBootTest |
测试注解 | 单元/集成测试 | 提供完整容器 |
@MockBean |
测试 Mock | 替换 Bean | 使用 Mockito |
建议收藏:这些注解是你开发 Spring Boot 项目的“基本功”,越熟练越高效!
如果你觉得有帮助,别忘了点赞、收藏和分享~