Spring Boot 常用注解大全
以下是Spring Boot中常用的注解及其詳細(xì)解釋以及相應(yīng)的代碼示例:
@SpringBootApplication: 這個(gè)注解用于標(biāo)識一個(gè)Spring Boot應(yīng)用的主類。它整合了 @Configuration,@EnableAutoConfiguration 和 @ComponentScan。
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController: 這個(gè)注解用于定義一個(gè)RESTful控制器,在Spring MVC中它表示所有的處理方法都返回一個(gè)Restful風(fēng)格的數(shù)據(jù)。
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@Service: 這個(gè)注解用于標(biāo)識一個(gè)類是業(yè)務(wù)邏輯層的組件。
@Service
public class UserService {
// Service logic here
}
@Repository: 這個(gè)注解用于標(biāo)識一個(gè)類是數(shù)據(jù)訪問層的組件。
@Repository
public class UserRepository {
// Data access logic here
}
@Component: 這個(gè)注解用于標(biāo)識一個(gè)類是Spring的組件。
@Component
public class MyComponent {
// Component logic here
}
@Autowired: 這個(gè)注解用于自動裝配Spring Bean。
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// Service logic here
}
@Qualifier: 當(dāng)多個(gè)實(shí)現(xiàn)類滿足一個(gè)接口時(shí),可以與 @Autowired 配合使用以指定具體要注入的Bean。
@Service
public class UserService {
@Autowired
@Qualifier("userDatabaseRepository")
private UserRepository userRepository;
// Service logic here
}
@RequestMapping: 這個(gè)注解用于將HTTP請求映射到處理方法上。
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping: 這些注解用于將HTTP GET、POST、PUT、DELETE 請求映射到處理方法上。
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/get")
public String get() {
return "GET Request";
}
@PostMapping("/post")
public String post() {
return "POST Request";
}
@PutMapping("/put")
public String put() {
return "PUT Request";
}
@DeleteMapping("/delete")
public String delete() {
return "DELETE Request";
}
}
@RequestParam: 這個(gè)注解用于從請求中獲取參數(shù)的值。
@GetMapping("/user")
public String getUserById(@RequestParam Long id) {
// logic to fetch user by id
}
@PathVariable: 這個(gè)注解用于從請求的URL中獲取參數(shù)的值。
@GetMapping("/user/{id}")
public String getUserById(@PathVariable Long id) {
// logic to fetch user by id
}
@ResponseBody: 這個(gè)注解用于將方法返回的對象轉(zhuǎn)換為HTTP響應(yīng)的主體部分。
@GetMapping("/user")
@ResponseBody
public User getUser() {
// logic to fetch user
return user;
}
@RequestBody: 這個(gè)注解用于將HTTP請求的主體部分轉(zhuǎn)換為方法參數(shù)。
@PostMapping("/user")
public String addUser(@RequestBody User user) {
// logic to add user
}
@ResponseStatus: 這個(gè)注解用于指定方法返回的HTTP狀態(tài)碼。
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
// Exception handling logic here
}
@ExceptionHandler: 這個(gè)注解用于定義全局異常處理方法。
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException(Exception ex) {
// Exception handling logic here
return "error";
}
}
@Configuration: 這個(gè)注解用于定義配置類,通常與 @Bean 注解一起使用。
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}
@Value: 這個(gè)注解用于從配置文件中獲取值。
@Component
public class MyComponent {
@Value("${my.property}")
private String myProperty;
// Component logic here
}
以上是一些常見的Spring Boot注解及其用法示例。在實(shí)際開發(fā)中,可能還會使用到其他的注解,具體根據(jù)項(xiàng)目需求和設(shè)計(jì)選擇。
相關(guān)文章
Springboot實(shí)現(xiàn)Java阿里短信發(fā)送代碼實(shí)例
這篇文章主要介紹了springboot實(shí)現(xiàn)Java阿里短信發(fā)送代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java向上轉(zhuǎn)型和向下轉(zhuǎn)型的區(qū)別說明
這篇文章主要介紹了Java向上轉(zhuǎn)型和向下轉(zhuǎn)型的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java實(shí)現(xiàn)短信驗(yàn)證碼服務(wù)的完整代碼示例
這篇文章主要介紹了Java實(shí)現(xiàn)短信驗(yàn)證碼服務(wù)的完整代碼示例,文中使用阿里云的短信服務(wù)進(jìn)行應(yīng)用開發(fā)的流程,包括將屬性寫入application.yml配置文件,定義類并指定配置文件,注入實(shí)體類對象等等,需要的朋友可以參考下2024-09-09
Spring Security+MyBatis實(shí)現(xiàn)從數(shù)據(jù)庫動態(tài)查詢權(quán)限的完整實(shí)現(xiàn)方案
文章詳細(xì)介紹了SpringSecurity從數(shù)據(jù)庫動態(tài)查詢權(quán)限的實(shí)現(xiàn)方案,包括核心接口、數(shù)據(jù)庫表設(shè)計(jì)、MyBatis查詢權(quán)限、UserDetailsService實(shí)現(xiàn)、SpringSecurity配置、Controller使用及常見問題優(yōu)化等內(nèi)容,該方案生產(chǎn)可用,靈活且遵循SpringSecurity接口設(shè)計(jì)標(biāo)準(zhǔn)2026-04-04
spring aop 攔截業(yè)務(wù)方法,實(shí)現(xiàn)權(quán)限控制示例
這篇文章主要介紹了spring aop 攔截業(yè)務(wù)方法,實(shí)現(xiàn)權(quán)限控制示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01

