重生之 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 后再次访问
相关推荐
brave and determined16 分钟前
传感器学习(day19):ToF传感技术:从测距到三维视觉革命
嵌入式硬件·学习·嵌入式系统·st·tof·嵌入式设计·flightsense
复业思维202401081 小时前
Altium Designer (24.2.2)中更改库以及保持器件参数不变
笔记·学习·硬件工程
巧克力味的桃子1 小时前
进制转换3 学习笔记
笔记·学习
Purple Coder2 小时前
人工智能学习路线
学习
小帅学编程2 小时前
Spring(侧重注解开发)
java·学习·spring
爱喝水的鱼丶2 小时前
SAP-ABAP:在SAP世界里与特殊字符“斗智斗勇”:一份来自实战的避坑指南
运维·服务器·数据库·学习·sap·abap·特殊字符
科技林总2 小时前
【系统分析师】认证介绍
学习
不吃橘子的橘猫2 小时前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
刘孬孬沉迷学习3 小时前
层与天线的区别
网络·学习·5g·信息与通信·mimo·预编码·层映射
冬夜戏雪3 小时前
【学习日记】【12.30】【14/60】
服务器·网络·学习