Specifying multiple Spring configuration files using ant style pattern in command line app
I have a web application with mutiple Spri开发者_StackOverflow社区ng configuration files. These files are loaded using "contextConfigLocation" in web.xml. Like this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/*beans.xml
</param-value>
</context-param>
Everything works as desired.
Now I am having to write a command line application that must load the same files as the web application. Currently I am using ClassPathXmlApplicationContext and manually specifying each configuration file name. But sooner or later somebody is going to add another file and expect it to be read by the CLI, just like the web app. Currently that will not happen because each file is explicitly specified in my CLI. So I need my CLI to load configuration files just like the web app i.e. load all configuration files that match a pattern. Is there a way to do this using ClassPathXmlApplicationContext or any other way?
I think you can do this using ClassPathXmlApplicationContext, This will load any context file that is in class path ending with name Beans
public class LoadContext {
/**
* @param args
*/
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:*Beans.xml");
}
}
Why don't you create a new configuration file spring-all.xml
and load only this. In this file, use the import
element to import all other xml configuration files that you need.
<beans>
<import resource="spring-services.xml"/>
<import resource="spring-daos.xml"/>
<import resource="spring-controllers.xml"/>
...
</beans>
精彩评论