以下是Java后端开发中50个最核心注解的详细整理,涵盖Spring、Spring Boot、JPA、MyBatis等主流框架,包含源码片段核心作用解释

一、Spring核心注解(12个)

1. @Component

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
    String value() default "";
}

作用:通用组件注解,标识类为Spring容器管理的Bean。

2. @Repository

@Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Repository {
    String value() default "";
}

作用:数据访问层(DAO)专用注解,继承自@Component

3. @Service

@Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Service {
    String value() default "";
}

作用:业务逻辑层专用注解,继承自@Component

4. @Controller

@Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Controller {
    String value() default "";
}

作用:Web控制层专用注解,继承自@Component

5. @Autowired

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}

作用:自动装配Bean,默认按类型注入。

6. @Qualifier

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
    String value() default "";
}

作用:配合@Autowired,按名称注入Bean。

7. @Configuration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    String value() default "";
}

作用:标识配置类,替代XML配置文件。

8. @Bean

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
    String[] name() default {};
    boolean autowireCandidate() default true;
}

作用:在配置类中定义Bean,方法返回值为Bean实例。

9. @Scope

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
    String value() default "singleton";
}

作用:定义Bean的作用域(singleton/prototype/request/session等)。

10. @PostConstruct

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PostConstruct {
}

作用:Bean初始化后执行的方法(JSR-250标准)。

11. @PreDestroy

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PreDestroy {
}

作用:Bean销毁前执行的方法(JSR-250标准)。

12. @Primary

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Primary {
}

作用:当多个同类型Bean存在时,优先注入标注@Primary的Bean。

二、Spring Web注解(10个)

13. @RestController

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    String value() default "";
}

作用:组合注解,相当于@Controller + @ResponseBody,返回JSON/XML。

14. @RequestMapping

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String[] value() default {};
    RequestMethod[] method() default {};
}

作用:映射HTTP请求到控制器方法,支持GET/POST等。

15. @GetMapping

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
    String[] value() default {};
}

作用@RequestMapping(method = RequestMethod.GET)的简写。

16. @PostMapping

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
    String[] value() default {};
}

作用@RequestMapping(method = RequestMethod.POST)的简写。

17. @RequestParam

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    String value() default "";
    boolean required() default true;
}

作用:绑定请求参数到方法参数。

18. @PathVariable

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
    String value() default "";
}

作用:绑定URL路径变量到方法参数。

19. @RequestBody

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
    boolean required() default true;
}

作用:将HTTP请求体绑定到方法参数(JSON转对象)。

20. @ResponseBody

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}

作用:将方法返回值直接写入HTTP响应体(对象转JSON)。

21. @ExceptionHandler

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {
    Class<? extends Throwable>[] value() default {};
}

作用:全局异常处理,捕获指定异常并处理。

22. @ResponseStatus

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseStatus {
    HttpStatus value() default HttpStatus.INTERNAL_SERVER_ERROR;
}

作用:设置HTTP响应状态码。

三、Spring Data JPA注解(10个)

23. @Entity

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Entity {
    String name() default "";
}

作用:标识实体类,与数据库表映射。

24. @Table

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Table {
    String name() default "";
    String schema() default "";
}

作用:指定实体类对应的数据库表名。

25. @Id

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Id {
}

作用:标识实体类的主键字段。

26. @GeneratedValue

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GeneratedValue {
    GenerationType strategy() default GenerationType.AUTO;
}

作用:定义主键生成策略(AUTO/IDENTITY/SEQUENCE/TABLE)。

27. @Column

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
    String name() default "";
    boolean nullable() default true;
    int length() default 255;
}

作用:定义字段对应的数据库列属性。

28. @Transient

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Transient {
}

作用:标识字段不与数据库列映射。

29. @ManyToOne

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ManyToOne {
    FetchType fetch() default FetchType.EAGER;
}

作用:定义多对一关联关系。

30. @OneToMany

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OneToMany {
    FetchType fetch() default FetchType.LAZY;
    String mappedBy() default "";
}

作用:定义一对多关联关系。

31. @Transactional

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Transactional {
    Propagation propagation() default Propagation.REQUIRED;
    Isolation isolation() default Isolation.DEFAULT;
    boolean readOnly() default false;
}

作用:声明事务,支持传播行为、隔离级别等配置。

32. @Query

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Query {
    String value() default "";
    boolean nativeQuery() default false;
}

作用:自定义JPQL或SQL查询语句。

四、Spring Boot注解(5个)

33. @SpringBootApplication

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
    String[] exclude() default {};
}

作用:组合注解,标识Spring Boot启动类(包含自动配置、组件扫描)。

34. @EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    String[] exclude() default {};
}

作用:启用Spring Boot自动配置机制。

35. @ConfigurationProperties

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
    String prefix() default "";
}

作用:绑定配置文件属性到Java对象。

36. @ConditionalOnProperty

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
    String[] value() default {};
    String havingValue() default "";
    boolean matchIfMissing() default false;
}

作用:条件注解,当配置文件存在指定属性时生效。

37. @Value

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
    String value();
}

作用:注入配置文件中的单个属性值。

五、MyBatis注解(5个)

38. @Mapper

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Mapper {
}

作用:标识MyBatis Mapper接口,自动生成代理实现类。

39. @Select

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Select {
    String[] value();
}

作用:定义查询SQL语句。

40. @Insert

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Insert {
    String[] value();
}

作用:定义插入SQL语句。

41. @Update

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Update {
    String[] value();
}

作用:定义更新SQL语句。

42. @Delete

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Delete {
    String[] value();
}

作用:定义删除SQL语句。

六、Java原生与其他注解(8个)

43. @Override

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

作用:标记方法重写父类方法(编译时检查)。

44. @Deprecated

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ...})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Deprecated {
}

作用:标记已过时的类、方法或字段。

45. @SuppressWarnings

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ...})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

作用:抑制编译器警告。

46. @FunctionalInterface

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FunctionalInterface {
}

作用:标记函数式接口(仅一个抽象方法)。

47. @Valid

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ...})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Valid {
}

作用:启用对象参数验证(JSR-380标准)。

48. @NotNull

@Target({ElementType.FIELD, ElementType.PARAMETER, ...})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NotNull {
    String message() default "must not be null";
}

作用:验证字段或参数不为null。

49. @Size

@Target({ElementType.FIELD, ElementType.PARAMETER, ...})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Size {
    int min() default 0;
    int max() default Integer.MAX_VALUE;
}

作用:验证字符串、集合或数组的大小范围。

50. @Async

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async {
    String value() default "";
}

作用:标记方法为异步执行,需配合@EnableAsync使用。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐