重生之 SpringBoot3 入门保姆级学习(04、 包扫描)

重生之 SpringBoot3 入门保姆级学习(04、 包扫描)

    • [2.1 包扫描](#2.1 包扫描)

2.1 包扫描


  • 默认包扫描规则:

    • @SpringBootApplication 标注的就是主程序

    • SpringBoot 只会扫描主程序下面的包 自动的 component-scan 功能

    • 在 @SpringBootApplication 添加参数可以增加包扫描范围 如:@SpringBootApplication(scanBasePackages = "com.zhong")

    • 在 @SpringBootApplication 直接使用注解 @ComponentScan("com.zhong") 指定扫描路径

  • 开始可以正常访问

  • 当 controller 包移动到 com.zhong 下面 不能正常访问
  • 解决办法:在 Boot302DemoApplication 上注解参数为 scanBasePackages = "com.zhong" 也就是你想要扫描的包的位置
java 复制代码
package com.zhong.boot302demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.zhong") // 添加包扫描位置
// @ComponentScan("com.zhong")     // 直接指定扫描路径
public class Boot302DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(Boot302DemoApplication.class, args);
    }

}
  • 重启 SpringBoot 后再次访问
相关推荐
wanzhong23331 小时前
ArcGIS学习-20 实战-地形研究
学习
wanzhong23331 小时前
ArcGIS学习-20 实战-县域水文分析
学习·arcgis
小马学嵌入式~1 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
索迪迈科技2 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
武文斌772 小时前
arm启动代码总结
arm开发·嵌入式硬件·学习
我怕是好3 小时前
学习stm32 蓝牙
stm32·嵌入式硬件·学习
索迪迈科技3 小时前
STM32F103C8T6开发板入门学习——点亮LED灯2
stm32·嵌入式硬件·学习
Learn Beyond Limits4 小时前
The learning process of Decision Tree Model|决策树模型学习过程
人工智能·深度学习·神经网络·学习·决策树·机器学习·ai
凯尔萨厮5 小时前
Java学习笔记三(封装)
java·笔记·学习
YoungUpUp5 小时前
【文件快速搜索神器Everything】实用工具强推——文件快速搜索神器Everything详细图文下载安装教程 办公学习必备软件
学习·everything·文件搜索·实用办公软件·everything 工具·文件快速搜索·搜索神器