简体   繁体   English

在Spring框架中使用@Import和@ImportResource注释有什么区别?

[英]What is the difference between the use of @Import and @ImportResource annotations in Spring framework?

I am studying for the Spring Core certification and I have a doubt related to the @import and the @importresource annotations use. 我正在攻读Spring Core认证,我怀疑与@import@importresource注释的使用有关。

So for example I have a configuration class named RootConfig.java , this one: 例如,我有一个名为RootConfig.java的配置类,这个:

@Configuration
@Import({AppConfig.class,DbConfig.class})
@ImportResource("classpath:/config/security-config.xml")
@EnableTransactionManagement
public class RootConfig {

}

So I know that in this example the: 所以我知道在这个例子中:

@Import({AppConfig.class,DbConfig.class})

are importing 2 others configuration classes (something like including these configurations into the main configuration represented by my RootConfig.java configuration class. 正在导入另外两个配置类(类似于将这些配置包含在我的RootConfig.java配置类所代表的主配置中)。

Ant I understand that the: Ant我明白了:

@ImportResource("classpath:/config/security-config.xml")

are importing an XML file. 正在导入XML文件。

So my doubt is: why exist 2 differents annotations that do a very similar task? 所以我的疑问是:为什么存在2个不同的注释,它们执行非常相似的任务? Why don't use a single annotation to import both configurations classes and resources (such as xml files or property files?) 为什么不使用单个注释来导入配置类和资源(例如xml文件或属性文件?)

This is only a semantic differentiation or there are something else? 这只是一个语义差异还是还有其他什么?

@ImportResource is meant to be used in java-centric configuration context. @ImportResource旨在用于以Java为中心的配置上下文。 The docs say, 文档说,

In applications where @Configuration classes are the primary mechanism for configuring the container, it will still likely be necessary to use at least some XML. 在@Configuration类是配置容器的主要机制的应用程序中,仍然可能需要使用至少一些XML。 In these scenarios, simply use @ImportResource and define only as much XML as is needed. 在这些场景中,只需使用@ImportResource并仅根据需要定义尽可能多的XML。 Doing so achieves a "Java-centric" approach to configuring the container and keeps XML to a bare minimum. 这样做可以实现“以Java为中心”的方法来配置容器并将XML保持在最低限度。

One important aspect is that when using @ImportResource XML configuration you can override the java-centric configuration using the @Bean annotation. 一个重要的方面是,当使用@ImportResource XML配置时,您可以使用@Bean批注覆盖以Java为中心的配置。 This means that you can override the configuration (by changing the configuration XML) without impacting the code. 这意味着您可以覆盖配置(通过更改配置XML)而不会影响代码。 This semantic gives you one context where you can consider using @ImportResource , and its in my view a very valuable asset 'cause one of the most common critique of the java-centric configuration is that it requires code recompile. 这个语义为您提供了一个可以考虑使用@ImportResource的上下文 ,并且在我看来它是一个非常有价值的资产,因为对于以Java为中心的配置最常见的批评之一是它需要重新编译代码。

The second context is providing the means to gradually move from the XML centric to a java-centric configuration. 第二个上下文提供了逐步从XML中心迁移到以Java为中心的配置的方法。

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

相关问题 Spring注释之间的区别 - Difference between Spring annotations Spring 和 Spring MVC 框架有什么区别 - what is the difference between Spring and Spring MVC framework Spring 中的@Component、@Repository 和@Service 注解有什么区别? - What's the difference between @Component, @Repository & @Service annotations in Spring? Spring Framework 中@Inject 和@Autowired 的区别是什么? 在什么条件下使用哪一个? - What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition? Spring MongoDB - @Indexed 和 @Field 注释之间的区别 - Spring MongoDB - Difference between @Indexed and @Field annotations @ContextConfiguration和“位置”或相同配置和“类”与@ImportResource之间的区别 - Difference between @ContextConfiguration with “locations” or same config with “classes” and @ImportResource hibernate缓存和Spring框架缓存有什么区别? - What's difference between hibernate caching and Spring framework cache? @Entity 和 @Repository 注释有什么区别? - What is the difference between @Entity and @Repository annotations? @RepositoryRestController和@Repository注释之间有什么区别? - What is the difference between @RepositoryRestController and the @Repository annotations? Spring 3 @ImportResource包含多个文件 - Spring 3 @ImportResource with multiple files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM