我可以由较小的文件组成一个Spring配置文件吗?
时间:2020-03-06 14:22:24 来源:igfitidea点击:
我有几个项目都使用一个项目作为数据模型。这些项目中的每一个都有其自己的applicationContext.xml文件,其中包含一堆重复的数据。
我想要一个modelContext.xml文件,以及另一个ui.xml等文件。
我可以这样做吗?
解决方案
是的,我们可以通过import元素执行此操作。
<import resource="services.xml"/>
每个元素的资源属性都是有效路径(例如,classpath:foo.xml)
从Spring Docs(v 2.5.5第3.2.2.1节。):
It can often be useful to split up container definitions into multiple XML files. One way to then load an application context which is configured from all these XML fragments is to use the application context constructor which takes multiple Resource locations. With a bean factory, a bean definition reader can be used multiple times to read definitions from each file in turn. Generally, the Spring team prefers the above approach, since it keeps container configuration files unaware of the fact that they are being combined with others. An alternate approach is to use one or more occurrences of the element to load bean definitions from another file (or files). Let's look at a sample: <import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> In this example, external bean definitions are being loaded from 3 files, services.xml, messageSource.xml, and themeSource.xml. All location paths are considered relative to the definition file doing the importing, so services.xml in this case must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. As you can see, a leading slash is actually ignored, but given that these are considered relative paths, it is probably better form not to use the slash at all. The contents of the files being imported must be valid XML bean definition files according to the Spring Schema or DTD, including the top level element.
我们在工作中的项目中使用Spring中的classpath *资源加载器来做到这一点。对于某个应用程序,将加载包含该应用程序ID的所有appcontext文件:
classpath*:springconfig/spring-appname-*.xml
鉴于尼古拉斯指出的内容,我在文档中发现了这一点。它允许我在运行时选择我感兴趣的bean上下文。
GenericApplicationContext ctx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx); xmlReader.loadBeanDefinitions(new ClassPathResource("modelContext.xml")); xmlReader.loadBeanDefinitions(new ClassPathResource("uiContext.xml")); ctx.refresh();
这是我为我的一个项目所做的工作。在web.xml
文件中,我们可以定义我们希望应用程序使用的Spring bean文件:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml /WEB-INF/modelContext.xml /WEB-INF/ui.xml </param-value> </context-param>
如果web.xml
中没有定义,它将自动寻找/ WEB-INF / applicationContext.xml
。
要注意的另一件事是,尽管可以执行此操作,但是如果我们不是XML的忠实拥护者,则可以在Spring 2.5中使用批注进行很多操作。
是的,我们可以在" Master" bean文件中使用标记。但是为什么呢?为什么不在bean工厂的wab.xml或者alslocations数组的contextConfigLocation上下文参数中列出文件?
我认为多个文件更容易处理。我们可以只选择其中的一部分进行测试,只需添加重命名或者删除一部分应用程序,就可以使用相同的配置文件(一个Webapp和一个带有一些重叠bean定义的命令行版本)将不同的应用程序捆绑在一起。