首页 > 解决方案 > sping boot - how to configure application class in a different package, and controllers and services in other package

问题描述

We generally define our main application class in a root package above other classes.

The Spring Boot Application annotation in the main application class implicitly loads other classes as its the base package.

MY Question my main class is in com.example.main my controller class package is com.example.controller

when i run as Spring boot app, application is loaded but the rest API throws 404, how to configure parallel

标签: spring-boot

解决方案


由于您的主类不在根包中,因此您需要@componentScan在主类中进行如下注释。

@SpringBootApplication
@ComponentScan({"com.example.controller"})
public class SpringBootApplication {
//...your code.
}

如果要扫描所有子类,则需要在 componentScan 中传递根包路径

 @ComponentScan({"com.example"})

推荐阅读