2013-12-16 63 views
1

我想使用@PropertySource注释来设置动态属性源值。任何人都可以告诉我如何做到这一点?对于下面我有动态传递属性文件名。如何将动态值设置为@PropertySource?

@Configuration 
@PropertySource("classpath:message.properties") 
public abstract class AbstractCommonAMQPConfiguration { 

     @Value("${cust.name}") 
    private String custName; 

     @Value("${cust.id}") 
    private String custId; 

} 
+0

只需使用占位符,要么定义它们的环境或JVM性能(-D),这是只有这样你才能动态地指定它。但为什么?为什么不把它放在一个众所周知的位置,并从那里加载... –

+0

感谢您的反应Deinum。加载不同属性文件的原因是,我有5个服务(1,2,... 5)对于每个服务属性值都是不同的。就像,对于第一个服务我想要加载service1.properties,等等。这里只有值是不同的,但是bean的属性是相同的。所以,为了达到这个目的,我尝试使用注解加载动态属性文件。你能否建议我们是否有其他方法? – Pand005

回答

2

我不知道如何与@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; 
    } 
相关问题