Spring & Spring Boot 常用注解整理
现代的 Spring 与 Spring Boot 应用大量使用注解来简化配置、管理组件和实现各种框架功能。本文系统整理了常用的 Spring/Spring Boot 注解,按照功能分类进行介绍。每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发者深入理解和快速检索。
一、Spring Boot 核心注解
@SpringBootApplication
简介: @SpringBootApplication
是 Spring Boot 应用的主入口注解。它标注在启动类上,表示这是一个 Spring Boot 应用。该注解由 Spring Boot 提供(位于 org.springframework.boot.autoconfigure
包),本质上是一个组合注解,包含了 Spring Framework 和 Spring Boot 的关键配置注解。
作用与场景: 使用 @SpringBootApplication
标记主类后,Spring Boot 会自动进行以下配置:
- 配置类声明: 包含了
@SpringBootConfiguration
(其本身是@Configuration
的特化),因此该类被视为配置类,可定义 Bean。 - 组件扫描: 内含
@ComponentScan
,会自动扫描该类所在包及其子包下的组件(被诸如@Component
、@Service
、@Controller
等注解标记的类),将它们注册为 Spring 容器中的 Bean。 - 自动配置: 内含
@EnableAutoConfiguration
,根据类路径下依赖自动配置 Spring Boot 应用。例如,若 classpath 中存在 HSQLDB 数据库依赖,则会自动配置内存数据库等。开发者无需手动编写大量配置即可启动应用。
使用示例: 创建一个 Spring Boot 主启动类,在类上添加 @SpringBootApplication
注解,并编写 main
方法启动应用:
typescript
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
AI写代码java
运行
123456
上述代码中,MyApplication
类由 @SpringBootApplication
注解标记为应用入口。运行 SpringApplication.run
后,Spring Boot 将引导启动内嵌服务器、初始化 Spring 容器,自动扫描组件并完成配置。
注: @SpringBootApplication
提供了属性用于定制,如 exclude
可排除特定的自动配置类。如果需要禁用某些自动配置,可以使用例如 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
来排除。
二、Spring 容器与组件注册注解
这一类注解用于将类注册为 Spring 容器管理的组件或定义配置,以取代传统的 XML 配置文件,实现注解驱动的装配。
@Component
简介: @Component
是一个通用的组件注解,由 Spring Framework 提供(org.springframework.stereotype.Component
)。它用于将一个普通的 Java 类标识为 Spring 容器中的 Bean。被标注的类在组件扫描时会被发现并实例化,由容器统一管理生命周期。
作用与场景: 当某个类不好归类到特定层时,可以使用 @Component
进行标注。典型场景如工具类、通用逻辑处理类等。使用 @Component
后,无需在 XML 中声明 bean,Spring 会根据配置的扫描路径自动将其注册。提供模块: Spring Context 模块提供对组件扫描和 @Component
注解的支持。
使用示例: 定义一个组件类并演示注入:
typescript
@Component
public class MessageUtil {
public String getWelcomeMessage() {
return "Welcome to Spring!";
}
}
// 使用组件
@Service
public class GreetingService {
@Autowired
private MessageUtil messageUtil;
public void greet() {
System.out.println(messageUtil.getWelcomeMessage());
}
}
AI写代码java
运行
1234567891011121314151617
上例中,MessageUtil
类通过 @Component
标记成为容器 Bean,GreetingService
中使用 @Autowired
(详见后文)将其注入,最后调用其方法。
@Service
简介: @Service
是 @Component
的一种特化,用于标注业务逻辑层 的组件(Service层)。它位于 Spring 框架的 org.springframework.stereotype
包。
作用与场景: 在分层架构中,服务层类使用 @Service
注解,使代码含义更语义化。尽管行为上和 @Component
相同(被扫描注册为 Bean),@Service
强调该类承担业务服务职责。提供模块: Spring Context,同属于组件模型的一部分。
使用示例:
typescript
@Service
public class OrderService {
public void createOrder(Order order) {
// 业务逻辑:创建订单
}
}
AI写代码java
运行
123456
通过 @Service
,OrderService
会被自动扫描注册。在需要使用它的地方,例如控制层或其他服务层,可以通过依赖注入获取该 Bean 实例。
@Repository
简介: @Repository
是 @Component
的特化注解之一,用于标注数据访问层 组件(DAO层,或仓库类)。定义在 Spring Framework 的 org.springframework.stereotype.Repository
包中。
作用与场景: DAO 类(例如访问数据库的类)使用 @Repository
注解不仅可以被扫描为容器 Bean,还能启用异常转换 功能。Spring DAO 层会捕获底层数据访问异常(如 JDBC 的 SQLException
或 JPA 的异常),将其翻译为 Spring 统一的DataAccessException
体系,从而简化异常处理。换句话说,如果一个类标注为 @Repository
,Spring 在为其创建代理时会自动处理持久化异常,将原始异常转为 Spring 的非检查型数据访问异常,以提高健壮性。另外,标注了 @Repository
的类可以被 @Autowired
等注解自动装配到其他地方。
使用示例:
kotlin
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public User findById(Long id) {
try {
return jdbcTemplate.queryForObject("SELECT * FROM users WHERE id=?",
new BeanPropertyRowMapper<>(User.class), id);
} catch (DataAccessException e) {
// Spring 已将底层SQLException翻译为DataAccessException
throw e;
}
}
}
AI写代码java
运行
123456789101112131415
上例中,UserDao
使用 @Repository
注解,使其成为容器管理的DAO组件。Spring 自动为其提供异常转换功能:如果 JDBC 操作抛出 SQLException
,会被翻译为 DataAccessException
(RuntimeException),调用处可以统一处理。标注后也允许通过 @Autowired
注入到服务层使用。
@Controller
简介: @Controller
是 Spring MVC 的控制层组件注解,同样派生自 @Component
。由 Spring Web MVC 模块提供(org.springframework.stereotype.Controller
),用于标识一个类是Web MVC 控制器,负责处理 HTTP 请求并返回视图或响应。
作用与场景: 在 Web 应用程序中,@Controller
注解的类会被 DispatcherServlet 识别为控制器,用于映射请求URL、封装模型数据并返回视图名。通常配合视图模板(如 Thymeleaf、JSP)返回页面。如果需要直接返回 JSON 数据,可以配合 @ResponseBody
或直接使用 @RestController
(后者见下文)。
使用示例:
kotlin
@Controller
public class HomeController {
@RequestMapping("/home")
public String homePage(Model model) {
model.addAttribute("msg", "Hello Spring MVC");
return "home"; // 返回视图名,由视图解析器解析为页面
}
}
AI写代码java
运行
12345678
上述 HomeController
使用 @Controller
标记,提供一个映射 "/home" 请求的处理方法。返回值 "home"
代表视图逻辑名,框架会根据配置解析到具体的页面(如 home.html
)。如果我们在类上使用了 @Controller
,框架在启动时会自动注册相应的映射。
@RestController
简介: @RestController
是 Spring 提供的组合注解,等价于同时在类上使用 @Controller
和 @ResponseBody
。它主要由 Spring Web 模块提供,用于RESTful Web服务的控制器。
作用与场景: 标注 @RestController
的类会被识别为控制器,并且其每个处理方法的返回值会直接作为 HTTP 响应体输出,而不是作为视图名称解析。适用于需要返回 JSON、XML 等数据的场景,比如 Web API 接口。模块提供: Spring Web(Spring MVC)。
使用示例:
less
@RestController
@RequestMapping("/api")
public class UserApiController {
@GetMapping("/hello")
public String hello() {
return "Hello, RESTful";
}
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// 直接接收JSON反序列化为User对象,处理后返回
return userService.save(user);
}
}
AI写代码java
运行
123456789101112131415
UserApiController
使用 @RestController
注解,其方法返回字符串和对象将直接通过消息转换器写入响应(例如字符串作为纯文本,User
对象会序列化为 JSON)。不需要再在每个方法上加 @ResponseBody
,使代码更加简洁。通常在开发 REST API 时,都使用 @RestController
来定义控制器。
@Configuration
简介: @Configuration
用于声明一个配置类 ,由 Spring Framework 提供(org.springframework.context.annotation.Configuration
)。配置类可以包含若干个带有 @Bean
注解的方法,以定义 Bean 并交由 Spring 容器管理。@Configuration
本身也是 @Component
,因此配置类也会被组件扫描注册。
作用与场景: 在 Java Config 风格的应用中,@Configuration
相当于传统 XML 配置文件。用于定义 Beans、设置依赖注入规则等。Spring Boot 应用的某些自动配置也是以配置类形式存在。提供模块: Spring Context。
使用示例:
typescript
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
// 配置数据源 Bean,例如使用 HikariCP 数据源
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
ds.setUsername("root");
ds.setPassword("123456");
return ds;
}
@Bean
public UserService userService() {
// 将 UserService 注册为 Bean,并注入依赖的数据源
return new UserService(dataSource());
}
}
AI写代码java
运行
12345678910111213141516171819
上述 AppConfig
被 @Configuration
注解标识为配置类。方法 dataSource()
和 userService()
上的 @Bean
注解会使 Spring 将其返回值注册为容器中的 Bean。其中 userService()
方法调用了 dataSource()
,Spring 会拦截并确保返回的是容器中单例的 DataSource Bean,而非每次调用重新实例化(即 CGLIB 增强 @Configuration
类确保 Bean 单例行为)。
@Bean
简介: @Bean
注解用于定义一个 Bean 。它标注在方法上,表示该方法返回的对象会注册到 Spring 容器中。@Bean
通常配合 @Configuration
使用,由 Spring Context 模块提供。
作用与场景: 当通过 JavaConfig 定义 Bean 时,用 @Bean
替代传统 XML <bean>
声明。例如整合第三方库的 Bean、或需要在创建 Bean 时执行一些自定义逻辑等场景。@Bean
方法可以指定名称(默认是方法名),还支持设置 initMethod
(初始化时回调方法)和 destroyMethod
(销毁时回调方法)。
使用示例:
kotlin
@Configuration
public class MyConfig {
@Bean(name = "customBean", initMethod = "init", destroyMethod = "cleanup")
public MyComponent customBean() {
return new MyComponent();
}
}
AI写代码java
运行
12345678
在上例中,@Bean
注解声明了 customBean
这个 Bean。容器启动时调用 customBean()
方法创建 MyComponent
实例,并以 "customBean"
名称注册。initMethod="init"
表示在 Bean 创建后自动调用其 init()
方法进行初始化;destroyMethod="cleanup"
表示容器销毁该 Bean 时调用其 cleanup()
方法。通过这种方式可以管理 Bean 的生命周期方法(类似于 InitializingBean
和 DisposableBean
接口或 @PostConstruct
/@PreDestroy
,见后文)。
@ComponentScan
简介: @ComponentScan
用于配置组件扫描 路径的注解。由 Spring Context 提供,通常与 @Configuration
一起使用。它的作用是指示 Spring 在指定的包路径下搜索带有组件注解的类,并注册为 Bean。
作用与场景: 默认情况下,Spring Boot 的 @SpringBootApplication
已经隐含指定扫描其所在包及子包。如果需要自定义扫描范围(例如扫描其他包的组件),可以使用 @ComponentScan
注解并提供 basePackages
等属性。普通 Spring 应用(非 Boot)则经常需要在主配置类上显式使用 @ComponentScan
指定根包。提供模块: Spring Context。
使用示例:
less
@Configuration
@ComponentScan(basePackages = {"com.example.service", "com.example.dao"})
public class AppConfig {
// ... Bean definitions
}
AI写代码java
运行
12345
上述配置类通过 @ComponentScan
指定 Spring 将扫描 com.example.service
和 com.example.dao
这两个包及其子包,搜索所有标注了 @Component
/@Service
/@Controller
等的类并注册。这样可以将应用的组件按照包组织,而由配置集中管理扫描范围。
@Import
简介: @Import
注解用于导入额外的配置类或组件到 Spring 容器。它由 Spring Context 提供,可用在 @Configuration
类上,将一个或多个配置类合并进来。也可以用于引入第三方配置。
作用与场景: 当项目拆分成多个配置类时,可以通过 @Import
将它们组合。例如,将公共配置独立出来,再在主配置中引入。Spring Boot 自动配置内部也大量使用了 @Import
来按条件加载配置类。提供模块: Spring Context。
使用示例:
less
@Configuration
@Import({SecurityConfig.class, DataConfig.class})
public class MainConfig {
// 主配置,导入了安全配置和数据配置
}
AI写代码java
运行
12345
如上,MainConfig
通过 @Import
导入了 SecurityConfig
和 DataConfig
两个配置类。这样这两个配置类中定义的 Bean 同样会加载到容器中,相当于把多个配置模块拼装在一起。相比在 XML 里用 <import>
,注解方式更加直观。
注: Spring Boot 提供的许多 @Enable...
注解(例如后文的 @EnableScheduling
等)内部也是通过 @Import
导入相应的配置实现启用功能的。
三、依赖注入注解
依赖注入(DI)是 Spring 核心机制之一。以下注解用于在容器中进行 Bean 注入和装配,解决 Bean 间的依赖关系。
@Autowired
简介: @Autowired
是 Spring 提供的自动装配注解(org.springframework.beans.factory.annotation.Autowired
),用于按类型自动注入依赖对象。它可作用于字段、setter方法或者构造函数上。由 Spring Context 模块支持。
作用与场景: 标注了 @Autowired
的属性或方法,Spring 会在容器启动时自动寻找匹配的 Bean 注入。其中按类型 匹配是默认行为。如果匹配到多个同类型 Bean,则需要结合 @Qualifier
或 @Primary
来消除歧义(见下文)。如果没有找到匹配 Bean,默认会抛出异常。可通过设置 @Autowired(required=false)
来表示找不到 Bean 时跳过注入而不报错。
使用示例:
kotlin
@Component
public class UserService {
@Autowired // 按类型自动装配
private UserRepository userRepository;
// 或者构造函数注入
// @Autowired
// public UserService(UserRepository userRepository) { ... }
public User findUser(Long id) {
return userRepository.findById(id);
}
}
AI写代码java
运行
12345678910111213
上例中,UserService
有一个成员 userRepository
,使用 @Autowired
标注。容器会自动将类型为 UserRepository
的 Bean 注入进来(假设已有 @Repository
或 @Component
标记的 UserRepository
实现)。开发者可以通过构造器、setter 或字段注入的方式使用 @Autowired
。注意: Spring 4.3+ 如果类中只有一个构造器,且需要注入参数,可省略构造函数上的 @Autowired
,仍会自动注入。
@Qualifier
简介: @Qualifier
注解与 @Autowired
配合使用,用于按照名称或限定符进行依赖注入匹配。它由 Spring 提供(org.springframework.beans.factory.annotation.Qualifier
),可以解决当容器中存在多个同类型 Bean 时的冲突。
作用与场景: 默认按类型注入在有多于一个候选 Bean 时会无法确定注入哪个。例如有两个实现类实现了同一接口,都被注册为 Bean。这种情况下,可以在注入点使用 @Qualifier("beanName")
指定注入哪一个 Bean,或在 Bean 定义处使用 @Component("name")
为 Bean 命名,然后在注入处引用同名限定符。提供模块: Spring Context/Beans。
使用示例:
less
@Component("mysqlRepo")
public class MySqlUserRepository implements UserRepository { ... }
@Component("oracleRepo")
public class OracleUserRepository implements UserRepository { ... }
@Service
public class UserService {
@Autowired
@Qualifier("mysqlRepo") // 指定注入名称为 mysqlRepo 的实现
private UserRepository userRepository;
// ...
}
AI写代码java
运行
12345678910111213
如上,有两个 UserRepository
实现 Bean,分别命名为 "mysqlRepo" 和 "oracleRepo"。在 UserService
中,通过 @Qualifier("mysqlRepo")
指定注入名为 mysqlRepo 的 Bean。这样即使存在多个同类型 Bean,Spring 也能准确地注入所需的依赖。
@Primary
简介: @Primary
注解用于标记一个 Bean 为主要候选者。当按类型注入出现多个 Bean 可选时,标有 @Primary
的 Bean 将优先被注入。它由 Spring 提供(org.springframework.context.annotation.Primary
),可作用于类或方法(例如 @Bean
方法)上。
作用与场景: 如果不方便在每个注入点都使用 @Qualifier
指定 Bean,另一种方式是在 Bean 定义处用 @Primary
声明一个首选 Bean。当存在歧义时,容器会选择标记了 @Primary
的 Bean 注入。注意,@Primary
只能有一个,否则仍然无法明确选择。提供模块: Spring Context。
使用示例:
typescript
@Configuration
public class RepoConfig {
@Bean
@Primary // 将这个Bean标记为首选
public UserRepository mysqlUserRepository() {
return new MySqlUserRepository();
}
@Bean
public UserRepository oracleUserRepository() {
return new OracleUserRepository();
}
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// 将自动注入 mysqlUserRepository,因为它被标记为 @Primary
}
AI写代码java
运行
1234567891011121314151617181920
在上例的配置中,我们定义了两个 UserRepository
Bean,其中 MySQL 实现被标记为 @Primary
。因此在 UserService
中按类型注入 UserRepository
时,Spring 会注入标记了 @Primary
的 MySQL 实现。@Primary
提供了一个全局默认方案,简化了注入点的选择。
@Resource
简介: @Resource
是来自 JSR-250 规范的注解(Javax/Jakarta Annotation),Spring 对其提供了支持,用于按名称或按类型注入依赖。它通常位于 jakarta.annotation.Resource
(Java EE/Jakarta EE)包下。注意: 尽管不在 Spring 包中,Spring 容器能识别并处理它。
作用与场景: @Resource
可以看作功能类似于 @Autowired + @Qualifier
的组合。默认情况下按名称 注入:它首先按照属性名或指定的名称在容器中查找 Bean,找不到再按类型匹配。这在某些情况下很有用,例如需要与传统 Java EE 代码兼容时。在 Spring 应用中,也有开发者偏好使用 @Resource
进行依赖注入。提供模块: 需要引入相应的 Jakarta Annotation API,但 Spring Framework 自身支持处理。
使用示例:
less
@Component("userRepo")
public class UserRepositoryImpl implements UserRepository { ... }
@Service
public class UserService {
@Resource(name = "userRepo") // 按名称注入名为"userRepo"的Bean
private UserRepository userRepo;
// ...
}
AI写代码java
运行
123456789
这里,UserRepositoryImpl
组件被命名为 "userRepo"
。在 UserService
中,通过 @Resource(name = "userRepo")
来注入。如果省略 name
属性,@Resource
默认以属性名 userRepo
作为 Bean 名称查找。与 @Autowired
不同,@Resource
不支持 required=false
属性,但其异常信息可能更直观(若找不到 Bean 则抛出 NoSuchBeanDefinitionException
)。值得一提的是,Spring 也支持 JSR-330 的 @Inject
(javax.inject.Inject)注解,其语义与 @Autowired
相同,也可用于构造函数注入等。在实际开发中,可根据团队规范选择使用 Spring 原生的 @Autowired
还是标准的 @Resource
/@Inject
。
@Value
简介: @Value
注解用于将外部化配置中的属性值注入到 Bean 的字段或参数中。它由 Spring 提供(org.springframework.beans.factory.annotation.Value
),常用于读取 application.properties/yaml 配置文件或系统环境变量、JNDI等属性。
作用与场景: 当需要在 Bean 中使用配置文件里的值时,可以使用 @Value("${property.name}")
注入。例如数据库连接参数、服务端口号等。还支持设置默认值和 SpEL 表达式。提供模块: Spring Context 环境抽象。
使用示例:
假设 application.properties 有如下内容:
ini
app.name=MySpringApp
app.version=1.0.0
AI写代码properties
12
Java 类使用 @Value
注入:
kotlin
@Component
public class AppInfo {
@Value("${app.name}")
private String appName;
@Value("${app.version:0.0.1}") // 带默认值,若配置缺失则使用0.0.1
private String appVersion;
// ...
}
AI写代码java
运行
12345678910
上述 AppInfo
类中,@Value("${app.name}")
将把配置中的 app.name
值注入到 appName
字段。如果对应属性不存在,会启动失败。而 appVersion
字段提供了默认值 0.0.1
,当配置文件未设置 app.version
时就会使用默认值。这样,可以灵活地将外部配置与代码解耦,使应用更易于调整参数而无需改动源码。
@Scope
简介: @Scope
注解用于指定 Bean 的作用域,由 Spring 提供(org.springframework.context.annotation.Scope
)。默认情况下,Spring 容器中的 Bean 都是单例(singleton)作用域。通过 @Scope
可以定义其他作用域,例如 prototype、request、session 等。
作用与场景: 常见的作用域:
singleton
(默认):容器中仅保持一个实例。prototype
:每次请求 Bean 时都会创建新实例。- Web相关的作用域(需要在 Web 容器环境下使用):如
request
(每个HTTP请求创建)、session
(每个会话创建)等。
在需要每次使用新对象的场景(如有状态 Bean),可将 Bean 定义成 prototype;在 Web 应用中某些 Bean 希望随请求或会话存续,可用相应作用域。提供模块: Spring Context。
使用示例:
less
@Component
@Scope("prototype")
public class Connection {
public Connection() {
System.out.println("New Connection created.");
}
}
AI写代码java
运行
1234567
将 Connection
Bean 声明为 prototype,每次获取都会创建新的实例:
java
@Autowired
private Connection conn1;
@Autowired
private Connection conn2;
AI写代码java
运行
1234
上面 conn1
和 conn2
将是不同的实例,因为 Connection
定义为 prototype。日志会打印两次 "New Connection created."。若作用域是 singleton,则只创建一次实例并复用。需要注意,prototype Bean 的生命周期由使用方管理,Spring 只负责创建,不会自动调用其销毁方法。
@Lazy
简介: @Lazy
注解用于将 Bean 的初始化延迟到首次使用时(懒加载)。由 Spring 提供(org.springframework.context.annotation.Lazy
),可用于类级别或 @Bean
方法上。
作用与场景: 默认情况下,单例 Bean 在容器启动时就会初始化。如果某些 Bean 的创建比较耗时或在应用运行期间可能不会被用到,可以标记为 @Lazy
,这样只有在真正需要时才实例化,减少启动时间和资源消耗。懒加载常用于:例如调试或在单元测试中减少不必要 Bean 创建,或避免循环依赖时暂缓 Bean 的注入初始化。对于 prototype Bean,Spring 始终延迟创建(因为本身就按需创建),@Lazy
主要针对单例 Bean。提供模块: Spring Context。
使用示例:
less
@Service
@Lazy
public class HeavyService {
public HeavyService() {
// 构造函数可能进行大量初始化
System.out.println("HeavyService initialized");
}
// ...
}
@Controller
public class DemoController {
@Autowired
private HeavyService heavyService; // 被@Lazy标记,不会在容器启动时实例化
// ...
}
AI写代码java
运行
12345678910111213141516
如上,HeavyService
使用 @Lazy
注解标记为懒加载单例。启动时不会打印 "HeavyService initialized"。当 DemoController
第一次实际调用 heavyService
的方法或访问它时,Spring 才会创建 HeavyService
实例并注入。这对于优化启动性能和按需加载组件很有帮助。但应谨慎使用懒加载,如果Bean在启动后马上就会用到,则不应延迟初始化,以免首次调用时产生延迟。
四、配置属性注解
Spring 提供了将配置文件内容绑定到对象的机制,这类注解帮助管理应用的外部化配置和环境区分。
@ConfigurationProperties
简介: @ConfigurationProperties
用于将一组配置属性映射到一个 Java 类上。由 Spring Boot 提供(org.springframework.boot.context.properties.ConfigurationProperties
),通常配合 Bean 使用。通过前缀(prefix)来批量注入配置项到类的属性中。
作用与场景: 当有多项相关配置需要使用时,比起逐个使用 @Value
,可以定义一个配置属性类。例如应用配置、数据源配置等。在类上标注 @ConfigurationProperties(prefix="xxx")
后,该类的各属性会根据前缀读取配置文件中的对应项赋值。需要将该类注册为 Bean(可以通过在类上加 @Component
或在配置类中用 @Bean
创建),Spring Boot 会自动将配置绑定到 Bean 实例上。
使用示例:
application.yml:
yaml
app:
name: MyApp
apiUrl: https://api.example.com
pool:
size: 20
enableLog: true
AI写代码yaml
123456
定义属性绑定类:
less
@Component // 确保被扫描注册为Bean
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String apiUrl;
private Pool pool;