chengaofeng
发布于 2024-09-06 / 23 阅读
0
0

Java注解知识点枚举

  1. @Override

  2. @Deprecated

  3. @SuppressWarnings

  4. @SafeVarargs

  5. @FunctionalInterface

  6. @Retention

  7. @Target

  8. @Inherited

  9. @Documented

  10. @Repeatable

  11. @Native

  12. @PostConstruct

  13. @PreDestroy

  14. @Resource

  15. @Autowired

  16. @Qualifier

  17. @Value

  18. @Component

  19. @Service

  20. @Repository

  21. @Controller

  22. @RestController

  23. @RequestMapping

  24. @GetMapping

  25. @PostMapping

  26. @PutMapping

  27. @DeleteMapping

  28. @PatchMapping

  29. @RequestParam

  30. @PathVariable

  31. @RequestBody

  32. @ResponseBody

  33. @ResponseStatus

  34. @ExceptionHandler

  35. @ControllerAdvice

  36. @Configuration

  37. @Bean

  38. @Scope

  39. @Primary

  40. @Lazy

  41. @Profile

  42. @EnableAutoConfiguration

  43. @SpringBootApplication

  44. @Entity

  45. @Table

  46. @Id

  47. @GeneratedValue

  48. @Column

  49. @OneToOne

  50. @OneToMany

  51. @ManyToOne

  52. @ManyToMany

  53. @JoinColumn

  54. @JoinTable

  55. @MappedSuperclass

  56. @Embeddable

  57. @Embedded

  58. @Transient

  59. @JsonProperty

  60. @JsonIgnore

  61. @JsonInclude

  62. @JsonFormat

  63. @JsonCreator

  64. @JsonValue

  65. @JsonAlias

  66. @JsonTypeInfo

  67. @JsonSubTypes

  68. @JsonTypeName

  69. @JsonAutoDetect

  70. @JsonUnwrapped

  71. @JsonManagedReference

  72. @JsonBackReference

  73. @JsonIdentityInfo

  74. @JsonFilter

  75. @JsonRootName

  76. @JsonView

  77. @JsonDeserialize

  78. @JsonSerialize

  79. @JsonAppend

  80. @JsonMerge

解释

  1. @Override

    • 用法:用于标注方法,表示该方法重写了父类的方法。

    • 示例

      @Override
      
      public String toString() {
      
          return "MyClass";
      
      }
  2. @Deprecated

    • 用法:用于标注不推荐使用的类、方法或字段,表示它们可能在未来版本中被移除。

    • 示例

      @Deprecated
      
      public void oldMethod() {
      
          // ...
      
      }
  3. @SuppressWarnings

    • 用法:用于抑制编译器警告。

    • 示例

      @SuppressWarnings("unchecked")
      
      public void myMethod() {
      
          // ...
      
      }
  4. @SafeVarargs

    • 用法:用于标注不对其可变参数进行不安全操作的方法或构造函数。

    • 示例

      @SafeVarargs
      
      public final void myMethod(List<String>... lists) {
      
          // ...
      
      }
  5. @FunctionalInterface

    • 用法:用于标注接口,表示该接口是一个函数式接口(即只有一个抽象方法)。

    • 示例

      @FunctionalInterface
      
      public interface MyFunctionalInterface {
      
          void execute();
      
      }
  6. @Retention

    • 用法:用于标注注解,表示该注解的保留策略(SOURCE, CLASS, RUNTIME)。

    • 示例

      @Retention(RetentionPolicy.RUNTIME)
      
      public @interface MyAnnotation {
      
          // ...
      
      }
  7. @Target

    • 用法:用于标注注解,表示该注解可以应用的程序元素(如METHOD, FIELD, TYPE)。

    • 示例

      @Target(ElementType.METHOD)
      
      public @interface MyAnnotation {
      
          // ...
      
      }
  8. @Inherited

    • 用法:用于标注注解,表示该注解可以被子类继承。

    • 示例

      @Inherited
      
      @Retention(RetentionPolicy.RUNTIME)
      
      @Target(ElementType.TYPE)
      
      public @interface MyAnnotation {
      
          // ...
      
      }
  9. @Documented

    • 用法:用于标注注解,表示该注解将包含在Javadoc中。

    • 示例

      @Documented
      
      @Retention(RetentionPolicy.RUNTIME)
      
      @Target(ElementType.METHOD)
      
      public @interface MyAnnotation {
      
          // ...
      
      }
  10. @Repeatable

    • 用法:用于标注注解,表示该注解可以在同一元素上多次使用。

    • 示例

      @Repeatable(MyAnnotations.class)
      
      @Retention(RetentionPolicy.RUNTIME)
      
      @Target(ElementType.METHOD)
      
      public @interface MyAnnotation {
      
          String value();
      
      }
      
      @Retention(RetentionPolicy.RUNTIME)
      
      @Target(ElementType.METHOD)
      
      public @interface MyAnnotations {
      
          MyAnnotation[] value();
      
      }
  11. @PostConstruct

    • 用法:用于标注方法,表示该方法应在依赖注入完成后立即调用。

    • 示例

      @PostConstruct
      
      public void init() {
      
          // ...
      
      }
  12. @PreDestroy

    • 用法:用于标注方法,表示该方法应在容器销毁之前调用。

    • 示例

      @PreDestroy
      
      public void cleanup() {
      
          // ...
      
      }
  13. @Resource

    • 用法:用于标注字段或方法,表示该字段或方法需要依赖注入。

    • 示例

      @Resource(name = "myBean")
      
      private MyBean myBean;
  14. @Autowired

    • 用法:用于标注字段、构造函数或方法,表示需要自动注入依赖。

    • 示例

      @Autowired
      
      private MyBean myBean;
  15. @Qualifier

    • 用法:用于标注字段或参数,结合@Autowired使用,以指定注入的具体实现。

    • 示例

      @Autowired
      
      @Qualifier("specificBean")
      
      private MyBean myBean;
  16. @Value

    • 用法:用于标注字段或方法参数,表示需要注入配置文件中的值。

    • 示例

      @Value("${my.property}")
      
      private String myProperty;
  17. @Component

    • 用法:用于标注类,表示该类是一个Spring组件。

    • 示例

      @Component
      
      public class MyComponent {
      
          // ...
      
      }
  18. @Service

    • 用法:用于标注类,表示该类是一个服务层组件。

    • 示例

      @Service
      
      public class MyService {
      
          // ...
      
      }
  19. @Repository

    • 用法:用于标注类,表示该类是一个数据访问层组件。

    • 示例

      @Repository
      
      public class MyRepository {
      
          // ...
      
      }
  20. @Controller

    • 用法:用于标注类,表示该类是一个Spring MVC控制器。

    • 示例

      @Controller
      
      public class MyController {
      
          // ...
      
      }
  21. @RestController

    • 用法:用于标注类,表示该类是一个RESTful控制器,结合@Controller和@ResponseBody。

    • 示例

      @RestController
      
      public class MyRestController {
      
          // ...
      
      }
  22. @RequestMapping

    • 用法:用于标注方法或类,映射HTTP请求到处理方法。

    • 示例

      @RequestMapping("/path")
      
      public String handleRequest() {
      
          return "response";
      
      }
  23. @GetMapping

    • 用法:用于标注方法,映射HTTP GET请求到处理方法。

    • 示例

      @GetMapping("/path")
      
      public String handleGetRequest() {
      
          return "response";
      
      }
  24. @PostMapping

    • 用法:用于标注方法,映射HTTP POST请求到处理方法。

    • 示例

      @PostMapping("/path")
      
      public String handlePostRequest() {
      
          return "response";
      
      }
  25. @PutMapping

    • 用法:用于标注方法,映射HTTP PUT请求到处理方法。

    • 示例

      @PutMapping("/path")
      
      public String handlePutRequest() {
      
          return "response";
      
      }
  26. @DeleteMapping

    • 用法:用于标注方法,映射HTTP DELETE请求到处理方法。

    • 示例

      @DeleteMapping("/path")
      
      public String handleDeleteRequest() {
      
          return "response";
      
      }
  27. @PatchMapping

    • 用法:用于标注方法,映射HTTP PATCH请求到处理方法。

    • 示例

      @PatchMapping("/path")
      
      public String handlePatchRequest() {
      
          return "response";
      
      }
  28. @RequestParam

    • 用法:用于标注方法参数,绑定HTTP请求参数到方法参数。

    • 示例

      @RequestMapping("/path")
      
      public String handleRequest(@RequestParam("param") String param) {
      
          return "response";
      
      }
  29. @PathVariable

    • 用法:用于标注方法参数,绑定URI模板变量到方法参数。

    • 示例

      @RequestMapping("/path/{id}")
      
      public String handleRequest(@PathVariable("id") String id) {
      
          return "response";
      
      }
  30. @RequestBody

    • 用法:用于标注方法参数,绑定HTTP请求体到方法参数。

    • 示例

      @PostMapping("/path")
      
      public String handleRequest(@RequestBody MyRequestBody body) {
      
          return "response";
      
      }
  31. @ResponseBody

    • 用法:用于标注方法,表示返回值应直接写入HTTP响应体。

    • 示例

      @RequestMapping("/path")
      
      @ResponseBody
      
      public String handleRequest() {
      
          return "response";
      
      }
  32. @ResponseStatus

    • 用法:用于标注方法或类,设置HTTP响应状态码。

    • 示例

      @ResponseStatus(HttpStatus.CREATED)
      
      public void handleRequest() {
      
          // ...
      
      }
  33. @ExceptionHandler

    • 用法:用于标注方法,处理特定类型的异常。

    • 示例

      @ExceptionHandler(Exception.class)
      
      public String handleException(Exception e) {
      
          return "error";
      
      }
  34. @ControllerAdvice

    • 用法:用于标注类,集中处理控制器中的异常。

    • 示例

      @ControllerAdvice
      
      public class GlobalExceptionHandler {
      
          @ExceptionHandler(Exception.class)
      
          public String handleException(Exception e) {
      
              return "error";
      
          }
      
      }
  35. @Configuration

    • 用法:用于标注类,表示该类是一个Spring配置类。

    • 示例

      @Configuration
      
      public class AppConfig {
      
          // ...
      
      }
  36. @Bean

    • 用法:用于标注方法,定义一个Spring Bean。

    • 示例

      @Configuration
      
      public class AppConfig {
      
          @Bean
      
          public MyBean myBean() {
      
              return new MyBean();
      
          }
      
      }
  37. @Scope

    • 用法:用于标注Bean,定义Bean的作用域。

    • 示例

      @Bean
      
      @Scope("prototype")
      
      public MyBean myBean() {
      
          return new MyBean();
      
      }
  38. @Primary

    • 用法:用于标注Bean,表示该Bean在自动注入时优先被考虑。

    • 示例

      @Bean
      
      @Primary
      
      public MyBean myPrimaryBean() {
      
          return new MyBean();
      
      }
  39. @Lazy

    • 用法:用于标注Bean,表示该Bean在第一次使用时才被初始化。

    • 示例

      @Bean
      
      @Lazy
      
      public MyBean myLazyBean() {
      
          return new MyBean();
      
      }
  40. @Profile

    • 用法:用于标注Bean,表示该Bean在特定的环境配置下才会被创建。

    • 示例

      @Bean
      
      @Profile("dev")
      
      public MyBean myDevBean() {
      
          return new MyBean();
      
      }
  41. @EnableAutoConfiguration

    • 用法:用于标注类,启用Spring Boot的自动配置功能。

    • 示例

      @EnableAutoConfiguration
      
      public class MyApplication {
      
          // ...
      
      }
  42. @SpringBootApplication

    • 用法:用于标注类,组合了@Configuration、@EnableAutoConfiguration和@ComponentScan注解。

    • 示例

      @SpringBootApplication
      
      public class MyApplication {
      
          public static void main(String[] args) {
      
              SpringApplication.run(MyApplication.class, args);
      
          }
      
      }
  43. @Entity

    • 用法:用于标注类,表示该类是一个JPA实体。

    • 示例

      @Entity
      
      public class MyEntity {
      
          // ...
      
      }
  44. @Table

    • 用法:用于标注实体类,指定对应数据库表的信息。

    • 示例

      @Entity
      
      @Table(name = "my_table")
      
      public class MyEntity {
      
          // ...
      
      }
  45. @Id

    • 用法:用于标注字段,表示该字段是实体的主键。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @Id
      
          private Long id;
      
          // ...
      
      }
  46. @GeneratedValue

    • 用法:用于标注主键字段,表示该主键的生成策略。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @Id
      
          @GeneratedValue(strategy = GenerationType.AUTO)
      
          private Long id;
      
          // ...
      
      }
  47. @Column

    • 用法:用于标注字段,指定对应数据库列的信息。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @Column(name = "my_column")
      
          private String myField;
      
          // ...
      
      }
  48. @OneToOne

    • 用法:用于标注字段,表示一对一的实体关系。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @OneToOne
      
          private AnotherEntity anotherEntity;
      
          // ...
      
      }
  49. @OneToMany

    • 用法:用于标注字段,表示一对多的实体关系。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @OneToMany
      
          private List<AnotherEntity> anotherEntities;
      
          // ...
      
      }
  50. @ManyToOne

    • 用法:用于标注字段,表示多对一的实体关系。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @ManyToOne
      
          private AnotherEntity anotherEntity;
      
          // ...
      
      }
  51. @ManyToMany

    • 用法:用于标注字段,表示多对多的实体关系。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @ManyToMany
      
          private List<AnotherEntity> anotherEntities;
      
          // ...
      
      }
  52. @JoinColumn

    • 用法:用于标注字段,指定外键列的信息。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @ManyToOne
      
          @JoinColumn(name = "another_entity_id")
      
          private AnotherEntity anotherEntity;
      
          // ...
      
      }
  53. @JoinTable

    • 用法:用于标注字段,指定多对多关系的连接表信息。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @ManyToMany
      
          @JoinTable(
      
              name = "my_entity_another_entity",
      
              joinColumns = @JoinColumn(name = "my_entity_id"),
      
              inverseJoinColumns = @JoinColumn(name = "another_entity_id")
      
          )
      
          private List<AnotherEntity> anotherEntities;
      
          // ...
      
      }
  54. @MappedSuperclass

    • 用法:用于标注类,表示该类是一个JPA映射超类,不能直接映射到数据库表。

    • 示例

      @MappedSuperclass
      
      public abstract class BaseEntity {
      
          @Id
      
          @GeneratedValue(strategy = GenerationType.AUTO)
      
          private Long id;
      
          // ...
      
      }
  55. @Embeddable

    • 用法:用于标注类,表示该类可以嵌入到其他实体中。

    • 示例

      @Embeddable
      
      public class Address {
      
          private String street;
      
          private String city;
      
          // ...
      
      }
  56. @Embedded

    • 用法:用于标注字段,表示该字段是一个嵌入的对象。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @Embedded
      
          private Address address;
      
          // ...
      
      }
  57. @Transient

    • 用法:用于标注字段,表示该字段不应持久化到数据库。

    • 示例

      @Entity
      
      public class MyEntity {
      
          @Transient
      
          private String tempData;
      
          // ...
      
      }


评论