重生之 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 后再次访问
相关推荐
AA陈超15 分钟前
LyraStarterGame_5.6 Experience系统加载流程详细实现
c++·笔记·学习·ue5·虚幻引擎·lyra
秋深枫叶红17 分钟前
嵌入式第三十四篇——linux系统编程——进程
linux·服务器·数据库·学习
d111111111d1 小时前
STM32得中断服务函数,为什么不能有返回值
笔记·stm32·单片机·嵌入式硬件·学习
阿蒙Amon1 小时前
JavaScript学习笔记:12.类
javascript·笔记·学习
光影少年1 小时前
PostgreSQL数据库学习路线
数据库·学习·postgresql
wjykp1 小时前
part 3神经网络的学习
人工智能·神经网络·学习
阿蒙Amon1 小时前
JavaScript学习笔记:10.集合
javascript·笔记·学习
快撑死的鱼2 小时前
Llama-factory 详细学习笔记:第六章:DPO (直接偏好优化) 实战 (难点)
笔记·学习·llama
d111111111d2 小时前
连续形式PID和离散PID-详情学习-江科大(学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习
四维碎片2 小时前
【Qt】生产者-消费者模式学习笔记
笔记·qt·学习