Java 为什么 Spring Boot Application 类需要有 @Configuration 注解?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39247487/
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-08-11 20:59:47  来源:igfitidea点击:

Why Spring Boot Application class needs to have @Configuration annotation?

javaspringspring-boot

提问by varunkr

I am learning about the Spring Framework but I can't understand what exactly the @Configurationannotation means and which classes should be annotated so. In the Spring Boot docs it is said that the Application class should be @Configurationclass.

我正在学习 Spring 框架,但我无法理解@Configuration注释的确切含义以及应该对哪些类进行注释。在 Spring Boot 文档中,应用程序类应该是@Configuration类。

Spring Boot favors Java-based configuration. Although it is possible to call SpringApplication.run() with an XML source, we generally recommend that your primary source is a @Configuration class.

Spring Boot 支持基于 Java 的配置。尽管可以使用 XML 源调用 SpringApplication.run(),但我们通常建议您的主要源是 @Configuration 类。

Trying to learn about @ConfigurationI find that annotating a class with the @Configurationindicates that the class can be used by the Spring IoC container as a source of bean definitions.

试图了解@Configuration我发现用 注释一个类@Configuration表明该类可以被 Spring IoC 容器用作 bean 定义的来源。

If that is so then how is this application class a source of bean definitions?

如果是这样,那么这个应用程序类如何成为 bean 定义的来源?

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class App 
{
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);    
    }
}

I have pretty much understood most other basic concepts regarding Spring but I can't understand the purpose of @Configurationor which classes should be @Configurationclasses? Can someone please help. Thanks !!

我已经非常了解有关 Spring 的大多数其他基本概念,但我无法理解类的目的@Configuration或哪些类应该是@Configuration类?有人可以帮忙吗。谢谢 !!

采纳答案by Artem Novikov

You understood it right.

你理解正确。

@Configuration

@配置

@Configurationis an analog for xml file. Such classes are sources of bean definitions by defining methods with the @Beanannotation.

@Configuration是 xml 文件的模拟。通过使用@Bean注释定义方法,这些类是 bean 定义的来源。

@Configurationis:

@Configuration是:

  • not required, if you already pass the annotated class in the sourcesparameter when calling the SpringApplication.run()method;
  • required, when you don't pass the annotated class explicitly, but it's in the package that's specified in the @ComponentScanannotation of your main configuration class.
  • 不需要,如果您sources在调用SpringApplication.run()方法时已经在参数中传递了带注释的类;
  • required,当您没有显式传递带注释的类,但它位于@ComponentScan主配置类的注释中指定的包中。

For readability, classes that are even explicitly passed as sourcesmay anyway be annotated with @Configuration- just to show the intentions more clearly.

为了可读性,即使明确传递的类也sources可能被注释@Configuration- 只是为了更清楚地显示意图。

Your current class is not really source of bean definitions, because it doesn't have any, but if you had @Beanannotated methods, Spring would see them.

您当前的类并不是真正的 bean 定义源,因为它没有任何定义,但是如果您有带@Bean注释的方法,Spring 会看到它们。

@EnableAutoConfiguration

@EnableAutoConfiguration

Can be used with or without @Configuration. It tells Spring to setup some basic infrastructure judging by what you have in the classpath. It's done by invoking a so called import class that's derived from the value of the @Importannotation that @EnableAutoConfigurationincludes. Only one class should be annotated with @EnableAutoConfiguration, duplicating it doesn't do anything.

可以在有或没有的情况下使用@Configuration。它告诉 Spring 根据您在类路径中的内容来设置一些基本的基础设施。它是通过调用从包含的@Import注释的值派生的所谓的导入类来完成的@EnableAutoConfiguration。只有一个类应该用 注释@EnableAutoConfiguration,复制它不会做任何事情。

This answer may also be helpful to understand the Spring Boot initialization process: Which piece of code in Spring Boot actually registers dispatcher servlet for SpringMVC?

这个回答可能对理解 Spring Boot 初始化过程也有帮助:Spring Boot 中的哪一段代码实际上为 SpringMVC 注册了 dispatcher servlet?

回答by Nikem

I think the main reason, why Spring Boot's @SpringBootApplicationannotation automatically applies @Configurationis to allow to add bean definitions in the very same class. One of the main goals of Spring Boot is to allow you to create application fast and without extra movements. So by allowing you add bean definitions right into the Application's class, you don't need to create extra classes to hold your configuration. You have just 1 class and that's it.

我认为 Spring Boot 的@SpringBootApplication注解自动应用的主要原因@Configuration是允许在同一个类中添加 bean 定义。Spring Boot 的主要目标之一是让您无需额外移动即可快速创建应用程序。因此,通过允许您将 bean 定义直接添加到应用程序的类中,您无需创建额外的类来保存您的配置。您只有 1 个班级,仅此而已。