2013-03-01 56 views
0

我有基於註釋的bean配置佔位符。 藉助此佔位符,我可以非常輕鬆地使用我想要的屬性值。PropertySourcesPlaceholderConfigurer的動態位置

@Bean 
    public static PropertySourcesPlaceholderConfigurer initPlaceholder() { 
     PropertySourcesPlaceholderConfigurer placeholder = new PropertySourcesPlaceholderConfigurer(); 
     placeholder.setLocation(new ClassPathResource("some.properties")); 
     placeholder.setIgnoreUnresolvablePlaceholders(true); 

     return placeholder; 
    } 

我如何設置這個佔位符$ {} some.properties動態值?

placeholder.setLocation(new ClassPathResource(ANY_PROPERTIES)); 

我不能使用initPlaceholder(String屬性)...

+0

所以,你想獲得不使用佔位符的財產?在這種情況下,彈簧配置文件會更好,我認爲...(根據某些配置文件選擇屬性文件)。 – 2013-03-01 15:09:27

回答

0

我所做的這個是創建自己的PropertyPlaceHolder(獲得相關的外部屬性文件)

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { 

public static final String ANY_PROPERTY = "ANY_PROPERTY"; 

private static final Log LOG = LogFactory.getLog(MyPropertyPlaceholderConfigurer.class); 

@Override 
protected void loadProperties(Properties props) throws IOException { 

    String anyProperty = System.getProperty(ANY_PROPERTY); 

    if (StringUtils.isEmpty(anyProperty)) { 
     LOG.info("Using default configuration"); 
     super.loadProperties(props); 

    } else { 
     LOG.info("Setting HDFS LOCATION PATH TO : " + anyProperty); 

     try { 
      Path pt = new Path(anyProperty); 
      Configuration conf = new Configuration(); 
      conf.set(FileSystem.FS_DEFAULT_NAME_KEY, anyProperty); 
      FileSystem fs = FileSystem.get(conf); 
      FSDataInputStream fileOpen = fs.open(pt); 
      BufferedReader br = new BufferedReader(new InputStreamReader(fileOpen)); 
      props.load(br); 
     } catch (Exception e) { 
      LOG.error(e); 
     } 
    } 

}