java Spring框架中@Import和@ImportResource注解的使用有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28381957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:29:35  来源:igfitidea点击:

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

javaspringspring-mvcannotations

提问by Paul Vargas

I am studying for the Spring Core certification and I have a doubt related to the @importand the @importresourceannotations 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.javaconfiguration class.

正在导入 2 个其他配置类(类似于将这些配置包含到由我的RootConfig.java配置类表示的主配置中。

Ant I understand that the:

蚂蚁我明白:

@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? Why don't use a single annotation to import both configurations classes and resources (such as xml files or property files?)

所以我的疑问是:为什么存在 2 个不同的注释来完成非常相似的任务?为什么不使用单个注解同时导入配置类和资源(例如 xml 文件或属性文件?)

This is only a semantic differentiation or there are something else?

这只是语义上的区分还是另有其他原因?

采纳答案by Master Slave

@ImportResourceis meant to be used in java-centric configuration context. The docs say,

@ImportResource旨在用于以 Java 为中心的配置上下文。文档说,

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. In these scenarios, simply use @ImportResource and define only as much XML as is needed. Doing so achieves a "Java-centric" approach to configuring the container and keeps XML to a bare minimum.

在@Configuration 类是配置容器的主要机制的应用程序中,仍然可能需要至少使用一些 XML。在这些场景中,只需使用 @ImportResource 并根据需要定义尽可能多的 XML。这样做实现了一种“以 Java 为中心”的方法来配置容器并将 XML 保持在最低限度。

One important aspect is that when using @ImportResourceXML configuration you can override the java-centric configuration using the @Beanannotation. This means that you can override the configuration (by changing the configuration XML) without impacting the code. 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.

一个重要的方面是,在使用@ImportResourceXML 配置时,您可以使用@Bean注释覆盖以 Java 为中心的配置。这意味着您可以在不影响代码的情况下覆盖配置(通过更改配置 XML)。这种语义为您提供了一个可以考虑使用@ImportResource 的上下文,在我看来,这是一项非常宝贵的资产,因为对以 Java 为中心的配置最常见的批评之一是它需要重新编译代码。

The second context is providing the means to gradually move from the XML centric to a java-centric configuration.

第二个上下文提供了逐渐从以 XML 为中心的配置转向以 Java 为中心的配置的方法。

回答by Paul Vargas

  • @Import

    Indicates one or more @Configurationclasses to import. For import Java code-based configuration. e.g.

    @Configuration
    @Import({ DataSourceConfig.class, TransactionConfig.class })
    public class AppConfig { ... }
    

    See more in Using the @Importannotation.

  • @ImportResource

    Indicates one or more resources containing bean definitions to import. For import XML-based configuration or other non-@Configurationbean definition resources. e.g.

    @Configuration
    @ImportResource({"classpath:spring-security.xml"})
    public class SecurityConfig { ... }
    

    See more in @Configurationclass-centric use of XML with @ImportResource.

  • @Import

    表示@Configuration要导入的一个或多个类。用于导入基于 Java 代码的配置。例如

    @Configuration
    @Import({ DataSourceConfig.class, TransactionConfig.class })
    public class AppConfig { ... }
    

    请参阅使用@Import注释中的更多信息

  • @ImportResource

    表示一个或多个包含要导入的 bean 定义的资源。用于导入基于 XML 的配置或其他非@Configurationbean 定义资源。例如

    @Configuration
    @ImportResource({"classpath:spring-security.xml"})
    public class SecurityConfig { ... }
    

    查看更多以@Configuration类为中心的 XML 与@ImportResource.