There are many commonly used annotations in Spring Boot that are used to configure, manage, and define various aspects of a Spring Boot application. Below are these notes organized into major and subcategories, with explanations and examples.
1. Spring Boot core annotations
@SpringBootApplication
Explanation: This is a composition annotation, typically used on the main application class, marking this as the entry point for a Spring Boot application. It contains other annotations such as @Configuration, @ComponentScan and @EnableAutoConfiguration.
Example:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Configuration
- Explanation: Marks a class as a configuration class, which is usually used to define beans.
- Example:
@Configuration
public class MyConfig {
@Beanpublic MyBean myBean() {
return new MyBean();
}
}
@ComponentScan
Explanation: Used to specify the basic package path of the Spring container scanning component.
Example:
@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
2. Spring Boot Web annotations
@Controller
- Explanation: Marks a class as a Spring MVC controller that handles HTTP requests.
- Example:
@Controller
public class MyController {
// Controller methods here
}
@RestController
Explanation: Combining @Controller and @ResponseBody is used to create RESTful style controller. Example:
@RestController
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@RequestMapping
Explanation: Used to map HTTP requests to controller methods and specify URL paths.
Example:
@Controller
@RequestMapping("/my")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
3. Spring Boot Bean annotations
@Component
Explanation: Marks a class as a component of Spring and will be scanned and registered as a Bean by Spring.
Example:
@Component
public class MyComponent {
// Component logic here
}
@Service
explain:
Used to mark a class as a service component of business logic.
Example:
@Service
public class MyService {
// Service logic here
}
@Repository
explain:
Used to mark a class as a data access component, usually used in the persistence layer.
Example:
@Repository
public class MyRepository {
// Repository logic here
}
@Controller
Explanation: Used to mark a class as a Spring MVC controller.
Example:
@Controller
public class MyController {
// Controller logic here
}
@RestController
Explanation: Combine @Controller and @ResponseBody to create a RESTful style controller.
Example:
@RestController
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@Configuration
Explanation: Marks a class as a Spring configuration class, usually used to define beans.
Example:
@Configuration
public class MyConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
These annotations are used to define and manage Spring Beans and are an important part of Spring Boot applications. Each annotation has a different purpose and context, and you can use the appropriate annotation based on your application's needs. In Spring Boot applications, these annotations make it easy to create and manage beans without explicit XML configuration.
4. Spring Boot data access annotations
@Repository
Explanation: Marks a class as a Spring Data warehouse for database access.
Example:
@Repository
public class UserRepository {
// Data access methods here
}
@Entity
Explanation: Used to define JPA entity classes, mapped to database tables.
Example:
@Entity
public class User {
// Entity fields and methods here
}
5. Spring Boot dependency injection annotations
@Autowired
Explanation: Used to autowire beans, usually used with constructors, setter methods, or fields.
Example:
@Service
public class MyService {
private final MyRepository repository;
@Autowired
public MyService(MyRepository repository) {
this.repository = repository;
}
}
@Qualifier
Explanation: Used with @Autowired to specify the name of the Bean to be injected.
Example:
@Service
public class MyService {
private final MyRepository repository;
@Autowired
public MyService(@Qualifier("myRepository") MyRepository repository) {
this.repository = repository;
}
}
6. Other commonly used annotations
@Value
- Explanation: Used to inject property values, usually obtained from configuration files.
- Example:
@Component
public class MyComponent {
@Value("${my.property}")
private String myProperty;
}
@ConfigurationProperties
Explanation: Used to bind configuration properties to a POJO class, usually used to read property values from configuration files.
Example:
@Configuration
@ConfigurationProperties(prefix = "my")
public class MyProperties {
private String property1;
private int property2;
// Getters and setters
}
This is just a taxonomy and example of some of the commonly used annotations in Spring Boot, there are many others available for more specialized use cases. Depending on your needs, you can choose to use appropriate annotations to configure and manage your Spring Boot application.
Test and manage interfaces using Apifox
Apifox is a more powerful interface testing tool than Postman. Apifox = Postman + Swagger + Mock + JMeter, supports debugging http(s), WebSocket, Socket, . After developing the interface, you can automatically generate the interface document with one click through the IDEA plug-in of Apifox, and synchronize multiple terminals, which is very convenient for testing and maintenance. IDEA plug-in and other protocol interfaces, and integrated Dubbo, gRPC
Knowledge expansion: