重生之 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 后再次访问
相关推荐
是一个Bug3 小时前
Agent(智能体)应用 的入门学习路径
学习·机器学习
2301_809051143 小时前
Linux 网络编程 学习笔记
linux·网络·学习
eggcode4 小时前
【Qt学习】Linux(ARM架构)在线安装Qt6.x
linux·qt·学习·arm
_李小白5 小时前
【android opencv学习笔记】Day 26: 滤波算法之低通滤波与图像缩放插值
android·opencv·学习
Bechamz6 小时前
大数据开发学习Day43
大数据·学习
happymaker06268 小时前
SpringBoot学习日记——DAY06(整合MyBatisPlus的其他功能)
java·spring boot·学习
星夜夏空999 小时前
FreeRTOS学习(3)——FreeRTOS的移植与剪裁
学习
嵌入式×边缘AI:打怪升级日志10 小时前
硬件清单与学习进度存档
学习
Engineer邓祥浩11 小时前
软件设计师备考 第0章 题型分布、示例、学习路线
学习·职场和发展