目錄
一、Spring Boot 啟動與配置相關注解
1.1?@SpringBootApplication
1.2?@EnableAutoConfiguration
1.3?@Configuration
1.4?@ComponentScan
二、依賴注入與組件管理注解
2.1?@Component
2.2?@Service
2.3?@Repository
2.4?@Controller
2.5?@RestController
2.6?@Autowired
2.7?@Qualifier
2.8?@Resource
2.9?@Value
三、Web 開發相關注解
3.1?@RequestMapping
3.2?@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping
3.3?@PathVariable
3.4?@RequestParam
3.5?@RequestBody
3.6?@RequestHeader
3.7?@CookieValue
3.8?@ModelAttribute
3.9?@SessionAttributes
四、數據訪問與事務管理注解
4.1?@Entity
4.2?@Table
4.3?@Id
4.4?@GeneratedValue
4.5?@Column
4.6?@Repository
4.7?@Transactional
五、AOP 相關注解
5.1?@Aspect
5.2?@Pointcut
5.3?@Before
5.4?@After
5.5?@AfterReturning
5.6?@AfterThrowing
5.7?@Around
六、測試相關注解
6.1?@SpringBootTest
6.2?@MockBean
6.3?@WebMvcTest
6.4?@DataJpaTest
七、其他注解
7.1?@Conditional
7.2?@ConditionalOnClass
7.3?@ConditionalOnMissingBean
7.4?@ConditionalOnProperty
7.5?@Scheduled
7.6?@EnableScheduling
7.7?@EnableAsync
7.8?@Async
7.9?@ConfigurationProperties
7.10?@EnableConfigurationProperties
一、Spring Boot 啟動與配置相關注解
1.1?@SpringBootApplication
這是 Spring Boot 應用中最核心的注解,通常位于主應用類上。它是一個組合注解,實際上包含了?@Configuration
、@EnableAutoConfiguration
?和?@ComponentScan
?三個注解的功能。
- 作用:
@Configuration
:表明該類是一個配置類,相當于傳統 Spring 中的 XML 配置文件。@EnableAutoConfiguration
:開啟 Spring Boot 的自動配置功能,Spring Boot 會根據類路徑下的依賴和配置自動配置應用。@ComponentScan
:指定 Spring 掃描組件的范圍,默認掃描主應用類所在包及其子包下帶有?@Component
、@Service
、@Repository
、@Controller
?等注解的類。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MySpringBootApp {public static void main(String[] args) {SpringApplication.run(MySpringBootApp.class, args);}
}
1.2?@EnableAutoConfiguration
- 作用:Spring Boot 會根據類路徑中的依賴自動配置應用。例如,如果類路徑中存在 H2 數據庫的依賴,Spring Boot 會自動配置一個嵌入式的 H2 數據庫。不過,有時候自動配置可能不符合我們的需求,這時可以使用?
exclude
?屬性排除某些自動配置類。
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyAppWithoutDataSourceAutoConfig {public static void main(String[] args) {SpringApplication.run(MyAppWithoutDataSourceAutoConfig.class, args);}
}
1.3?@Configuration
- 作用:用于定義配置類,配置類可以包含多個?
@Bean
?注解的方法,這些方法會返回一個對象,該對象會被 Spring 容器管理。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;@Configuration
public class AppConfig {@Beanpublic List<String> myList() {return new ArrayList<>();}
}
1.4?@ComponentScan
- 作用:指定 Spring 掃描組件的范圍。可以通過?
basePackages
?屬性指定要掃描的包,也可以通過?basePackageClasses
?指定要掃描的類所在的包。 - 示例:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages = "com.example.demo.service")
public class AppConfig {// 配置類內容
}
二、依賴注入與組件管理注解
2.1?@Component
- 作用:這是一個通用的組件注解,用于標記一個類為 Spring 組件,被 Spring 容器管理。它是?
@Service
、@Repository
、@Controller
?的父注解。 - 示例:
import org.springframework.stereotype.Component;@Component
public class MyComponent {public void doSomething() {System.out.println("Doing something in MyComponent");}
}
2.2?@Service
- 作用:通常用于標記業務邏輯層的類,是?
@Component
?的一種特殊形式,語義上更明確表示這是一個服務類。 - 示例:
import org.springframework.stereotype.Service;@Service
public class UserService {public void saveUser() {// 保存用戶的業務邏輯}
}
2.3?@Repository
- 作用:主要用于標記數據訪問層的類,如 DAO 類。Spring 會自動處理?
@Repository
?注解類拋出的異常,將其轉換為 Spring 的數據訪問異常。 - 示例:
import org.springframework.stereotype.Repository;@Repository
public class UserRepository {public void saveUserToDb() {// 將用戶保存到數據庫的邏輯}
}
2.4?@Controller
- 作用:用于標記控制器類,處理 HTTP 請求。通常與?
@RequestMapping
?等注解配合使用。 - 示例:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class MyController {@RequestMapping("/hello")@ResponseBodypublic String sayHello() {return "Hello, World!";}
}
2.5?@RestController
- 作用:是?
@Controller
?和?@ResponseBody
?的組合注解,用于創建 RESTful 風格的控制器,方法返回的對象會自動轉換為 JSON 或 XML 等格式返回給客戶端。 - 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyRestController {@GetMapping("/data")public String getData() {return "{\"message\": \"This is some data\"}";}
}
2.6?@Autowired
- 作用:用于自動裝配 Bean,默認按照類型進行裝配。可以用于構造器、字段、方法上。
- 示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {private UserRepository userRepository;@Autowiredpublic UserService(UserRepository userRepository) {this.userRepository = userRepository;}public void saveUser() {userRepository.saveUserToDb();}
}
2.7?@Qualifier
- 作用:當存在多個相同類型的 Bean 時,
@Autowired
?無法確定要注入哪個 Bean,此時可以使用?@Qualifier
?指定要注入的 Bean 的名稱。 - 示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;@Service
public class MyService {private MyComponent myComponent;@Autowiredpublic MyService(@Qualifier("specificComponent") MyComponent myComponent) {this.myComponent = myComponent;}
}
2.8?@Resource
- 作用:也是用于依賴注入,默認按照名稱進行裝配,如果找不到匹配的名稱,則按照類型進行裝配。
- 示例:
import javax.annotation.Resource;
import org.springframework.stereotype.Service;@Service
public class MyService {@Resourceprivate MyComponent myComponent;public void doSomething() {myComponent.doSomething();}
}
2.9?@Value
- 作用:用于從配置文件中讀取屬性值并注入到 Bean 的字段中。支持 SpEL 表達式。
- 示例:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyConfig {@Value("${app.name}")private String appName;public String getAppName() {return appName;}
}
三、Web 開發相關注解
3.1?@RequestMapping
- 作用:用于映射 HTTP 請求到控制器的處理方法上。可以指定請求的 URL、請求方法(GET、POST 等)、請求參數等。
- 示例:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class MyController {@RequestMapping(value = "/hello", method = RequestMethod.GET)@ResponseBodypublic String sayHello() {return "Hello, World!";}
}
3.2?@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
、@PatchMapping
- 作用:這些注解是?
@RequestMapping
?的簡化形式,分別對應 HTTP 的 GET、POST、PUT、DELETE、PATCH 請求方法。 - 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyRestController {@GetMapping("/data")public String getData() {return "{\"message\": \"This is some data\"}";}
}
3.3?@PathVariable
- 作用:用于獲取 URL 中的路徑變量。
- 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@GetMapping("/users/{id}")public String getUser(@PathVariable Long id) {return "User with ID: " + id;}
}
3.4?@RequestParam
- 作用:用于獲取 HTTP 請求中的查詢參數。
- 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class SearchController {@GetMapping("/search")public String search(@RequestParam String keyword) {return "Searching for: " + keyword;}
}
3.5?@RequestBody
- 作用:用于將 HTTP 請求的主體內容綁定到方法的參數上,通常用于處理 JSON 或 XML 格式的數據。
- 示例:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@PostMapping("/users")public String createUser(@RequestBody User user) {// 處理用戶創建邏輯return "User created: " + user.getName();}
}class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}
3.6?@RequestHeader
- 作用:用于獲取 HTTP 請求的頭部信息。
- 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HeaderController {@GetMapping("/headers")public String getHeader(@RequestHeader("User-Agent") String userAgent) {return "User-Agent: " + userAgent;}
}
3.7?@CookieValue
- 作用:用于獲取 HTTP 請求中的 Cookie 值。
- 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CookieController {@GetMapping("/cookies")public String getCookie(@CookieValue("session_id") String sessionId) {return "Session ID: " + sessionId;}
}
3.8?@ModelAttribute
- 作用:有兩種使用方式。一是用于方法上,該方法會在控制器的每個處理方法執行前被調用,用于初始化一些數據;二是用于方法參數上,將請求參數綁定到一個對象上。
- 示例:
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ModelAttributeController {@ModelAttributepublic void addAttributes(Model model) {model.addAttribute("message", "This is a pre - added message");}@GetMapping("/model")public String getModel(@ModelAttribute("message") String message) {return message;}
}
3.9?@SessionAttributes
- 作用:用于將模型中的屬性存儲到會話中,以便在多個請求之間共享數據。
- 示例:
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.annotation.RestController;@RestController
@SessionAttributes("user")
public class SessionAttributeController {@GetMapping("/addUser")public String addUserToSession(Model model) {model.addAttribute("user", "John Doe");return "User added to session";}@GetMapping("/getUser")public String getUserFromSession(@ModelAttribute("user") String user) {return "User from session: " + user;}
}
四、數據訪問與事務管理注解
4.1?@Entity
- 作用:用于標記一個 Java 類為 JPA 實體類,該類會映射到數據庫中的一張表。
- 示例:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;// Getters and Setters
}
4.2?@Table
- 作用:用于指定實體類對應的數據庫表名。
- 示例:
import javax.persistence.Entity;
import javax.persistence.Table;@Entity
@Table(name = "users")
public class User {// 實體類內容
}
4.3?@Id
- 作用:用于標記實體類中的主鍵字段。
- 示例:見?
@Entity
?示例。
4.4?@GeneratedValue
- 作用:用于指定主鍵的生成策略,如自增、UUID 等。
- 示例:見?
@Entity
?示例。
4.5?@Column
- 作用:用于指定實體類的字段與數據庫表列的映射關系,可設置列名、長度、是否可為空等屬性。
- 示例:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class User {@Idprivate Long id;@Column(name = "user_name", length = 50, nullable = false)private String name;// Getters and Setters
}
4.6?@Repository
- 作用:在數據訪問層使用,標記該類為數據訪問組件,Spring 會自動處理數據訪問異常。
- 示例:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {// 自定義查詢方法
}
4.7?@Transactional
- 作用:用于聲明事務邊界,可以應用在類或方法上。在方法上使用時,該方法會在事務中執行;在類上使用時,該類的所有公共方法都會在事務中執行。可以設置事務的傳播行為、隔離級別等屬性。
- 示例:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class UserService {private UserRepository userRepository;public UserService(UserRepository userRepository) {this.userRepository = userRepository;}@Transactionalpublic void saveUser(User user) {userRepository.save(user);}
}
五、AOP 相關注解
5.1?@Aspect
- 作用:用于標記一個類為切面類,該類中可以定義切點和通知。
- 示例:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.demo.service.*.*(..))")public void serviceMethods() {}@Before("serviceMethods()")public void beforeServiceMethod() {System.out.println("Before service method execution");}
}
5.2?@Pointcut
- 作用:用于定義切點表達式,指定哪些方法會被切面攔截。
- 示例:見?
@Aspect
?示例。
5.3?@Before
- 作用:前置通知,在目標方法執行前執行。
- 示例:見?
@Aspect
?示例。
5.4?@After
- 作用:后置通知,在目標方法執行后執行,無論目標方法是否拋出異常。
- 示例:
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.demo.service.*.*(..))")public void serviceMethods() {}@After("serviceMethods()")public void afterServiceMethod() {System.out.println("After service method execution");}
}
5.5?@AfterReturning
- 作用:返回通知,在目標方法正常返回后執行,可以獲取目標方法的返回值。
- 示例:
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.demo.service.*.*(..))")public void serviceMethods() {}@AfterReturning(pointcut = "serviceMethods()", returning = "result")public void afterServiceMethodReturning(Object result) {System.out.println("Service method returned: " + result);}
}
5.6?@AfterThrowing
- 作用:異常通知,在目標方法拋出異常時執行,可以獲取異常信息。
- 示例:
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.demo.service.*.*(..))")public void serviceMethods() {}@AfterThrowing(pointcut = "serviceMethods()", throwing = "ex")public void afterServiceMethodThrowing(Exception ex) {System.out.println("Service method threw exception: " + ex.getMessage());}
}
5.7?@Around
- 作用:環繞通知,環繞目標方法執行,可以在目標方法執行前后進行增強處理,還可以控制目標方法是否執行。
- 示例:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.demo.service.*.*(..))")public void serviceMethods() {}@Around("serviceMethods()")public Object aroundServiceMethod(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("Before service method execution");Object result = joinPoint.proceed();System.out.println("After service method execution");return result;}
}
六、測試相關注解
6.1?@SpringBootTest
- 作用:用于啟動 Spring Boot 應用的上下文進行測試,可以模擬完整的應用環境。
- 示例:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MyServiceTest {@Autowiredprivate MyService myService;@Testpublic void testMyService() {myService.doSomething();}
}
6.2?@MockBean
- 作用:用于在測試中創建 Mock 對象,替換 Spring 容器中的實際 Bean,方便進行單元測試。
- 示例:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;import static org.mockito.Mockito.when;@SpringBootTest
public class UserServiceTest {@Autowiredprivate UserService userService;@MockBeanprivate UserRepository userRepository;@Testpublic void testSaveUser() {User user = new User();when(userRepository.save(user)).thenReturn(user);userService.saveUser(user);}
}
6.3?@WebMvcTest
- 作用:用于測試 Spring MVC 控制器,只加載與控制器相關的組件,不加載整個應用上下文,提高測試效率。
- 示例:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@WebMvcTest(MyController.class)
public class MyControllerTest {@Autowiredprivate MockMvc mockMvc;@Testpublic void testSayHello() throws Exception {mockMvc.perform(get("/hello")).andExpect(status().isOk()).andExpect(content().string("Hello, World!"));}
}
6.4?@DataJpaTest
- 作用:用于測試 JPA 數據訪問層,自動配置嵌入式數據庫,只加載與 JPA 相關的組件。
- 示例:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;import static org.junit.jupiter.api.Assertions.assertNotNull;@DataJpaTest
public class UserRepositoryTest {@Autowiredprivate UserRepository userRepository;@Testpublic void testSaveUser() {User user = new User();user.setName("John Doe");User savedUser = userRepository.save(user);assertNotNull(savedUser.getId());}
}
七、其他注解
7.1?@Conditional
- 作用:根據條件決定是否創建 Bean。可以自定義條件類,實現?
Condition
?接口。 - 示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean@Conditional(MyCondition.class)public MyBean myBean() {return new MyBean();}
}
7.2?@ConditionalOnClass
- 作用:當類路徑中存在指定的類時,才會創建對應的 Bean。
- 示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean@ConditionalOnClass(name = "com.example.SomeClass")public MyBean myBean() {return new MyBean();}
}
7.3?@ConditionalOnMissingBean
- 作用:當容器中不存在指定類型的 Bean 時,才會創建對應的 Bean。
- 示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean@ConditionalOnMissingBean(MyBean.class)public MyBean myBean() {return new MyBean();}
}
7.4?@ConditionalOnProperty
- 作用:根據配置文件中的屬性值決定是否創建 Bean。
- 示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")public MyBean myBean() {return new MyBean();}
}
7.5?@Scheduled
- 作用:用于創建定時任務,可指定任務的執行時間間隔、固定延遲等。
- 示例:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class MyScheduledTask {@Scheduled(fixedRate = 5000)public void doTask() {System.out.println("Task executed every 5 seconds");}
}
7.6?@EnableScheduling
- 作用:啟用 Spring 的定時任務功能,通常與?
@Scheduled
?配合使用。 - 示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling
public class MyApp {public static void main(String[] args) {SpringApplication.run(MyApp.class, args);}
}
7.7?@EnableAsync
- 作用:啟用 Spring 的異步方法調用功能,通常與?
@Async
?配合使用。 - 示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication
@EnableAsync
public class MyApp {public static void main(String[] args) {SpringApplication.run(MyApp.class, args);}
}
7.8?@Async
- 作用:標記方法為異步方法,該方法會在單獨的線程中執行。
- 示例:
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class MyAsyncService {@Asyncpublic void doAsyncTask() {// 異步執行的任務}
}
7.9?@ConfigurationProperties
- 作用:用于將配置文件中的屬性綁定到一個 Java 對象上。
- 示例:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "app")
public class AppConfigProperties {private String name;private String version;// Getters and Setters
}
7.10?@EnableConfigurationProperties
- 作用:啟用?
@ConfigurationProperties
?注解的類,通常在配置類上使用。 - 示例:
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableConfigurationProperties(AppConfigProperties.class)
public class AppConfig {// 配置類內容
}
以上涵蓋了 Spring Boot 中大部分常用的注解,理解和掌握這些注解可以幫助開發者更高效地開發 Spring Boot 應用。