我是Spring Boot Application的新手 . 我想了解spring Boot Application如何在没有@Configuration类的情况下创建bean . 我看了一个示例项目,其中既没有@Bean定义也没有组件扫描,但@Autowired提供了对类的依赖 . 请看下面的代码:
@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(){
.
.
}
}
我对spring的有限知识告诉我,当一个类上有@Service注释时,必须有一个@ComponentScan来创建bean . 但是没有组件扫描,如何创建CertificationServiceImpl bean,以及RestController中的CertificationService自动装配如何在这里工作?
1 回答
如said在文档中:
假设您有Spring Boot app类,例如:
然后,默认情况下会为Spring组件扫描包
com.mypackage
下面的所有包 . 顺便说一句,您可以指定要在@SpringBootApplication
注释中直接扫描的包,而不使用@ComponentScan
. 更多细节here .