为什么Spring Boot Application类需要@Configuration注释? [英] Why Spring Boot Application class needs to have @Configuration annotation?

查看:153
本文介绍了为什么Spring Boot Application类需要@Configuration注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring Framework,但我无法理解 @Configuration 注释的含义是什么,以及应该注释哪些类。在Spring Boot文档中,据说Application类应该是 @Configuration class。

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


Spring Boot支持基于Java的配置。虽然使用XML源调用SpringApplication.run()可能是
,但我们通常建议
建议您的主要源是@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.

试图了解 @Configuration 我发现用 @Configuration 表示Spring IoC容器可以将该类用作bean定义的源。

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

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

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

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

我完全理解大多数关于Spring的其他基本概念但我无法理解 @Configuration 的目的或哪些类应该是 @Configuration 类?有人可以请帮助。谢谢!!

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

推荐答案

你明白了。

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

@Configuration is an analog for xml file. Such classes are sources of bean definitions by defining methods with the @Bean annotation.

@Configuration 是:


  • 不需要,如果您已经通过带注释的类调用 SpringApplication.run()方法时在 sources 参数中;

  • 必需,当您未明确传递带注释的类时,但它位于主配置的 @ComponentScan 注释中指定的包中class。

  • not required, if you already pass the annotated class in the sources parameter 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 @ComponentScan annotation of your main configuration class.

为了便于阅读,甚至明确传递为 sources 的类可能无论如何要用 @Configuration 注释 - 只是为了更清楚地表明意图。

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

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

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

可以使用或不使用 @Configuration 。它告诉Spring根据类路径中的内容来设置一些基本的基础结构。这是通过调用一个所谓的导入类来完成的,该导入类是从 @Import 注释的值派生的, @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 @Import annotation that @EnableAutoConfiguration includes. Only one class should be annotated with @EnableAutoConfiguration, duplicating it doesn't do anything.

这个答案也可能有助于理解Spring Boot初始化过程:其中Spring Boot中的一段代码实际上为SpringMVC注册了调度程序servlet?

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 Application类需要@Configuration注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆