[英]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.