Java – what is the difference between using @ import and @ importresource annotations in the spring framework?
I am studying for spring core certification, and I suspect it is related to the use of @ import and @ importresource annotations
For example, I have a named rootconfig Java configuration class, this:
@Configuration @Import({AppConfig.class,DbConfig.class}) @ImportResource("classpath:/config/security-config.xml") @EnableTransactionManagement public class RootConfig { }
So I know that in this example:
@Import({AppConfig.class,DbConfig.class})
Two other configuration classes are being imported (similar to including these configurations in the main configuration represented by my rootconfig.java configuration class)
Ant, I see:
@ImportResource("classpath:/config/security-config.xml")
Importing XML file
So my question is: Why are there two different annotations that perform very similar tasks? Why not use a single comment to import configuration classes and resources (such as XML files or property files?)
Is this just a semantic difference or something else?
Solution
@Importresource is designed for Java - centric configuration context The document says,
An important aspect is that when using @ importresource XML configuration, you can use @ bean annotation to override Java centric configuration This means that you can override the configuration (by changing the configuration XML) without affecting the code This semantics gives you a context to consider using @ importresource, and in my opinion it is a very valuable asset, because one of the most common criticisms of Java centric configuration is that it needs to recompile code
The second context provides a step - by - step migration from XML - centric to Java - centric configuration