2017-01-30 74 views
0

我試圖以編程方式爲基於配置文件的Spring MVC應用程序設置.properties文件。例如,如果配置文件是dev,我希望獲取database-dev.properties文件。在ApplicationContextInitializer中找到屬性文件時無法解析佔位符

所以在我的web.xml我把它調出我的自定義類

<servlet> 
    <servlet-name>spring-web</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    <init-param> 
    <param-name>contextInitializerClasses</param-name> 
    <param-value>com.galapagos.context.CustomEnvironmentApplicationContextInitializer</param-value> 
    </init-param> 
</servlet> 

自定義類只是着眼於當前配置文件和正確.properties文件添加到環境中的財產來源

public class CustomEnvironmentApplicationContextInitializer 
     implements ApplicationContextInitializer<ConfigurableApplicationContext> { 

    private static final Logger logger = LoggerFactory.getLogger(
     CustomEnvironmentApplicationContextInitializer.class); 


    @Override 
    public void initialize(ConfigurableApplicationContext applicationContext) { 
     // Get the environment 
     ConfigurableEnvironment environment = applicationContext.getEnvironment(); 

     try { 

      profile = environment.getActiveProfiles()[0]; 

      String fileName = String.format(
       "database-%s.properties", 
       profile); 

      ResourcePropertySource resource = 
       new ResourcePropertySource(new ClassPathResource(fileName)); 

      environment.getPropertySources().addFirst(resource); 
      logger.info("Loaded: " + resource); 

     } catch (IOException e) { 
      logger.warn("Error loading: " + e); 
     } 

     // Print the list of property sources 
     logger.info("ENVIRONMENT: " + environment.getPropertySources()); 

     // Refresh the context - is this even needed?? 
     applicationContext.refresh(); 
    } 


} 

我可以告訴上面的工作,因爲它打印出底部的財產來源

2017-01-29 21:43:25 INFO CustomEnvironmentApplicationContextInitializer:66 - ENVIRONMENT: [class path resource [database-dev.properties],servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment] 

每個配置文件的所有database-%{xxx}.properties文件都非常簡單。只是一些JDBC連接的屬性

jdbc.driverClassName=org.postgresql.Driver 
jdbc.url=jdbc:postgresql://localhost:5432/galapagos 
jdbc.username=my_user 
jdbc.password= 

最後我的servlet定義中我加載資源,創造一個dataSource

<beans:bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
</beans:bean> 

<!-- Database/JDBC --> 

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <beans:property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <beans:property name="url" value="${jdbc.url}" /> 
    <beans:property name="username" value="${jdbc.username}" /> 
    <beans:property name="password" value="${jdbc.password}" /> 
</beans:bean> 

但是我得到一個失敗,因爲它不能解析的佔位符名稱

Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}" 

環顧各種例子,他們似乎都使用這種設置(here's one)。任何原因佔位符沒有生效?

謝謝!

回答

0

能夠在這裏自我診斷問題。

我的想法是,PropertyPlaceholderConfigurer類將通過我的環境來源和來源我在那裏的每個文件。

很顯然,自從Spring 3.1以後,新的類被取代 - PropertySourcesPlaceholderConfigurer

From the docs for PropertySourcesPlaceholderConfigurer -

此類設計爲PropertyPlaceholderConfigurer一般更換彈簧3.1應用

所以我更新了我的servlet的XML文件是 -

<beans:bean 
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 

+1

只要使用''這是爲你做的,你不應該在你的初始化類中調用'refresh'。該類僅用於在容器刷新/啓動之前配置上下文。 –

相關問題