重生之 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 后再次访问
相关推荐
lilihuigz6 小时前
Tutor LMS 4.0 Beta版全新上线:以学习者为中心的移动优先学习体验
学习·在线教育·lms
kuinnebula9 小时前
RTSP学习
学习
北顾笙98011 小时前
LLM学习-day04
学习
lzj_pxxw12 小时前
W25Q64存储芯片 软件设计刚需常识
stm32·单片机·嵌入式硬件·mcu·学习
Slow菜鸟13 小时前
AI学习篇(四) | AI设计类Skills推荐清单(2026年)
人工智能·学习
念恒1230613 小时前
Python(列表进阶)
python·学习
QYQ_112714 小时前
嵌入式学习——杂项设备、Platform总线和设备树源文件
学习
wuxinyan12315 小时前
大模型学习之路03:提示工程从入门到精通(第三篇)
人工智能·python·学习
十安_数学好题速析16 小时前
【多选】曲线方程:四步避坑判断曲线类型
笔记·学习·高考
千寻girling16 小时前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php