大道之行,天下为公。这篇文章主要讲述(012)Spring Boot之改变SpringBootApplication注解默认扫描路径相关的知识,希望能为你提供帮助。
SpringBootApplication默认扫描当前包以及子包的所有类,如下所示,SpringBootApplication 注解与User在同一级,无法获取User的bean:
pom.xml
文章图片
文章图片
< ?xml version="1.0" encoding="UTF-8"?> < project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion> 4.0.0< /modelVersion> < groupId> com.edu.spring< /groupId> < artifactId> springboot< /artifactId> < version> 1.0.0< /version> < packaging> jar< /packaging> < name> springboot< /name> < !-- FIXME change it to the project\'s website --> < url> http://www.example.com< /url> < dependencyManagement> < dependencies> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-dependencies< /artifactId> < version> 2.1.6.RELEASE< /version> < scope> import< /scope> < type> pom< /type> < /dependency> < /dependencies> < /dependencyManagement> < properties> < project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding> < maven.compiler.source> 1.8< /maven.compiler.source> < maven.compiler.target> 1.8< /maven.compiler.target> < /properties> < dependencies> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter< /artifactId> < /dependency> < /dependencies> < /project>
View CodeUser.java
文章图片
文章图片
package com.edu.spring.user; import org.springframework.stereotype.Component; @Component public class User {}
View CodeApp.java
文章图片
文章图片
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.edu.spring.user.User; @SpringBootApplication public class App {public static void main( String[] args) { SpringApplication app=new SpringApplication(App.class); ConfigurableApplicationContext context= app.run(args); System.out.println(context.getBean(User.class)); context.close(); } }
View Code目录结构:
文章图片
运行结果如下:
文章图片
可以通过scanBasePackages指定扫描路径
App.java,修改为@SpringBootApplication(scanBasePackages={"com.edu.spring"})
文章图片
文章图片
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.edu.spring.user.User; @SpringBootApplication(scanBasePackages={"com.edu.spring"}) public class App {public static void main( String[] args) { SpringApplication app=new SpringApplication(App.class); ConfigurableApplicationContext context= app.run(args); System.out.println(context.getBean(User.class)); context.close(); } }
View Code运行结果如下:
文章图片
exclude和excludeName可以排除掉要扫描的类,如下将不会获取User的bean:
【(012)Spring Boot之改变SpringBootApplication注解默认扫描路径】@SpringBootApplication(scanBasePackages={"com.edu.spring"},exclude=User.class)
@SpringBootApplication(scanBasePackages={"com.edu.spring"},excludeName="com.edu.spring.user.User")
推荐阅读
- 安卓设备连接Mac的简单方法
- The happy secret to better work,https://www.ted.com/talks/shawn_achor_the_happy_secret_to_better_wor
- APP嵌入移动端银行卡识别SDK,通过手机摄像头扫描提取银行卡号
- apply和call的使用和区别
- MyBatis modelxmlmapper 自动生成,附源码
- Hbuilder 生成移动App资源升级包
- 如何查看android studio sdk路径配置
- 基于Android 6.0 的蓝牙开发-- 发送数据,接收数据,实现改变色板功能
- 基于Android 6.0 的蓝牙开发-- 开启,关闭,搜索,连接