我想使用@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 Framework 5.2.5和Spring Boot 2.2.6开始:]
@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("cust.name", "some property");
}