Spring @Profile
外观
Spring @Profile[编辑 | 编辑源代码]
简介[编辑 | 编辑源代码]
@Profile 是 Spring Framework 提供的一个核心注解,用于根据不同的环境(如开发、测试、生产)来条件化地加载 Bean。通过使用 @Profile
,开发者可以轻松管理不同环境下的配置,避免手动切换配置文件或代码逻辑。该注解可以应用于类级别或方法级别,通常与 @Configuration
或 @Component
结合使用。
语法[编辑 | 编辑源代码]
@Profile
的基本语法如下:
@Profile("环境名称")
其中,环境名称 可以是任意字符串,表示当前激活的环境(如 "dev"、"test"、"prod")。
激活 Profile[编辑 | 编辑源代码]
Spring 允许通过多种方式激活指定的 Profile:
1. **编程方式**:在应用启动时设置 spring.profiles.active
。
2. **配置文件方式**:在 application.properties
或 application.yml
中配置。
3. **命令行方式**:通过 JVM 参数 -Dspring.profiles.active=环境名称
指定。
示例:激活 Profile[编辑 | 编辑源代码]
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("dev"); // 激活 dev 环境
app.run(args);
}
或者在 application.properties
中:
spring.profiles.active=dev
使用场景[编辑 | 编辑源代码]
@Profile
适用于以下场景:
- 不同环境使用不同的数据源(如开发环境使用 H2,生产环境使用 MySQL)。
- 测试环境下禁用某些功能(如邮件发送)。
- 动态加载配置(如 API 密钥、日志级别)。
代码示例[编辑 | 编辑源代码]
以下示例展示如何在不同环境下加载不同的 Bean。
定义不同环境的配置[编辑 | 编辑源代码]
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema-dev.sql")
.build();
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
dataSource.setUsername("root");
dataSource.setPassword("password");
return dataSource;
}
}
测试代码[编辑 | 编辑源代码]
@SpringBootTest
@ActiveProfiles("dev") // 指定测试环境为 dev
public class ProfileTest {
@Autowired
private DataSource dataSource;
@Test
public void testDevDataSource() {
assertTrue(dataSource instanceof EmbeddedDatabase);
}
}
默认 Profile[编辑 | 编辑源代码]
如果未显式激活任何 Profile,Spring 会加载未标记 @Profile
的 Bean,或者标记为 @Profile("default")
的 Bean。
@Bean
@Profile("default")
public DataSource defaultDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema-default.sql")
.build();
}
多 Profile 组合[编辑 | 编辑源代码]
Spring 支持使用逻辑运算符组合多个 Profile:
@Profile({"dev", "test"})
:表示 dev 或 test 环境激活时加载。@Profile("!prod")
:表示非 prod 环境时加载。
示例[编辑 | 编辑源代码]
@Bean
@Profile({"dev", "test"})
public Service mockService() {
return new MockService();
}
@Bean
@Profile("!prod")
public Logger consoleLogger() {
return new ConsoleLogger();
}
实际案例[编辑 | 编辑源代码]
假设一个电商系统需要区分开发环境和生产环境:
- **开发环境**:使用内存数据库,日志级别为 DEBUG。
- **生产环境**:使用 MySQL 数据库,日志级别为 INFO。
配置类示例[编辑 | 编辑源代码]
@Configuration
public class ECommerceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:init-dev.sql")
.build();
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
// 生产环境数据源配置
}
@Bean
@Profile("dev")
public LoggerConfig devLogger() {
return new LoggerConfig(Level.DEBUG);
}
@Bean
@Profile("prod")
public LoggerConfig prodLogger() {
return new LoggerConfig(Level.INFO);
}
}
总结[编辑 | 编辑源代码]
@Profile
是 Spring 提供的环境感知注解,用于条件化加载 Bean。- 可通过编程、配置文件或命令行激活 Profile。
- 支持默认 Profile 和多 Profile 组合逻辑。
- 适用于多环境配置管理,提高代码的可维护性。