spring boot里,扫描的class对象是用注解@ComponentScan(未指定的情况下默认是main函数所在package)来指定的
新建两个新包
我们在项目中新建两个包cn.kfit ; org.kfit ;
新建两个测试类
package cn.kfit;
import org.springframework.boot.CommandLineRunner;
@Configuration
publicclass MyCommandLineRunner1 implements CommandLineRunner {
@Override
publicvoid run(String... args) throws Exception {
System.out.println("MyCommandLineRunner1.run()");
}
}
package org.kfit;
import org.springframework.boot.CommandLineRunner;
@Configuration
publicclass MyCommandLineRunner2 implements CommandLineRunner {
@Override
publicvoid run(String... args) throws Exception {
System.out.println("MyCommandLineRunner2.run()");
}
}
在App.java类中加入如下注解:
//可以使用:basePackageClasses={},basePackages={}
@ComponentScan(basePackages={"cn.kfit","org.kfit"})
启动如果看到打印信息:
MyCommandLineRunner1.run()
MyCommandLineRunner2.run()