Java 使用注解@SpringBootApplication 进行配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33619532/
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 14:49:53  来源:igfitidea点击:

Configuration using annotation @SpringBootApplication

javaspringgradlespring-boot

提问by Piotr ?ak

I have problem with Spring Boot configuration.

我对 Spring Boot 配置有问题。

I have created base Spring Boot project using https://start.spring.io/

我已经使用https://start.spring.io/创建了基本的 Spring Boot 项目

And I have a problem, configuration works only for classes in sub catalog:

我有一个问题,配置仅适用于子目录中的类:

enter image description here

在此处输入图片说明

I have tried annotation @ComponentScanbut it didn't help.

我试过注释@ComponentScan,但没有帮助。

Do You have any idea what can I do with this?

你知道我能用这个做什么吗?

采纳答案by Sotirios Delimanolis

The Spring Boot documentation for @SpringBootApplicationstates

春季启动文档@SpringBootApplication状态

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfigurationand @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplicationalternative.

The @SpringBootApplicationannotation is equivalent to using @Configuration, @EnableAutoConfigurationand @ComponentScanwith their default attributes: [...]

许多 Spring Boot 开发人员总是用@Configuration,@EnableAutoConfiguration和注释他们的主类@ComponentScan。由于这些注释经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot 提供了一个方便的@SpringBootApplication替代方案。

@SpringBootApplication注解相当于使用 @Configuration@EnableAutoConfiguration@ComponentScan与他们的默认属性:[...]

where the @ComponentScanjavadoc states

其中@ComponentScan的javadoc规定

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

如果没有定义特定的包,将从声明该注解的类的包开始扫描。

That is, only the types that are in the same package as your ReadingListApplicationwill be scanned.

也就是说,只会ReadingListApplication扫描与您在同一包中的类型。

If you want a custom configuration, provide your own @Configuration, @EnableAutoConfiguration, and @ComponentScan, as appropriate.

如果你想有一个自定义配置,提供自己的@Configuration@EnableAutoConfiguration@ComponentScan适当。

回答by Kabir

When setting up a Spring bootproject, have your Application class (the one that contains the @SpringBootApplicationannotation in the base package.

设置Spring boot项目时,请让您的 Application 类(包含@SpringBootApplication基本包中的注释的类)。

One of the things the @SpringBootApplicationdoes is a component scan. But, it only scans on sub-packages. i.e. if you put that class in com.mypackage, then it will scan for all classes in sub-packages i.e. com.mypackage.*.

@SpringBootApplication所做的一件事是组件扫描。但是,它只扫描子包。即,如果您将该类放在com.mypackage 中,那么它将扫描子包中的所有类,即 com.mypackage.*。

If you do not want to do it this way, you can also add a @ComponentScanto a class specifying the root package i.e @ComponentScan("com.mypackage")

如果你不想这样做,你也可以在@ComponentScan指定根包的类中添加一个ie@ComponentScan("com.mypackage")

I would recommend you have a base package i.e com.mypackage. And within those packages, have your sub-packages. Have you class containing the @SpringBootApplicationin that base package.

我建议你有一个基础包,即com.mypackage。在这些包中,有你的子包。你有没有包含@SpringBootApplication在那个基本包中的类。

回答by alvgarvilla

Checking the Spring documentation:

检查 Spring 文档:

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html

You can override, with the @SpringBootApplication, the default values of component scan. You just need to include it as a parameters:

您可以使用@SpringBootApplication 覆盖组件扫描的默认值。您只需要将其作为参数包含:

@SpringBootApplication(scanBasePackages = "entertainment")

@SpringBootApplication(scanBasePackages = "entertainment")

or String array:

或字符串数​​组:

@SpringBootApplication(scanBasePackages = {"entertainment", "readinglist"})

@SpringBootApplication(scanBasePackages = {"entertainment", "readinglist"})

回答by José Oliveira

I was having the same problem and to solve it I renamed my packages like this.

我遇到了同样的问题,为了解决它,我像这样重命名了我的包。

"com.project"

“com.project”

there you can place your SpringBootAplication main class, then just create the others packages beginning with "com.project"

在那里你可以放置你的 SpringBootAplication 主类,然后只需创建以“com.project”开头的其他包

"com.project.dao"

"com.project.controller"

“com.project.dao”

“com.project.controller”

Creating this sub project structure you have no need to use scanBasePackages in @SpringBootApplication annotation, doing this your main class will be able to find every component in your project.

创建此子项目结构时,您无需在 @SpringBootApplication 注释中使用 scanBasePackages,这样做您的主类将能够找到项目中的每个组件。

And in case you chose to use scanBasePackages remember that you need to set all your components packageslike this.

如果您选择使用 scanBasePackages,请记住您需要像这样设置所有组件包

@SpringBootApplication(scanBasePackages = {"com.project.dao", "com.project.controller"})

@SpringBootApplication(scanBasePackages = {"com.project.dao", "com.project.controller"})