2014-09-23 104 views
0

我面臨着一些挑戰,基於一些值可用在其他屬性文件中創建PropertySourcesPlaceholderConfigurer。 我有一個屬性文件custom- {environment} .property,它包含一個值,用於設置PropertySourcesPlaceholderConfigurer的位置。使用屬性創建自定義PropertySourcesPlaceholderConfigurer動態加載

我CustomConfiguration看起來像:

@Bean 
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { 
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); 
    propertySourcesPlaceholderConfigurer.setLocation(customLocation); 
    //Custom url location based on a String available in the properties file. This is the problem area 
    return propertySourcesPlaceholderConfigurer; 
} 

我要填充這個customLocation從屬性文件。嘗試自動裝配環境,但它失敗,因爲當調用placeholderConfigurer()時環境爲空。嘗試使用@PropertySource("custom-${environment}.property"),然後@Value("**customLocation**"),但這也不起作用。

請讓我知道如何可以做到這一點。提前致謝。

+0

我想說,添加一個'ApplicationContextInitializer'來加載'custom- {environment} .properties',然後加載額外的配置文件,是easiests,你可以再補充一個'PropertySource'的方式。 – 2014-09-23 05:54:02

回答

1

我建議增加一個ApplicationContextInitializer,而不是加載的純@PropertySource你的屬性文件。首先加載您的custom-{environment}.properties接下來您的可配置屬性文件。

public class PropertySourceInitializer implements ApplicationContextInitializer { 

    private static final String DEFAULT_CONFIG_FILE = "classpath:custom-${environment}.properties"; 

    @Override 
    public void initialize(ConfigurableApplicationContext applicationContext) { 
     final ConfigurableEnvironment env = applicationContext.getEnvironment(); 
     final MutablePropertySources mps = env.getPropertySources();  
     // 
     Resource resource = applicationContext.getResource(env.resolvePlaceholders(DEFAULT_CONFIG_FILE)); 
     mps.addLast(new ResourcePropertySource(resource.getDescription(), resource)); 

     String additional = env.getProperty("name.of.property"); 
     if (StringUtils.hasText(additional) { 
      Resource additionalResource = applicationContext.getResource(env.resolvePlaceholders(additional)); 
      if (additionalResource.isReadable()) { 
       mps.addLast(new ResourcePropertySource(resource.getDescription(), resource)); 
      } 
     } 
    } 
} 

試圖讓它與@PropertySource工作將作爲其中PropertySourcesPlaceHolderConfigurer創建階段更難是不同的話,則可@PropertySource註釋掃描的一個。分階段加載@PropertySource(這基本上是你想要的)是相當困難的。 Spring Boot也有它自己的加載機制(實際上它也是ApplicationContextInitializer

+0

非常感謝您的建議。但是,仍然有一些問題。在我的代碼,我都用來解析/持續的附加屬性位置CustomPropertyPersister在PropertySourcePlaceholderConfigurer,我可以把我的留存爲這種財產。我該如何使用,在留存PropertySourceInitializer的第二個propertysource? – Nil 2014-09-23 13:48:00

+0

爲什麼在這種情況下需要一個'CustomPropertyPersister'?我沒有看到。您只需從已有的propertysources中檢索屬性。 – 2014-09-23 14:01:11

+0

我使用的CustomLocation不是文本或xml文件。這是一個CMS的位置。我會從一個url中獲取內容,但需要使用自定義persister來解析相關屬性,並分別加載這些內容。據我所知,Spring DefaultPersister只有屬性和xml支持。 – Nil 2014-09-23 14:53:28

0

你可以嘗試設置您的位置的系統屬性?

@Value("#{ systemProperties['myapp.location'] }") 
private String location; 

您需要將「myapp.location」設置爲系統屬性。

+0

我恐怕不能。目前,環境是系統屬性。然而,自定義位置可在屬性文件的基礎上,該環境。 – Nil 2014-09-23 16:12:51