我想使用 @PropertySource 注释设置动态属性源值。谁能告诉我如何实现这一目标?对于下面我动态传递属性文件名。
@Configuration
@PropertySource("classpath:message.properties")
public abstract class AbstractCommonAMQPConfiguration {
@Value("${cust.name}")
private String custName;
@Value("${cust.id}")
private String custId;
}
最佳答案
我不知道如何使用 @PropertySource
但你可以定义一个 PropertySourcesPlaceholderConfigurer
以编程方式:
private int some_value = 1;
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test" + some_value + ".properties"));
return propertySourcesPlaceholderConfigurer;
}
关于spring - 如何将动态值设置为@PropertySource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20611487/