"Cannot find configuration source" when using XMLConfiguration Commons configuration using Tomcat

I am creating two applications that use the XMLC configuration configuration commons-configuration. Since the applications are related to each other, I created another project called commons, which has a custom configuration manager that initializes XMLConfiguration as follows:

config = new XMLConfiguration("conf/config.xml");

What happens is that the command-line application works fine by loading the configuration file. But when I try to use my own configuration manager in webapp (using Tomcat), I get

org.apache.commons.configuration.ConfigurationException: cannot find configuration source

I put the conf directory in the WEB-INF folder, the root folder and the META-INF folder. I also tried with "/conf/config.xml", "./conf/config.xml"and "../conf/config.xml".

The only time I got this to work - in a web application - was using the absolute path.

What am I missing?

Thanks Bruno

+3
source share
2 answers

Use ServletContext.getResourceAsStream(..)and stream. Or, if the file is in the classpath, you can usegetClass().getResourceAsStream(..)

+1
source

Actually, it org.apache.commons.configuration.XMLConfigurationdoes not have a constructor that accepts InputStream, so getClass().getResourceAsStream()it will not work. However, there is a constructor XMLConfigurationthat accepts a URL, so use getClass().getResource()instead.

. http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/XMLConfiguration.html

+2

Source: https://habr.com/ru/post/1781860/


All Articles