Jeff opened SPR-17165 and commented
In migrating from xml defined configuration java defined configuration, it is not possible to load xml and .class configurations in arbitrary orders. This makes mixing the two somewhat impossible and forces us to stay with the xml configurations. In our case, our xml configs are huge, and migrating them all at once isn't desired.
For example:
The below is the desired configuration. The PropertiesConfig is loaded first, as it loads all property placeholders for use in all of the xml files.
At the end, I'd like to add a MongoConfig.class to add more mongo-spring-data defined beans within the application that reference the beans defined in applicationContext-mongo.xml.
Unfortunately, I can't order the loading of configuration classes between xml and .class files - I have to either load all class files first then all xml files, or load all xml files first, then all class files.
The implementation below gives me the error: "Duplicate annotation of non-repeatable type @Import
"
@Configuration
@Import({PropertiesConfig.class})
@ImportResource({
"classpath:META-INF/spring/applicationContext-assets.xml",
"classpath:META-INF/spring/applicationContext-mongo.xml",
"classpath:META-INF/spring/applicationContext-security.xml",
"classpath:META-INF/spring/applicationContext.xml"})
@Import({MongoConfig.class})
public class CoreConfig {
// more config here.
}
Please allow spring to have ordered loading of configuration files wheather they are xml or .class configurations by making the Import annotations repeatable, or provide another solution.
No further details from SPR-17165
Comment From: SammyVimes
Repeatable annotations won't help here, as they have one order for all (the first occurrence). The only solution that I see is to add @ImportResource
capabilities to @Import
annotation, so it can be used for both classes and files and then we can have an ordered import via repeatable annotations.
Btw, configuration can be split to multiple classes like this:
@Configuration
@ImportResource({
"classpath:META-INF/spring/applicationContext-assets.xml",
"classpath:META-INF/spring/applicationContext-mongo.xml",
"classpath:META-INF/spring/applicationContext-security.xml",
"classpath:META-INF/spring/applicationContext.xml"})
public class XmlLegacyConfig {
}
@Configuration
@Import({PropertiesConfig.class, XmlLegacyConfig.class, MongoConfig.class})
public class CoreConfig {
// more config here.
}
Comment From: jhoeller
Given that import declarations can be split into multiple configuration classes with a dedicated import order for those classes themselves then, I'm inclined to rather recommend that approach over complicating our import annotation arrangement there.