重生之 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 后再次访问
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意6 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码6 天前
嵌入式学习路线
学习
毛小茛6 天前
计算机系统概论——校验码
学习
babe小鑫6 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms6 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下6 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。6 天前
2026.2.25监控学习
学习
im_AMBER6 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J6 天前
从“Hello World“ 开始 C++
c语言·c++·学习