PropertyPlaceholderConfigurer reads from XML File (Apache Commons Configuration)

后端 未结 3 1604
忘了有多久
忘了有多久 2021-02-10 18:51

Is there a possibility to configure the Spring PropertyPlaceholderConfigurer to read from properties.xml, via Apache Commons Configuration?

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 19:27

    The easiest way (maybe not the nicest though) would be to subclass the PropertyPlaceholdeConfigurer, load the commons configuration there and then pass it to the superclass:

    public class TestPlaceholderConfigurer extends PropertyPlaceholderConfigurer
    {
        public TestPlaceholderConfigurer()
        {
            super();
        }
    
        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
                throws BeansException
        {
            XMLConfiguration config = new XMLConfiguration("config.xml");
            Properties commonsProperties = config.getProperties("someKey")
            // Or something else with the configuration
            super.processProperties(beanFactoryToProcess, commonsProperties);
        }
    }
    

    Then you just use this class as the placeholderConfig:

    
        
    
    

提交回复
热议问题