简体   繁体   English

如何使用几个bean配置文件配置Spring BeanFactory?

[英]How can I configure a Spring BeanFactory with several bean configuration files?

I am sure that the above question has a straightforward answer but I couldn't easily find it (neither in the documentation nor on stackoverflow.com) 我敢肯定,上面的问题有一个简单的答案,但我很难找到它(既不在文档中,也不在stackoverflow.com上)

I got the notion that a BeanFactory/ApplicatioContext can be initialized with several bean configuration files. 我有一个可以用几个bean配置文件初始化BeanFactory / ApplicatioContext的想法。 Is that so? 是这样吗? And, if it is how can it be done? 而且,如果可以的话,怎么办呢?

Mark's answer is fine. 马克的回答很好。 You may also want to try this: 您可能还想尝试一下:

ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{  
                               "services.xml", 
                               "daos.xml", 
                               "webservices.xml", 
                               "validators.xml"
                             });

See section 3.2.2.1 in the Spring Reference documentation. 请参阅Spring Reference文档中的3.2.2.1节 This describes how a configuration file can be split into separate configuration files that can then be imported into your main configuration file. 这描述了如何将配置文件拆分为单独的配置文件,然后将其导入到主配置文件中。

If you use an XML configuration file you can import multiple files from the classpath as such: 如果使用XML配置文件,则可以这样从类路径导入多个文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
  <import resource="classpath:/path/to/file/one.xml" />
  <import resource="classpath:/path/to/file/two.xml" />
</beans>

While reading the above answers I had found the following class and its relevant constructor: 在阅读以上答案时,我发现了以下类及其相关的构造函数:

FileSystemXmlApplicationContext FileSystemXmlApplicationContext来

public FileSystemXmlApplicationContext(String[] configLocations,
                                       boolean refresh,
                                       ApplicationContext parent)
                                throws BeansException

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

相关问题 Spring在多个@Configuration文件中配置同一bean - Spring configure the same bean at multiple @Configuration files 如何在Spring Boot中用自己的模拟替换BeanFactory中的bean? - How to replace bean in BeanFactory with own mock in Spring Boot? 如何以编程方式配置 Spring 的 @Configuration 注释? - How can I programmatically configure Spring's @Configuration annotations? 使用BeanFactory.getBean()创建Spring bean - Creating spring bean using BeanFactory.getBean() 在春季如何将xml bean配置文件导入到@configuration类? - How can I import a xml bean configuration file to a @configuration class in spring? 如何在受管Bean(JSF)中@Autowire BeanFactory - How to @Autowire a BeanFactory in a Managed Bean(JSF) 如何在带有xml配置的Spring MVC中提供默认的bean实现? - How can I provide a default bean implementation in Spring MVC with xml configuration? 如何使用现有应用程序的spring配置类仅测试一个bean? - How can I test only one bean using existing application's spring configuration class? Spring @Configuration类包含@Bean方法,但是如何首先执行init()? - Spring @Configuration class contains @Bean methods but how can I get execute init() first? 如何使用Spring XML配置来设置包含特定类型的所有bean的列表的bean属性? - How can I use Spring XML configuration to set a bean property with list of all beans of a certain type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM