You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

是否有含简要说明的Java(含Spring/SpringBoot)注解汇总列表?

Common Java & Spring/Spring Boot Annotations with Quick Explanations

Hey there! I totally get where you’re coming from—moving from C++/JS to Java (especially the Spring ecosystem) can feel like you’re drowning in annotations at first. They’re everywhere, and tracking down each one’s purpose individually is such a time sink. Let me break down the most common, mainstream annotations you’ll encounter day-to-day, with quick, straightforward explanations:

Core Java Annotations

These are built into the Java language itself, no Spring required:

  • @Override: Marks a method as overriding a parent class method. The compiler will validate that the method signature matches the parent’s, catching typos or mismatches early.
  • @Deprecated: Flags a class, method, or field as outdated. It signals developers to avoid using it, and often includes notes on a recommended replacement.
  • @SuppressWarnings: Tells the compiler to ignore specific warning types (like unchecked type conversions or deprecation warnings). For example, @SuppressWarnings("unchecked") silences warnings about raw generic types.
  • @FunctionalInterface: Identifies an interface as a functional interface (only one abstract method), enabling use with Lambda expressions.
  • @SafeVarargs: Applied to methods with variable arguments, assures the compiler that the method handles generic varargs safely, preventing "unchecked" warnings.

Spring Core Annotations

These are foundational for working with the Spring Framework:

  • @Component: Marks a class as a Spring-managed component. Spring will automatically scan and create an instance (Bean) of this class in its container.
  • @Controller: A specialized @Component for web controllers. Used to handle HTTP requests, often paired with request-mapping annotations.
  • @Service: A @Component variant for business logic layer classes. Helps organize code by clearly marking its role in the application architecture.
  • @Repository: A @Component variant for data access (DAO) classes. Spring automatically handles exception translation for database-related errors here.
  • @Autowired: Automatically injects a Spring Bean into a constructor, field, or setter method. Spring finds a matching Bean in the container and wires it in.
  • @Qualifier: Used alongside @Autowired when multiple Beans of the same type exist. Specifies the exact Bean name to inject, resolving ambiguity.
  • @RequestMapping: Maps HTTP requests to controller methods. Can specify URL paths, request methods (GET/POST), headers, and more. Now often replaced by more specific annotations like @GetMapping.
  • @Value: Injects values from configuration files (like application.properties) into fields. For example, @Value("${app.version}") pulls the app.version property into a String field.
  • @Configuration: Marks a class as a Spring configuration class. Methods annotated with @Bean inside will return objects managed as Spring Beans.
  • @Bean: Defines a Spring Bean within a @Configuration class. The method’s return value becomes a Bean in the Spring container.

Spring Boot Annotations

These simplify Spring Boot development and extend core Spring features:

  • @SpringBootApplication: The core annotation for Spring Boot apps. Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan—use it on your main application class to kickstart auto-configuration and component scanning.
  • @RestController: Combines @Controller and @ResponseBody. Methods in this class automatically return JSON/XML response bodies instead of view templates, perfect for REST APIs.
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping: Specialized versions of @RequestMapping for specific HTTP methods. More readable than using @RequestMapping(method = RequestMethod.GET).
  • @RequestParam: Retrieves query parameters from HTTP requests. For example, @RequestParam("id") Long userId grabs the id parameter from a URL like /users?id=123.
  • @PathVariable: Extracts values from URL path segments. Used with paths like /users/{id}, @PathVariable("id") Long userId gets the id value from the URL.
  • @RequestBody: Converts the HTTP request body (usually JSON/XML) into a Java object. Ideal for receiving complex data in POST/PUT requests.
  • @ConfigurationProperties: Binds configuration file properties to a Java class. Use @ConfigurationProperties(prefix = "app") to map all properties starting with app. to fields in the class.
  • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration magic—automatically sets up Beans based on your project’s dependencies (e.g., configuring Tomcat if you include spring-boot-starter-web). This is already included in @SpringBootApplication.

This list covers the vast majority of annotations you’ll encounter in daily Java/Spring/Spring Boot work. If you need deeper details on a specific one, checking the official Javadoc (for core Java) or Spring’s documentation is still the best bet, but this should give you a solid starting point without endless searching.

内容的提问来源于stack exchange,提问作者user2171796

火山引擎 最新活动