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

