Spring Boot是一個用于構(gòu)建獨立的、基于Spring框架的Java應(yīng)用程序的開發(fā)框架。它提供了許多注解,用于簡化開發(fā)過程并提供特定功能。下面是一些常用的Spring Boot注解,按照功能進(jìn)行分類說明:
1. 控制器(Controller)注解:
-
@RestController
:將類標(biāo)記為RESTful風(fēng)格的控制器,自動將返回值轉(zhuǎn)換為JSON響應(yīng)。 -
@Controller
:將類標(biāo)記為基于MVC的控制器。
示例:
@RestController
public class UserController {
@GetMapping("/users")
public List<User> getUsers() {
// 返回用戶列表
}
}
2. 路由(Routing)注解:
-
@RequestMapping
:映射HTTP請求到方法或控制器類,指定處理請求的URL路徑、HTTP方法、媒體類型等。 -
@GetMapping
:映射HTTP GET請求到方法。 -
@PostMapping
:映射HTTP POST請求到方法。 -
@PutMapping
:映射HTTP PUT請求到方法。 -
@DeleteMapping
:映射HTTP DELETE請求到方法。 -
@PatchMapping
:映射HTTP PATCH請求到方法。
示例:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable("id") Long id) {
// 根據(jù)ID獲取用戶
}
@PostMapping
public User createUser(@RequestBody User user) {
// 創(chuàng)建新用戶
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable("id") Long id) {
// 刪除用戶
}
}
3. 數(shù)據(jù)訪問(Data Access)注解:
-
@Entity
:將類標(biāo)記為JPA實體。 -
@Repository
:將類標(biāo)記為數(shù)據(jù)訪問組件。 -
@Transactional
:指定事務(wù)的邊界。
示例:
@Entity
public class User {
// 實體類定義
}
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// 數(shù)據(jù)訪問方法定義
}
4. 依賴注入(Dependency Injection)注解:
-
@Autowired
:自動裝配Bean依賴。 -
@Qualifier
:指定注入的Bean的名稱。 -
@Value
:注入配置屬性值。
示例:
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
// 其他方法
@Value("${spring.datasource.username}")
private String databaseSchema;
}
5. 配置(Configuration)注解:
-
@Configuration
:將類標(biāo)記為配置類。 -
@ComponentScan
:指定要掃描的組件包。 -
@PropertySource
:指定屬性文件的位置。
示例:
@Configuration
@ComponentScan("com.example")
@PropertySource("classpath:config.properties")
public class AppConfig {
// 配置類定義
}
config.properties")
public class AppConfig {
// 配置類定義
}
6. AOP(Aspect-Oriented Programming)注解:
-
@Aspect
:將類標(biāo)記為切面。 -
@Before
:在方法執(zhí)行前執(zhí)行通知。 -
@After
:在方法執(zhí)行后執(zhí)行通知。 -
@Around
:在方法執(zhí)行前后執(zhí)行通知。
示例:
@Aspect
@Component
public class LoggingAspect {
@Before("execution(public * com.example.service.*.*(..))")
public void beforeMethodExecution(JoinPoint joinPoint) {
// 在方法執(zhí)行前執(zhí)行日志記錄
}
@After("execution(public * com.example.service.*.*(..))")
public void afterMethodExecution(JoinPoint joinPoint) {
// 在方法執(zhí)行后執(zhí)行日志記錄
}
}
7. 異步處理(Asynchronous Processing)注解:
-
@Async
:將方法標(biāo)記為異步執(zhí)行,可以在方法上直接使用,也可以應(yīng)用在類級別的方法上,作用于整個類的所有方法。
示例:
@Service
public class EmailService {
@Async
public CompletableFuture<Void> sendEmail(String recipient, String message) {
// 異步發(fā)送郵件
}
}
8. 定時任務(wù)(Scheduled Tasks)注解:
-
@Scheduled
:將方法標(biāo)記為定時任務(wù),用于定期執(zhí)行方法。
示例:
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 5000)
public void performTask() {
// 定時執(zhí)行任務(wù)
}
}
9. 條件裝配(Conditional Configuration)注解:
-
@ConditionalOnProperty
:根據(jù)配置屬性的值決定是否加載Bean或配置類。 -
@ConditionalOnClass
:根據(jù)類的存在與否決定是否加載Bean或配置類。 -
@ConditionalOnBean
:根據(jù)Bean的存在與否決定是否加載Bean或配置類。
示例:文章來源:http://www.zghlxwxcb.cn/news/detail-437524.html
@Configuration
@ConditionalOnProperty(name = "myapp.feature.enabled", havingValue = "true")
public class MyFeatureConfiguration {
// 根據(jù)配置屬性決定是否加載配置類
}
10. 異常處理(Exception Handling)注解:
- `@ControllerAdvice`:定義全局異常處理器。
- `@ExceptionHandler`:定義異常處理方法。
示例:文章來源地址http://www.zghlxwxcb.cn/news/detail-437524.html
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
// 處理運行時異常
}
}
到了這里,關(guān)于Spring Boot常用注解詳細(xì)說明的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!