spring - 如何将动态值设置为@PropertySource?

标签 spring spring-mvc

我想使用 @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/

相关文章:

java - SpelEvaluationException: EL1007E:(pos 43): 在 null 上找不到字段或属性 'group'

java - 将 InitBinder 与命令对象一起使用

java - Spring MVC pdf生成

java - 创建类路径资源 3 中定义的名称为 'springSecurityFilterChain' 的 bean 时出错

java - Spring bean 堆栈溢出错误

java - yammer @Timed 将值保留为零

java - 修改Spring安全过滤器中的响应内容

1.7.0.M1 查询查找策略异常的 Spring Data JPA 错误

spring - SeekToCurrentBatchErrorHandler 对死 DLQ 的支持

java - Atmosphere + Spring + Autowiring 问题