SpringBoot中必須掌握的常用注解小結(jié)
1.Bean相關(guān)
@Component:將一個(gè)類標(biāo)識(shí)為 Spring 組件(Bean),可以被 Spring 容器自動(dòng)檢測(cè)和注冊(cè)。通用注解,適用于任何層次的組件。
@Component public class MyComponent { public void doSomething() { System.out.println("MyComponent is doing something."); } }
@ComponentScan:自動(dòng)掃描指定包及其子包中的 Spring 組件。
@ComponentScan("com.example") // 掃描com.example包下的組件,加載到Spring容器 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@Controller:標(biāo)識(shí)控制層組件,實(shí)際上是 @Component 的一個(gè)特化,用于表示 Web 控制器。處理 HTTP 請(qǐng)求并返回視圖或響應(yīng)數(shù)據(jù)。
@Controller public class MyController { @Autowired private final UserService userService; @GetMapping("/user") public String getUser(Model model) { User user = userService.getUserById(1); model.addAttribute("user", user); return "user"; } }
@RestController:是 @Controller 和 @ResponseBody 的結(jié)合,返回的對(duì)象會(huì)自動(dòng)序列化為 JSON 或 XML,并寫入 HTTP 響應(yīng)體中。
@RestController public class MyController { @Autowired private final UserService userService; @GetMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
@Service:標(biāo)識(shí)服務(wù)層組件,實(shí)際上是 @Component 的一個(gè)特化,用于表示業(yè)務(wù)邏輯服務(wù)。
@Service public class MyService { }
@Repository:標(biāo)識(shí)持久層組件(DAO 層),實(shí)際上是 @Component 的一個(gè)特化,用于表示數(shù)據(jù)訪問(wèn)組件。常用于與數(shù)據(jù)庫(kù)交互。
@Repository public interface UserDao { }
@Bean:方法注解,用于修飾方法,主要功能是將修飾方法的返回對(duì)象添加到 Spring 容器中,使得其他組件可以通過(guò)依賴注入的方式使用這個(gè)對(duì)象。
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
@Configuration:標(biāo)識(shí)一個(gè)類作為配置類,@Component 的一個(gè)特化,通常配合 @Bean 注解一起使用。
@Configuration public class AppConfig { }
@Scope:用于聲明一個(gè) Spring Bean 實(shí)例的作用域,作用域的范圍有單例模式、原型模式(多例模式)等。
@Configuration public class AppConfig { @Bean @Scope("prototype") public MyPrototypeBean myPrototypeBean() { return new MyPrototypeBean(); } }
2.依賴注入
@Autowired:用于自動(dòng)注入依賴對(duì)象,Spring 框架提供的注解。
@RestController public class MyController { @Autowired private final UserService userService; @GetMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
@Resource:按名稱自動(dòng)注入依賴對(duì)象(也可以按類型,但默認(rèn)按名稱),JDK 提供注解。
@RestController public class MyController { @Resource private final UserService userService; @GetMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
@Qualifier:與 @Autowired 一起使用,用于指定要注入的 Bean 的名稱。當(dāng)存在多個(gè)相同類型的 Bean 時(shí),可以使用 @Qualifier 來(lái)指定注入哪一個(gè)。
@RestController public class MyController { @Autowired @Qualifier("userService") private final UserService us; @GetMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
3.Web相關(guān)
@RequestMapping:用于映射 HTTP 請(qǐng)求到處理方法上,支持 GET、POST、PUT、DELETE 等請(qǐng)求方法??梢詷?biāo)注在類或方法上。標(biāo)注在類上時(shí),表示類中的所有響應(yīng)請(qǐng)求的方法都是以該類路徑為父路徑。
@RestController @RequestMapping("/contoller") public class MyController { @Resource private final UserService userService; @RequestMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping:分別用于映射 HTTP GET、POST、PUT、DELETE 請(qǐng)求到處理方法上。它們是 @RequestMapping 的特化,分別對(duì)應(yīng)不同的 HTTP 請(qǐng)求方法。
@RestController @GetMapping("/contoller") public class MyController { @Resource private final UserService userService; @GetMapping("/user") public User getUser() { User user = userService.getUserById(1); return user; } }
@RequestParam:用于將請(qǐng)求參數(shù)綁定到 Controller 方法的參數(shù)上。它主要用于處理 GET、POST 等請(qǐng)求中的查詢參數(shù),例如將 http://example.com/api?param1=value1¶m2=value2
中的 param1 和 param2 參數(shù)設(shè)置到方法的參數(shù)上。
@PathVariable:用于從請(qǐng)求的 URL 路徑中提取變量值,并將其綁定到控制器方法的參數(shù)上。
@GetMapping("/user/{userId}") public String getUserById(@PathVariable("userId") Long id) { return "User with ID " + id; }
@RequestBody:將 HTTP 請(qǐng)求體的內(nèi)容(如 JSON、XML)轉(zhuǎn)換為 Java 對(duì)象。通常用于接收前端傳遞的數(shù)據(jù),標(biāo)注在方法的參數(shù)上。
@Controller public class MyController { @PostMapping("/submit") public String submitData(@RequestBody MyData myData) { System.out.println(myData); return "success"; } }
@ResponseBody:將方法的返回值轉(zhuǎn)換為指定格式(如 JSON、XML)作為 HTTP 響應(yīng)的內(nèi)容返回給客戶端。通常與 @RequestMapping 或 @GetMapping 等注解一起使用在方法上。
@RestController public class MyController { @GetMapping("/data") @ResponseBody public String getData() { return "Some data"; } }
4. 讀取配置
@Value:用于注入屬性值,通常從配置文件中獲取。標(biāo)注在字段上,并指定屬性值的來(lái)源(如配置文件中的某個(gè)屬性)。
@Component public class MyComponent { @Value("${my.property}") private String myPropertyValue; public void printValue() { System.out.println(myPropertyValue); } }
@ConfigurationProperties:用于將配置屬性綁定到一個(gè)實(shí)體類上。通常用于從配置文件中讀取屬性值并綁定到類的字段上。
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "my.app") public class MyAppProperties { private String property1; private int property2; // Getters and setters }
5. 配置啟動(dòng)注解
@SpringBootApplication:用于標(biāo)識(shí) SpringBoot 應(yīng)用程序的入口類。它是一個(gè)組合注解,包括了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 三個(gè)注解。
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@EnableAutoConfiguration:?jiǎn)⒂?Spring Boot 的自動(dòng)配置機(jī)制,根據(jù)添加的依賴和配置文件自動(dòng)配置 Spring 應(yīng)用。
6. 其他常用注解
@Transactional:聲明事務(wù)管理。標(biāo)注在類或方法上,指定事務(wù)的傳播行為、隔離級(jí)別等。
import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class MyService { @Transactional public void performTransactionalOperation() { // Database operations } }
@Scheduled:聲明一個(gè)方法需要定時(shí)執(zhí)行。標(biāo)注在方法上,并指定定時(shí)執(zhí)行的規(guī)則(如每隔一定時(shí)間執(zhí)行一次)。
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyScheduledTask { @Scheduled(fixedRate = 5000) public void performTask() { System.out.println("Task executed."); } }
小結(jié)
Spring Boot 中的注解用很多,本文也是走馬觀花的帶大家了解了一下 Spring Boot 中的常見注解。當(dāng)然這些注解也不需要全部記住,只需有一個(gè)大概的印象即可,用的時(shí)候再查具體的使用就可以了。
以上就是SpringBoot中必須掌握的常用注解小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot常用注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)之并查集
并查集這種數(shù)據(jù)結(jié)構(gòu),可能出現(xiàn)的頻率不是那么高,但是還會(huì)經(jīng)常性的見到,其理解學(xué)習(xí)起來(lái)非常容易,通過(guò)本文,一定能夠輕輕松松搞定并查集2021-06-06SpringBoot實(shí)現(xiàn)本地上傳文件到resources目錄
Java后端項(xiàng)目上傳文件是一個(gè)很常見的需求,這篇文章主要為大家介紹了SpringBoot如何實(shí)現(xiàn)本地上傳文件到resources目錄永久保存下載,需要的可以參考一下2023-07-07Java中的System.getenv()和System.getProperty()使用詳解
文章介紹了Java中用于讀取環(huán)境配置信息的兩種方法:System.getenv()和System.getProperty(),前者讀取系統(tǒng)環(huán)境變量,返回一個(gè)不可修改的Map;后者獲取JVM環(huán)境變量值,可以通過(guò)-D參數(shù)設(shè)置,文章還提到,通過(guò)這兩種方法可以簡(jiǎn)化配置,不需要修改代碼2024-11-11Spring整合MyBatis(Maven+MySQL)圖文教程詳解
這篇文章主要介紹了Spring整合MyBatis(Maven+MySQL)圖文教程詳解的相關(guān)資料,需要的朋友可以參考下2016-07-07java學(xué)習(xí)之利用TCP實(shí)現(xiàn)的簡(jiǎn)單聊天示例代碼
這篇文章主要給大家介紹了關(guān)于java學(xué)習(xí)筆記之利用TCP實(shí)現(xiàn)的簡(jiǎn)單聊天的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Spring Cloud Feign接口返回流的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Feign接口返回流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例
今天小編就為大家分享一篇關(guān)于Java使用BigDecimal進(jìn)行運(yùn)算封裝的實(shí)際案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12