欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot?常用注解速查表(快速查找)

 更新時(shí)間:2025年08月09日 09:24:07   作者:向陽花自開  
本文整理SpringBoot常用注解速查表,分核心組件(IOC/AOP)、Web開發(fā)、配置類、數(shù)據(jù)訪問及測試等類別,簡明說明各注解作用與使用場景,便于開發(fā)時(shí)快速查閱與應(yīng)用,需要的朋友跟隨小編一起學(xué)習(xí)下吧

?? Spring Boot 常用注解速查表

?? 分類整理 + 應(yīng)用說明,適用于日常開發(fā)參考。

?? 核心注解(IOC/AOP)

注解

作用

應(yīng)用層

@Component

標(biāo)識一個(gè)通用組件,注冊到 Spring 容器

所有組件

@Service

表示服務(wù)層組件(業(yè)務(wù)邏輯)

Service

@Repository

表示 DAO 層組件,帶異常轉(zhuǎn)換功能

Repository

@Controller

表示控制器組件,返回視圖

Web 控制層

@RestController

@Controller + @ResponseBody,返回 JSON

Web API

@Autowired

按類型注入 Bean

所有層

@Resource

默認(rèn)按名稱注入 Bean

所有層

@Value("${key}")

注入配置文件中的屬性值

所有層

@PostConstruct

Bean 初始化后執(zhí)行方法

所有層

@PreDestroy

Bean 銷毀前執(zhí)行方法

所有層

?? Web 開發(fā)相關(guān)(Spring MVC)

注解

作用

@RequestMapping

映射請求路徑(支持 GET/POST 等)

@GetMapping / @PostMapping

更具體的請求方法映射

@PathVariable

獲取 URL 中的路徑變量

@RequestParam

獲取請求參數(shù)(?key=value)

@RequestBody

接收 JSON 請求體并映射為對象

@ResponseBody

將方法返回值轉(zhuǎn)為 JSON

@ModelAttribute

用于表單綁定參數(shù)到對象

@CrossOrigin

支持跨域請求

@SessionAttributes

設(shè)置需要存儲到 session 的屬性

?? 配置類 / 自動(dòng)裝配相關(guān)

注解

作用

@SpringBootApplication

啟動(dòng)類核心注解(包含 3 個(gè)注解)

@Configuration

定義配置類(相當(dāng)于 XML 配置)

@Bean

將方法返回對象注冊為 Bean

@ComponentScan

指定掃描組件的基礎(chǔ)包

@EnableAutoConfiguration

啟用 Spring Boot 自動(dòng)配置

@EnableConfigurationProperties

開啟配置綁定支持

@ConfigurationProperties(prefix="...")

將配置文件的屬性注入到 Java 類

??? 數(shù)據(jù)訪問(JPA)

注解

作用

@Entity

定義實(shí)體類

@Table(name="...")

指定數(shù)據(jù)庫表名

@Id

定義主鍵

@GeneratedValue

主鍵生成策略

@Column

配置列名、約束等

@Repository

標(biāo)注數(shù)據(jù)訪問層類

?? 測試相關(guān)

注解

作用

@SpringBootTest

啟動(dòng)整個(gè) Spring Boot 環(huán)境進(jìn)行測試

@WebMvcTest

測試 Controller 層(不啟動(dòng)整個(gè)容器)

@DataJpaTest

測試 JPA 數(shù)據(jù)層

@MockBean

向測試環(huán)境注入 mock 的 Bean

?? 其他實(shí)用注解

注解

作用

@Primary

標(biāo)記為首選注入 Bean(多個(gè) Bean 時(shí))

@ConditionalOnProperty

根據(jù)配置文件條件啟用 Bean

@Slf4j

自動(dòng)注入日志對象(需 lombok)

?? 建議組合使用(常見套路)

// 啟動(dòng)類
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
// Controller 示例
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/users/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getById(id);
    }
}

到此這篇關(guān)于java多線程的文章就介紹到這了,更多相關(guān)java多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論