1.核心配置注解
- @SpringBootApplication
- 启动类核心注解
- 组合了 @Configuration(配置类)、@EnableAutoConfiguration(自动配置)和 @ComponentScan(组件扫描)
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
- | @Configuration | 标记类为配置类,可定义 Bean |
@Configuration
public class AppConfig {@Beanpublic DataSource dataSource() { /* ... */ }
}
- | @Bean | 声明方法返回的对象由 Spring 管理 |
@Bean(name = "myBean")
public MyBean myBean() {return new MyBean();
}
- | @Value | 注入配置文件中的值 |
@Value("${server.port}")
private int port;
- | @ConfigurationProperties | 批量绑定配置文件属性到对象 |
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {private String host;private int port;// Getter/Setter
}
2.Web开发注解
- @RestController:组合 @Controller 和 @ResponseBody,直接返回 JSON/XML 数据
@RestController
@RequestMapping("/api")
public class UserController {@GetMapping("/users")public List<User> getUsers() { /* ... */ }
}
- | @RequestMapping | 映射 HTTP 请求到方法 |
@RequestMapping(value = "/user", method = RequestMethod.GET)
- | @GetMapping / @PostMapping | 简化版请求映射(GET/POST 等) |
@PostMapping("/create")
public ResponseEntity createUser(@RequestBody User user) { /* ... */ }
- | @PathVariable | 获取 URL 路径参数 |
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) { /* ... */ }
- | @RequestParam | 获取 URL 查询参数 |
@GetMapping("/search")
// http://localhost:8080/search?keyword=xxx
public List<User> search(@RequestParam String keyword) { /* ... */ }
- | @ResponseBody | 将方法返回值序列化为 HTTP 响应体 |
@ResponseBody
@GetMapping("/info")
public User getUserInfo() { /* ... */ }
3.依赖注入与组件扫描
- @Component:通用组件注解
@Component
public class MyComponent { /* ... */ }
- | @Service | 标记服务层组件(业务逻辑) |
@Service
public class UserService { /* ... */ }
- | @Repository / @Mapper | 标记数据访问层组件(DAO) |
@Repository // @Mapper
public class UserRepository { /* ... */ }
- | @Autowired | 自动注入 Bean(按类型) |、| @Resource| 自动注入 Bean(按名称) |
@Autowired //@Resource
private UserService userService;
- | @Qualifier | 按名称指定注入的 Bean |
@Autowired
@Qualifier("mysqlDataSource")
private DataSource dataSource;
- | @Primary | 当多个同类型 Bean 存在时,优先注入 |
@Bean
@Primary
public DataSource primaryDataSource() { /* ... */ }
4.条件化配置注解
- @ConditionalOnProperty:当配置属性存在时生效
@Bean
@ConditionalOnProperty(name = "cache.enabled", havingValue = "true")
public CacheService cacheService() { /* ... */ }
- | @ConditionalOnClass | 当类路径存在指定类时生效 |
@Configuration
@ConditionalOnClass(RedisTemplate.class)
public class RedisAutoConfig { /* ... */ }
- | @ConditionalOnMissingBean | 当容器中不存在指定 Bean 时生效 |
@Bean
@ConditionalOnMissingBean
public DataSource defaultDataSource() { /* ... */ }
5.其他常用注解
- @Transactional:声明事务管理
@Transactional
public void transferMoney() { /* ... */ }
- | @Scheduled | 定时任务 |
@Scheduled(fixedRate = 5000)
public void refreshCache() { /* ... */ }
- | @Async | 异步方法调用 |
@Async
public void asyncProcess() { /* ... */ }
- | @CrossOrigin | 跨域请求支持 |
@CrossOrigin(origins = "http://example.com")
@RestController
public class MyController { /* ... */ }
总结
- 核心配置:@SpringBootApplication, @Configuration, @Bean
- Web 开发:@RestController, @GetMapping, @PostMapping, @PathVariable
- 依赖注入:@Autowired, @Qualifier, @Component
- 条件配置:@ConditionalOnProperty, @ConditionalOnClass
- 高级功能:@Transactional, @Scheduled, @Async
注:更多注解及使用细节可参考(SpringBoot官网文档)