@ComponentScan 告诉 Spring 从哪里找到 bean.
如果你的其他包都在 @SpringBootApplication 注解的启动类所在的包及其下级包, 则你什么都不用做, SpringBoot 会自动帮你把其他包都扫描了.
如果你有一些 bean 所在的包, 不在启动类的包及其下级包, 那么你需要手动加上 @ComponentScan 注解并指定那个 bean 所在的包.
- @SpringBootApplication
- @ComponentScan({"com.demo"})
- public class Application {
- private static final Logger LOG = LoggerFactory.getLogger(Application.class);
- public static void main(String[] args) {
- SpringApplication App = new SpringApplication(Application.class);
- App.setBannerMode(Banner.Mode.OFF);
- App.setwebEnvironment(true);
- App.run(args);
- LOG.info("**************** Startup Success ****************");
- }
- }
来源: http://www.bubuko.com/infodetail-3423370.html