繁体   English   中英

为什么即使我未明确导入,Spring Boot仍在加载另一个组件?

[英]Why spring boot is loading another component even though i am not importing explicitly?

我没有使用@import批注,尽管上下文能够加载bean(SalesService),但它应该引发异常以查找代码。

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ApplicationContext context = (ApplicationContext) SpringApplication.run(CustomerConfig.class);
        CustomerService customer = (CustomerService) context.getBean("customerBean");
        customer.printMessage("setting message for CustomerService");
        SalesService sales = (SalesService)context.getBean("salesBean");
        sales.printMessage("setting message for SalesService");
    }

}

Annototation @SpringBootApplication对带有注释的类的包执行@ComponentScan

这意味着将实例化同一包或子包中的每个@ Component,@ Controller,@ Service。

在这里看看。

您可以通过向@SpringBootApplication提供属性来控制它。 文件

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM