简体   繁体   English

如何使用Spring中的@ImportResource批注从类路径加载多个配置文件

[英]How to load multiple configuration file from classpath using @ImportResource annotation in Spring

I don't know,how to load multiple configuration files from classpath in spring using @ImportResource.I have already gone through the link Spring 3 @ImportResource with multiple files but no luck so far. 我不知道,如何使用@ ImportResource在spring中从classpath加载多个配置文件。我已经通过链接Spring 3 @ImportResource使用多个文件但到目前为止没有运气。 My code is below. 我的代码如下。

@Configuration
@PropertySource("classpath:apis.application.properties")
@ComponentScan(basePackages = {"org.surfnet.oaaas.resource", "org.surfnet.oaaas.service"})
@ImportResource({"classpath:spring-repositories.xml,classpath:commonApplicationContext.xml"})
@EnableTransactionManagement
public class SpringConfiguration {

}

Exception i am facing is 我面临的例外是

java.io.FileNotFoundException: class path resource [spring-repositories.xml,classpath:commonApplicationContext.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

How ever when i try to load a single file,like below.it works for both file.But i can not include two ImportResource annotation in a java class. 然而,当我尝试加载单个文件时,如下所示。它适用于两个文件。但我不能在java类中包含两个ImportResource注释。

    @ImportResource("classpath:spring-repositories.xml"})

You are using the wrong syntax. 您使用的语法错误。 Look carefully at how it's done in the question you linked to. 仔细查看在您链接的问题中如何完成。

There are two strings, not one string containing names separated by commas: 有两个字符串,而不是一个包含以逗号分隔的名称的字符串:

@ImportResource({"classpath:spring-repositories.xml", "classpath:commonApplicationContext.xml"})

But you were almoust right:) It is simmilar like ComponentScan: 但你是正确的:)它与ComponentScan类似:

@ImportResource({"classpath:spring-repositories.xml","classpath:commonApplicationContext.xml"})

When you define resources inside {}, than you put each resource as separate String, optionally with file:, classpath: prefix. 在{}中定义资源时,将每个资源放在单独的String中,可选择使用file:,classpath:prefix。

I have found also this stack page: 我也找到了这个堆栈页面:

Spring 3 @ImportResource with multiple files Spring 3 @ImportResource包含多个文件

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

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