spring-boot 改变自动扫描的包
配置
package com.maple.testpackage;import org.springframework.boot.CommandLineRunner;import org.springframework.context.annotation.Configuration;@Configurationpublic class CustomScan implements CommandLineRunner{ @Override public void run(String... args) throws Exception { System.out.println("CustomScan.run()"); }}
package com.maple.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Import;import com.maple.springboot.config.CustomSettings;import com.maple.springboot.config.MapleSettings;import com.maple.springboot.util.SpringUtil;@SpringBootApplication@ServletComponentScan@Import(value={SpringUtil.class})@ComponentScan(basePackages={"com.maple.springboot","com.maple.testpackage"})@EnableConfigurationProperties({CustomSettings.class,MapleSettings.class})public class App { public static void main(String[] args) { Class [] c = {App.class}; SpringApplication.run(c, args); }}
App.java 类的package 是com.maple.springboot 所以默认扫描是这个package 下的。如果还想扫描com.maple.testpackage需要添加如下注解 @ComponentScan(basePackages={"com.maple.springboot","com.maple.testpackage"})