@Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
@Retention
@Target
@Inherited
@Documented
@Repeatable
@Native
@PostConstruct
@PreDestroy
@Resource
@Autowired
@Qualifier
@Value
@Component
@Service
@Repository
@Controller
@RestController
@RequestMapping
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@RequestParam
@PathVariable
@RequestBody
@ResponseBody
@ResponseStatus
@ExceptionHandler
@ControllerAdvice
@Configuration
@Bean
@Scope
@Primary
@Lazy
@Profile
@EnableAutoConfiguration
@SpringBootApplication
@Entity
@Table
@Id
@GeneratedValue
@Column
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
@JoinColumn
@JoinTable
@MappedSuperclass
@Embeddable
@Embedded
@Transient
@JsonProperty
@JsonIgnore
@JsonInclude
@JsonFormat
@JsonCreator
@JsonValue
@JsonAlias
@JsonTypeInfo
@JsonSubTypes
@JsonTypeName
@JsonAutoDetect
@JsonUnwrapped
@JsonManagedReference
@JsonBackReference
@JsonIdentityInfo
@JsonFilter
@JsonRootName
@JsonView
@JsonDeserialize
@JsonSerialize
@JsonAppend
@JsonMerge
解释
@Override:
用法:用于标注方法,表示该方法重写了父类的方法。
示例:
@Override public String toString() { return "MyClass"; }
@Deprecated:
用法:用于标注不推荐使用的类、方法或字段,表示它们可能在未来版本中被移除。
示例:
@Deprecated public void oldMethod() { // ... }
@SuppressWarnings:
用法:用于抑制编译器警告。
示例:
@SuppressWarnings("unchecked") public void myMethod() { // ... }
@SafeVarargs:
用法:用于标注不对其可变参数进行不安全操作的方法或构造函数。
示例:
@SafeVarargs public final void myMethod(List<String>... lists) { // ... }
@FunctionalInterface:
用法:用于标注接口,表示该接口是一个函数式接口(即只有一个抽象方法)。
示例:
@FunctionalInterface public interface MyFunctionalInterface { void execute(); }
@Retention:
用法:用于标注注解,表示该注解的保留策略(SOURCE, CLASS, RUNTIME)。
示例:
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { // ... }
@Target:
用法:用于标注注解,表示该注解可以应用的程序元素(如METHOD, FIELD, TYPE)。
示例:
@Target(ElementType.METHOD) public @interface MyAnnotation { // ... }
@Inherited:
用法:用于标注注解,表示该注解可以被子类继承。
示例:
@Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { // ... }
@Documented:
用法:用于标注注解,表示该注解将包含在Javadoc中。
示例:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { // ... }
@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(); }
@PostConstruct:
用法:用于标注方法,表示该方法应在依赖注入完成后立即调用。
示例:
@PostConstruct public void init() { // ... }
@PreDestroy:
用法:用于标注方法,表示该方法应在容器销毁之前调用。
示例:
@PreDestroy public void cleanup() { // ... }
@Resource:
用法:用于标注字段或方法,表示该字段或方法需要依赖注入。
示例:
@Resource(name = "myBean") private MyBean myBean;
@Autowired:
用法:用于标注字段、构造函数或方法,表示需要自动注入依赖。
示例:
@Autowired private MyBean myBean;
@Qualifier:
用法:用于标注字段或参数,结合@Autowired使用,以指定注入的具体实现。
示例:
@Autowired @Qualifier("specificBean") private MyBean myBean;
@Value:
用法:用于标注字段或方法参数,表示需要注入配置文件中的值。
示例:
@Value("${my.property}") private String myProperty;
@Component:
用法:用于标注类,表示该类是一个Spring组件。
示例:
@Component public class MyComponent { // ... }
@Service:
用法:用于标注类,表示该类是一个服务层组件。
示例:
@Service public class MyService { // ... }
@Repository:
用法:用于标注类,表示该类是一个数据访问层组件。
示例:
@Repository public class MyRepository { // ... }
@Controller:
用法:用于标注类,表示该类是一个Spring MVC控制器。
示例:
@Controller public class MyController { // ... }
@RestController:
用法:用于标注类,表示该类是一个RESTful控制器,结合@Controller和@ResponseBody。
示例:
@RestController public class MyRestController { // ... }
@RequestMapping:
用法:用于标注方法或类,映射HTTP请求到处理方法。
示例:
@RequestMapping("/path") public String handleRequest() { return "response"; }
@GetMapping:
用法:用于标注方法,映射HTTP GET请求到处理方法。
示例:
@GetMapping("/path") public String handleGetRequest() { return "response"; }
@PostMapping:
用法:用于标注方法,映射HTTP POST请求到处理方法。
示例:
@PostMapping("/path") public String handlePostRequest() { return "response"; }
@PutMapping:
用法:用于标注方法,映射HTTP PUT请求到处理方法。
示例:
@PutMapping("/path") public String handlePutRequest() { return "response"; }
@DeleteMapping:
用法:用于标注方法,映射HTTP DELETE请求到处理方法。
示例:
@DeleteMapping("/path") public String handleDeleteRequest() { return "response"; }
@PatchMapping:
用法:用于标注方法,映射HTTP PATCH请求到处理方法。
示例:
@PatchMapping("/path") public String handlePatchRequest() { return "response"; }
@RequestParam:
用法:用于标注方法参数,绑定HTTP请求参数到方法参数。
示例:
@RequestMapping("/path") public String handleRequest(@RequestParam("param") String param) { return "response"; }
@PathVariable:
用法:用于标注方法参数,绑定URI模板变量到方法参数。
示例:
@RequestMapping("/path/{id}") public String handleRequest(@PathVariable("id") String id) { return "response"; }
@RequestBody:
用法:用于标注方法参数,绑定HTTP请求体到方法参数。
示例:
@PostMapping("/path") public String handleRequest(@RequestBody MyRequestBody body) { return "response"; }
@ResponseBody:
用法:用于标注方法,表示返回值应直接写入HTTP响应体。
示例:
@RequestMapping("/path") @ResponseBody public String handleRequest() { return "response"; }
@ResponseStatus:
用法:用于标注方法或类,设置HTTP响应状态码。
示例:
@ResponseStatus(HttpStatus.CREATED) public void handleRequest() { // ... }
@ExceptionHandler:
用法:用于标注方法,处理特定类型的异常。
示例:
@ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; }
@ControllerAdvice:
用法:用于标注类,集中处理控制器中的异常。
示例:
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public String handleException(Exception e) { return "error"; } }
@Configuration:
用法:用于标注类,表示该类是一个Spring配置类。
示例:
@Configuration public class AppConfig { // ... }
@Bean:
用法:用于标注方法,定义一个Spring Bean。
示例:
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
@Scope:
用法:用于标注Bean,定义Bean的作用域。
示例:
@Bean @Scope("prototype") public MyBean myBean() { return new MyBean(); }
@Primary:
用法:用于标注Bean,表示该Bean在自动注入时优先被考虑。
示例:
@Bean @Primary public MyBean myPrimaryBean() { return new MyBean(); }
@Lazy:
用法:用于标注Bean,表示该Bean在第一次使用时才被初始化。
示例:
@Bean @Lazy public MyBean myLazyBean() { return new MyBean(); }
@Profile:
用法:用于标注Bean,表示该Bean在特定的环境配置下才会被创建。
示例:
@Bean @Profile("dev") public MyBean myDevBean() { return new MyBean(); }
@EnableAutoConfiguration:
用法:用于标注类,启用Spring Boot的自动配置功能。
示例:
@EnableAutoConfiguration public class MyApplication { // ... }
@SpringBootApplication:
用法:用于标注类,组合了@Configuration、@EnableAutoConfiguration和@ComponentScan注解。
示例:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
@Entity:
用法:用于标注类,表示该类是一个JPA实体。
示例:
@Entity public class MyEntity { // ... }
@Table:
用法:用于标注实体类,指定对应数据库表的信息。
示例:
@Entity @Table(name = "my_table") public class MyEntity { // ... }
@Id:
用法:用于标注字段,表示该字段是实体的主键。
示例:
@Entity public class MyEntity { @Id private Long id; // ... }
@GeneratedValue:
用法:用于标注主键字段,表示该主键的生成策略。
示例:
@Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // ... }
@Column:
用法:用于标注字段,指定对应数据库列的信息。
示例:
@Entity public class MyEntity { @Column(name = "my_column") private String myField; // ... }
@OneToOne:
用法:用于标注字段,表示一对一的实体关系。
示例:
@Entity public class MyEntity { @OneToOne private AnotherEntity anotherEntity; // ... }
@OneToMany:
用法:用于标注字段,表示一对多的实体关系。
示例:
@Entity public class MyEntity { @OneToMany private List<AnotherEntity> anotherEntities; // ... }
@ManyToOne:
用法:用于标注字段,表示多对一的实体关系。
示例:
@Entity public class MyEntity { @ManyToOne private AnotherEntity anotherEntity; // ... }
@ManyToMany:
用法:用于标注字段,表示多对多的实体关系。
示例:
@Entity public class MyEntity { @ManyToMany private List<AnotherEntity> anotherEntities; // ... }
@JoinColumn:
用法:用于标注字段,指定外键列的信息。
示例:
@Entity public class MyEntity { @ManyToOne @JoinColumn(name = "another_entity_id") private AnotherEntity anotherEntity; // ... }
@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; // ... }
@MappedSuperclass:
用法:用于标注类,表示该类是一个JPA映射超类,不能直接映射到数据库表。
示例:
@MappedSuperclass public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // ... }
@Embeddable:
用法:用于标注类,表示该类可以嵌入到其他实体中。
示例:
@Embeddable public class Address { private String street; private String city; // ... }
@Embedded:
用法:用于标注字段,表示该字段是一个嵌入的对象。
示例:
@Entity public class MyEntity { @Embedded private Address address; // ... }
@Transient:
用法:用于标注字段,表示该字段不应持久化到数据库。
示例:
@Entity public class MyEntity { @Transient private String tempData; // ... }