使用注解@SpringBootApplication 进行配置 [英] Configuration using annotation @SpringBootApplication

查看:44
本文介绍了使用注解@SpringBootApplication 进行配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Spring Boot 配置有问题.

我已经使用

我尝试了注释 @ComponentScan 但没有帮助.

你知道我可以用它做什么吗?

解决方案

@SpringBootApplication 状态的 Spring Boot 文档

<块引用>

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

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

其中@ComponentScan javadoc 状态

<块引用>

如果没有定义特定的包,扫描将从声明此注解的类的包.

也就是说,只会扫描与您的 ReadingListApplication 位于同一包中的类型.

如果您想要自定义配置,请酌情提供您自己的 @Configuration@EnableAutoConfiguration@ComponentScan.

I have problem with Spring Boot configuration.

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

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

I have tried annotation @ComponentScan but it didn't help.

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

解决方案

The Spring Boot documentation for @SpringBootApplication states

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

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]

where the @ComponentScan javadoc states

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 ReadingListApplication will be scanned.

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

这篇关于使用注解@SpringBootApplication 进行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆