简体   繁体   English

Spring Boot Application如何在没有@Configuration类的情况下创建bean

[英]How does a Spring Boot Application create beans without @Configuration class

I am pretty new to Spring Boot Application. 我是Spring Boot Application的新手。 I wanted to understand how does a spring Boot Application create beans without @Configuration class . 我想了解spring Boot Application如何在没有@Configuration类的情况下创建bean。 I had a look at a sample project where there was neither @Bean definitions nor a component scan yet @Autowired provided the dependency to the class. 我看了一个示例项目,其中既没有@Bean定义也没有组件扫描,但@Autowired提供了对类的依赖。 Please have a look at the snippet below: 请看下面的代码:

@RestController
public class RestController{

**@Autowired
public CertificationService certificationService;**
.
.
.
.
}

//Interface

public interface CertificationService{

public List<Certification> findAll();

}

//Implementation Class
@Transactional
@Service

public class CertificationServiceImpl{

public List<Certification> findAll(){
.
.
}

}

My limited knowledge of springs tells me that when there is a @Service annotation over a class, there has to be a @ComponentScan somewhere to create the bean. 我对spring的有限知识告诉我,当一个类上有@Service注释时,必须有一个@ComponentScan来创建bean。 But without a component scan, how does the CertificationServiceImpl bean gets created and thereby how does the autowiring of CertificationService in RestController works here? 但是没有组件扫描,如何创建CertificationServiceImpl bean,以及RestController中的CertificationService自动装配如何在这里工作?

As said in documentation: 如文档中所述

... The @SpringBootApplication annotation is equivalent to using @Configuration , @EnableAutoConfiguration and @ComponentScan ... ... @SpringBootApplication注释等同于使用@Configuration @EnableAutoConfiguration@ComponentScan @EnableAutoConfiguration@ComponentScan ......

Let say you have Spring Boot app class something like: 假设您有Spring Boot app类,例如:

package com.mypackage;

import org.springframework.boot.SpringApplication;    
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication    
public class SpringBootApplication {    
    public static void main(String[] args) {    
        SpringApplication.run(SpringBootApplication.class, args);    
    }    
}

Then all packages below of package com.mypackage will be scanned by default for Spring components. 然后,默认情况下会为Spring组件扫描包com.mypackage下面的所有包。 By the way, you can specify packages to scan right in @SpringBootApplication annotation, without usage of @ComponentScan . 顺便说一句,您可以指定要在@SpringBootApplication注释中直接扫描的包,而不使用@ComponentScan More details here . 更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring boot - 没有@component 注解的bean配置 - Spring boot - beans configuration without @component annotation Spring Boot应用程序需要在@Configuration类中为使用@Value定义默认bean? - Spring Boot application requires defining default beans in a @Configuration class for @Value to be used? 春季启动,不创建bean @ Controller,@ Service - Spring boot, does not create beans @Controller, @Service Spring @Configuration如何缓存对bean的引用 - How does Spring @Configuration cache references to beans 如何根据参数创建spring boot bean - How to create spring boot beans based on parameter 如何使用 map 的依赖注入测试 Spring 启动应用程序配置 class? - How to test Spring boot application configuration class with dependency injection of map? 导入其他没有它的bean的Spring Boot应用程序 - Import other Spring Boot application without it's beans Spring Framework:是否可以在没有@Configuration的情况下创建具有相同@Component的两个bean? - Spring Framework: is possible to create two beans of the same @Component without @Configuration? 在 Spring 启动应用程序中实例化 @Service @Transactional class 的多个 bean - Instantiating multiple beans of the @Service @Transactional class in Spring boot application Java中的Spring配置-创建和使用2个相同类的bean,而不使用Autowired - Spring configuration in Java - Create and use 2 beans of same class NOT using Autowired
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM