springboot改变自动扫描的包

来源:互联网 发布:网络攻防渗透 编辑:程序博客网 时间:2024/05/16 10:11

spring boot里,扫描的class对象是用注解@ComponentScan(未指定的情况下默认是main函数所在package)来指定的
新建两个新包
我们在项目中新建两个包cn.kfit ; org.kfit ;
新建两个测试类

package cn.kfit;import org.springframework.boot.CommandLineRunner;@Configurationpublicclass MyCommandLineRunner1 implements CommandLineRunner {    @Override    publicvoid run(String... args) throws Exception {       System.out.println("MyCommandLineRunner1.run()");    }}package org.kfit;import org.springframework.boot.CommandLineRunner;@Configurationpublicclass 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()
阅读全文
0 1