当前位置: 首页 > news >正文

Spring-boot读书笔记一主类看起来无所关联,却能运行完整项目的原因探索

**//The BootDemoApplication class**
package com.tutorial.bootdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BootDemoApplication {public static void main(String[] args) {SpringApplication.run(BootDemoApplication.class, args);}}

The BootDemoApplication class doesn't have explicit imports or references to other classes, but Spring Boot works through "magic" - specifically through annotations and component scanning.

Here's how it works:

  1. @SpringBootApplication Annotation
    This single annotation is actually a combination of three annotations:
  • @Configuration - Marks this as a configuration class
  • @EnableAutoConfiguration - Enables Spring Boot's auto-configuration
  • @ComponentScan - This is the key! It scans for components

2. Component Scanning
The @ComponentScan (part of @SpringBootApplication) automatically scans the current package and all sub-packages for Spring components:

com.tutorial.bootdemo/ ← Base package (where BootDemoApplication is)
├── controller/ ← Scanned automatically
│ └── StudentController.java ← @RestController found and registered
├── service/ ← Scanned automatically
│ └── StudentServiceimpl.java ← @Service found and registered
├── dao/ ← Scanned automatically
│ └── StudentRepository.java ← @Repository found and registered
└── BootDemoApplication.java ← Starting point

3. Spring Annotations Auto-Discovery
Spring Boot automatically finds and registers:

  • @RestController (StudentController)
  • @Service (StudentServiceimpl)
  • @Repository (StudentRepository)
  • @Entity (Student)

4. Dependency Injection
Spring automatically wires dependencies using @Autowired:
Controller → Service → Repository

5. Auto-Configuration
Spring Boot automatically configures:

  • Database connections (from application.properties)
  • JPA/Hibernate setup
  • Web server (Tomcat)
  • JSON serialization

So the "magic" happens because:

  • @SpringBootApplication triggers component scanning
  • All your classes are in sub-packages of com.tutorial.bootdemo
  • Spring finds all annotated classes automatically
  • Spring wires everything together using dependency injection

This is why you don't need explicit imports - Spring Boot discovers and connects everything automatically based on the package structure and annotations!

http://www.rkmt.cn/news/192598.html

相关文章:

  • 智能合约安全审计的三维测试体系
  • Clang内存泄漏检测实战(20年专家经验总结)
  • 揭秘Python调用C代码性能瓶颈:如何用CFFI实现零开销接口调用
  • 学习threejs,使用自定义GLSL 着色器,实现抽象艺术特效 - 实践
  • 广西省贵港市自建房设计公司哪家强?2026年最新权威靠谱测评榜单抢先看 - 苏木2025
  • 为什么你的CUDA程序跑不快?深度剖析C语言内核编译的3大常见错误
  • Yomiuri Shimbun日媒接触:进入日本市场的第一步
  • USBlyzer抓包机制深度剖析:系统学习数据过滤策略
  • 广东省潮州市自建房设计公司哪家强?2026年最新权威靠谱测评榜单抢先看 - 苏木2025
  • 谁是TOP1?广西省北海市自建房设计公司评测排行榜 + 真实建房案例参考 - 苏木2025
  • 广东省云浮市自建房设计公司/机构权威测评推荐排行榜 - 苏木2025
  • AMP项目尝试:为Google用户提供闪电般加载体验
  • TinyML C语言内存管理实战(20年专家经验倾囊相授)
  • 耳鸣怎么办?
  • 【Clang内存泄漏检测终极指南】:掌握C语言高效排查技巧,告别程序崩溃
  • 快手光合计划参与:覆盖下沉市场用户群体
  • 【独家技术披露】:工业级C语言TensorRT延迟优化框架设计全过程
  • DeepSpeed与FSDP对比:大规模训练场景下的选择建议
  • 【高性能计算必看】:OpenMP 5.3中AI任务动态调度的5个关键优化点
  • 模型合并功能上线:LoRA权重一键集成至基础模型
  • AI创业新风口:利用开源镜像降低GPU算力成本吸引Token购买用户
  • Foundation_pose在自己的物体上复现指南:实现任意物体6D位姿检测(利用realsense_D435i和iphone_16pro手机) - 实践
  • (昇腾芯片开发者必备)C语言算子编写标准与性能调优全公开
  • 如何将TensorRT推理延迟压缩至1ms以内?,C语言极致优化实践
  • YubiKey硬件密钥支持:物理设备双重验证
  • KTO知识蒸馏对齐:无需参考答案即可完成模型优化
  • 超越MyBatisPlus的灵活性?看ms-swift如何插件化扩展大模型训练组件
  • 自动命名规则:根据时间地点生成修复后图片的文件名
  • 基于SpringBoot的养宠指南服务平台的设计与实现毕业设计源码
  • 基于SpringBoot的医患交流平台的设计与实现毕业设计