[英]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? 这只是一个语义差异还是还有其他什么?
@Import
Indicates one or more @Configuration
classes to import. 表示要导入的一个或多个
@Configuration
类。 For import Java code-based configuration. 用于导入基于Java代码的配置。 eg
例如
@Configuration @Import({ DataSourceConfig.class, TransactionConfig.class }) public class AppConfig { ... }
See more in Using the @Import
annotation . 有关使用
@Import
注释的详细信息,请参阅。
@ImportResource
Indicates one or more resources containing bean definitions to import. 指示包含要导入的bean定义的一个或多个资源。 For import XML-based configuration or other non-
@Configuration
bean definition resources. 用于导入基于XML的配置或其他非
@Configuration
bean定义资源。 eg 例如
@Configuration @ImportResource({"classpath:spring-security.xml"}) public class SecurityConfig { ... }
See more in @Configuration
class-centric use of XML with @ImportResource
. 有关
@ImportResource
@Configuration
类为中心使用XML的@ImportResource
。
@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.