26

I'm trying to find the syntax for importing multiple spring xml context files using Spring 3 @ImportResource annotation.

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." 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

Kes115
  • 2,070
  • 2
  • 21
  • 37

4 Answers4

70

Try:

@Configuration  
@ImportResource( { "spring-context1.xml", "spring-context2.xml" } )  
public class ConfigClass { }  
ajames
  • 813
  • 8
  • 8
  • 1
    ahh needed curly braces. Thanks ajames – Kes115 Mar 01 '13 at 14:13
  • 9
    In case you want to import a resource that is outside the classpath the syntax would be `@ImportResource( { "file:path/spring-context1.xml", "file:path/spring-context2.xml" } ) ` Just adding this because it seems like it could be helpful for people who come accross this post. – konqi Mar 02 '16 at 09:56
11

You need to add the classpath before the file name

@ImportResource(value = { 
    "classpath:file1.xml",
    "classpath:file2.xml"
    })
Arar
  • 1,926
  • 5
  • 29
  • 47
4

Just adding for future reference if anyone is using this in a groovy project.

In groovy the correct syntax uses [ ] square brackets . The curly braces will lead to compilation errors. Please find the example below.

@Configuration  
@ImportResource( [ "spring-context1.xml", "spring-context2.xml" ] ) 
Timo
  • 2,212
  • 2
  • 25
  • 46
Sameer Patil
  • 526
  • 4
  • 13
0

The correct format to define multiple spring resources spring xml context files using Spring 3 @ImportResource:

@Configuration  
@ImportResource( { "spring-context1.xml", "spring-context2.xml" } ) 
konqi
  • 5,137
  • 3
  • 34
  • 52
joanluk
  • 27
  • 1