【问题标题】:Auto-Configuration vs Auto-Wiring - Spring Boot自动配置与自动装配 - Spring Boot
【发布时间】:2019-05-03 17:41:22
【问题描述】:

Spring Boot的自动配置和自动装配有什么区别?

是不是,自动装配是将一个 Bean 注入到另一个类中,而自动配置是用于完整的自动装配应用程序的术语?

【问题讨论】:

    标签: java spring spring-boot autowired


    【解决方案1】:

    @SpringBootApplication

    If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines that behaviour so you can also use that instead.
    

    @SpringBootApplication 实际上定义了@EnableAutoConfiguration 和@ComponentScan

    @EnableAutoConfiguration

    Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added
    

    @ComponentScan

    All of your application components (@Component, @Service, @Repository, @Controller etc.) are automatically registered as Spring Beans.
    

    @Autowired 用于依赖注入

    【讨论】:

      【解决方案2】:

      Spring 中的自动配置是指 Spring 根据您添加的依赖项为您配置应用程序所做的工作。 Spring Boot 无需在 Spring MVC 中定义 bean 和配置自己的东西(你还记得你必须做的 xml 配置的数量吗?),Spring Boot 本质上是“监听”你的类路径上的东西,如果它是它的东西能够为您自动配置,它会这样做。

      @SpringBootApplication 注释自动选择让 Spring 自动为您配置各种 bean。

      在自动装配与依赖注入相关的意义上,您是正确的。在您的一个类中添加注解 @Autowired 意味着您将被注解的类的实例带入注解所在的类中。

      【讨论】:

        猜你喜欢
        • 2017-09-07
        • 1970-01-01
        • 2018-10-25
        • 2019-10-21
        • 1970-01-01
        • 2017-01-19
        • 2018-09-09
        • 2018-09-06
        • 1970-01-01
        相关资源
        最近更新 更多