简体   繁体   English

Spring 3 @ImportResource包含多个文件

[英]Spring 3 @ImportResource with multiple files

I'm trying to find the syntax for importing multiple spring xml context files using Spring 3 @ImportResource annotation. 我正在尝试使用Spring 3 @ImportResource注释找到导入多个spring xml上下文文件的语法。

I have tried using comma to separate the filenames as illustrated below but that does not work: 我已尝试使用逗号分隔文件名,如下图所示,但这不起作用:

@Configuration
@ImportResource("spring-context1.xml", "spring-context2.xml")
public class ConfigClass { }

The doc for @ImportResource says "Indicates one or more resources containing bean definitions to import." @ImportResource的文档说“表示包含要导入的bean定义的一个或多个资源”。 so I believe there should be a way to specify multiple context files. 所以我认为应该有一种方法来指定多个上下文文件。 Surprisingly, I've not been able to find any example on Google 令人惊讶的是,我无法在Google上找到任何示例

Try: 尝试:

@Configuration  
@ImportResource( { "spring-context1.xml", "spring-context2.xml" } )  
public class ConfigClass { }  

You need to add the classpath before the file name 您需要在文件名之前添加类路径

@ImportResource(value = { 
    "classpath:file1.xml",
    "classpath:file2.xml"
    })

Just adding for future reference if anyone is using this in a groovy project. 如果有人在一个groovy项目中使用它,只需添加以供将来参考。

In groovy the correct syntax uses [ ] square brackets . 在groovy中,正确的语法使用[]方括号 The curly braces will lead to compilation errors. 花括号将导致编译错误。 Please find the example below. 请找到以下示例。

@Configuration  
@ImportResource( [ "spring-context1.xml", "spring-context2.xml" ] ) 

The correct format to define multiple spring resources spring xml context files using Spring 3 @ImportResource : 使用Spring 3 @ImportResource定义多个spring资源spring xml上下文文件的正确格式:

@Configuration  
@ImportResource( { "spring-context1.xml", "spring-context2.xml" } ) 

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

相关问题 如何使用Spring中的@ImportResource批注从类路径加载多个配置文件 - How to load multiple configuration file from classpath using @ImportResource annotation in Spring 在Configuration中由ImportResource覆盖的主Spring bean - Primary spring bean overridden by ImportResource in Configuration 无法通过@ImportResource加载Spring根上下文 - Spring root context is not loaded via @ImportResource 使用@ImportResource处理XML配置文件的解决方法 - Workaround to using @ImportResource for XML config files 在Spring框架中使用@Import和@ImportResource注释有什么区别? - What is the difference between the use of @Import and @ImportResource annotations in Spring framework? @ImportResource无法正常工作 - @ImportResource not working Spring-Boot:如何在@ImportResource中引用application.properties - Spring-Boot: How do I reference application.properties in an @ImportResource 如何使用@ImportResource导入src / main / resources / static中的配置文件的整个目录? - How can I use @ImportResource to import an entire directory of config files in src/main/resources/static? 有谁知道如何在“元注释”中使用Spring的ImportResource注释? - Does anyone have know how to use Spring's ImportResource annotation in a “meta-annotation”? Spring集成拖尾多个文件 - Spring integration tailing multiple files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM