@RestController 未找到,如果 main class 不在顶级包中
@RestController not found, if main class not in top level package
我有一个 class Resource
注释为 @RestController
,但它仅在主 class App
注释为 @SpringBootApplication
时使用] 在顶级包中。
作品:
com
+- test
+- project
+- App.java
|
+- resources
| +- Resource.java
无效:
com
+- test
+- project
+- app
| +- App.java
|
+- resources
| +- Resource.java
我假设您使用的是 @SpringBootApplication
注释。你应该知道它等同于使用@Configuration、@EnableAutoConfiguration 和@ComponentScan。
ComponentScan configures component scanning directives for use with
@Configuration classes. Provides support parallel with Spring XML's
element.
One of basePackageClasses(), basePackages() or its alias value() may
be specified to define specific packages to scan. If specific packages
are not defined scanning will occur from the package of the class with
this annotation.
您使用的是默认设置,因此您的扫描是在带有注释的 class 包中进行的。
您可以照原样移动它,也可以在@ComponentScan 中指定 basePackages。
在 documentation 中,您会发现建议将主要 class 置于所有其他 class 之上,以避免使用 basePackage
We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration
annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
...
Using a root package also allows the @ComponentScan
annotation to be used without needing to specify a basePackage
attribute. You can also use the @SpringBootApplication
annotation if your main class is in the root package.
我有一个 class Resource
注释为 @RestController
,但它仅在主 class App
注释为 @SpringBootApplication
时使用] 在顶级包中。
作品:
com
+- test
+- project
+- App.java
|
+- resources
| +- Resource.java
无效:
com
+- test
+- project
+- app
| +- App.java
|
+- resources
| +- Resource.java
我假设您使用的是 @SpringBootApplication
注释。你应该知道它等同于使用@Configuration、@EnableAutoConfiguration 和@ComponentScan。
ComponentScan configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's element.
One of basePackageClasses(), basePackages() or its alias value() may be specified to define specific packages to scan. If specific packages are not defined scanning will occur from the package of the class with this annotation.
您使用的是默认设置,因此您的扫描是在带有注释的 class 包中进行的。
您可以照原样移动它,也可以在@ComponentScan 中指定 basePackages。
在 documentation 中,您会发现建议将主要 class 置于所有其他 class 之上,以避免使用 basePackage
We generally recommend that you locate your main application class in a root package above other classes. The
@EnableAutoConfiguration
annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
...
Using a root package also allows the@ComponentScan
annotation to be used without needing to specify abasePackage
attribute. You can also use the@SpringBootApplication
annotation if your main class is in the root package.