简体   繁体   English

Spring:XML配置位置

[英]Spring : XML Configuration Location

I am learner of spring have build my test project with spring IOC container and have configure beans.xml in my project root path and load into my application and get bean from it. 我是spring的学习者,已使用spring IOC容器构建了测试项目,并在项目根路径中配置了beans.xml并加载到应用程序中并从中获取bean。

spring.xml in project root directory 项目根目录中的spring.xml

BeanFactory bean = new XmlBeanFactory(new FileSystemResource("spring.xml"));

spring.xml in source file 源文件中的spring.xml

BeanFactory bean = new XmlBeanFactory(new FileSystemResource("src/spring.xml"));

this is another code to load beans.xml file 这是另一个加载bean.xml文件的代码

ApplicationContext context = new GenericXmlApplicationContext("beans.xml");

my question is that is there any standards or conventions for creation of xml file name and location of file in real project.because in some reading articles i also found that there might be multiple xml files for large project like service.xml and dao.xml. 我的问题是,在实际项目中是否存在用于创建xml文件名和文件位置的任何标准或约定。由于在一些阅读文章中,我还发现大型项目(例如service.xml和dao.xml)可能存在多个xml文件。

It can be useful to have bean definitions span multiple XML files. 使bean定义跨越多个XML文件可能很有用。 Often each individual XML configuration file represents a logical layer such as defining DAO beans etc. in your architecture but you should always place your XML configuration files under src/resources and access them as 通常,每个单独的XML配置文件都代表一个逻辑层,例如在体系结构中定义DAO bean等,但是您应始终将XML配置文件放在src / resources下,并以

new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

From the Spring's Manual: 从春季手册中:

You can use the application context constructor to load bean definitions from all these XML fragments. 您可以使用应用程序上下文构造函数从所有这些XML片段中加载bean定义。 This constructor takes multiple Resource locations, as was shown in the previous section. 如上一节中所示,此构造函数具有多个Resource位置。 Alternatively, use one or more occurrences of the element to load bean definitions from another file or files. 或者,使用一个或多个出现的元素从另一个文件中加载bean定义。 For example: 例如:

<beans>
  <import resource="services.xml"/>
  <import resource="resources/messageSource.xml"/>
  <import resource="/resources/themeSource.xml"/>

</beans>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM