Java @ComponentScan 在 Spring Boot AutoConfiguration 类中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38474382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:21:41  来源:igfitidea点击:

@ComponentScan doesn't work in Spring boot AutoConfiguration class?

javaspringspring-boot

提问by Dongqing

I am trying to create a new starter. I have a business module, say ProjectManager, that contains some classes annotated with @Component. Following the tutorial, I created an autoconfigure module, it contains an AutoConfiguration class. Firstly, I tried to use @ComponentSan to find the beans in my business module.

我正在尝试创建一个新的启动器。我有一个业务模块,比如说 ProjectManager,它包含一些用 @Component 注释的类。按照教程,我创建了一个自动配置模块,它包含一个 AutoConfiguration 类。首先,我尝试使用 @ComponentSan 在我的业务模块中查找 bean。

@ComponentScan(value = {"com.foo.project"})
@ConditionalOnClass({Project.class})
@Configuration
public class ProjectAutoConfiguration {
    ....

}

But it doesn't work. I have to add additional configuration class as below:

但它不起作用。我必须添加额外的配置类,如下所示:

@Configuration
@ComponentScan(value = {"com.foo.project"})
@MapperScan(value = {"com.foo.project"})
public class ProjectConfig {
}

And then import it into AutoConfiguration class like below:

然后将其导入到 AutoConfiguration 类中,如下所示:

@Import(ProjectConfig.class)
@ConditionalOnClass({Project.class})
@Configuration
public class ProjectAutoConfiguration {
    ....

}

That works. But according to the spring doc.

那个有效。但是根据 spring文档

auto-configuration is implemented with standard @Configuration classes

自动配置是用标准的@Configuration 类实现的

So my question is, Why @ComponentScan doesn't work here ? Did I make something wrong? Or it is by design ?

所以我的问题是,为什么 @ComponentScan 在这里不起作用?我做错了什么吗?还是设计使然?

采纳答案by emoleumassi

you have to use the compentscan annotation into the main class. Here a sample code:

您必须在主类中使用 compentscan 注释。这是一个示例代码:

@SpringBootApplication
@ComponentScan("com.foo.project")
public class MainApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainApplication.class);
    }

    public static void main(String[] args) {
        new MainApplication().configure(new SpringApplicationBuilder(MainApplication.class)).run(args);
    }
}

Cheers

干杯

回答by Chathuranga Tennakoon

Can you try with following?

您可以尝试以下吗?

@ConditionalOnClass({Project.class})
@Configuration
@EnableAutoConfiguration
@ComponentScan(value = {"com.foo.project"})
public class ProjectAutoConfiguration {
    ....

}

回答by Merijn Vogel

Automatic everythingrequires the Application class (annotated with @SpringBootApplication) to be in a "higher" package than the components you want to scan.

自动一切都需要 Application 类(用 @SpringBootApplication 注释)位于比您要扫描的组件“更高”的包中。

Use:

用:

package com.example.foo;

for your application and put components in a package like:

用于您的应用程序并将组件放入一个包中,例如:

package com.example.foo.entities;

See also https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

另见https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html