【发布时间】:2016-07-13 09:06:30
【问题描述】:
我需要根据 spring.profiles.active 属性自动装配字段。
该服务仅基于配置文件创建,但由于该服务在其他类中自动装配,因此我无法使用 @Profile 注释。
有没有办法根据配置文件自动连接字段。
【问题讨论】:
我需要根据 spring.profiles.active 属性自动装配字段。
该服务仅基于配置文件创建,但由于该服务在其他类中自动装配,因此我无法使用 @Profile 注释。
有没有办法根据配置文件自动连接字段。
【问题讨论】:
您可以为每个配置文件创建不同的服务实现。
在下面的示例中,我使用 mockito 来模拟 dataSource bean
例如
@Configuration
@ComponentScan
class YourConfig {
@Profile("production")
@Qualifier("datasource")
@Bean
public DataSource dataSourceProduction(){
return new DataSourceProduction()
}
@Profile("development")
@Qualifier("datasource")
@Bean
public DataSource dataSourceDevelopment(){
return mock(DataSourceProduction.class);
}
【讨论】: