当前位置: 首页 > 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.jsqmd.com/news/176558/

相关文章:

  • MLCC dc bias character
  • 2025-2026广西省贺州市自建房设计公司权威测评排行榜:核心推荐机构深度解析 - 苏木2025
  • 微博话题运营:发起#我的第一个大模型#挑战活动
  • 谁是TOP1?海南省海口市自建房设计公司评测排行榜 + 真实建房案例参考 - 苏木2025
  • 【工业物联网实战】:基于C语言的边缘节点功耗监控与自适应调控方案
  • 打工人上班摸魚小說-第二章 带薪拉屎、策略划水与隐藏技能
  • 告别网盘限速!使用AI镜像站实现大模型文件直链高速下载
  • 2025 RDA年终复盘:从“上海方案”到全球共识,2026年三大战役即将打响
  • Clang内存泄漏检测实战(20年专家经验总结)
  • 揭秘Python调用C代码性能瓶颈:如何用CFFI实现零开销接口调用
  • Cell Reports Physical Science:交叉学科创新潜力展示
  • 为什么你的CUDA程序跑不快?深度剖析C语言内核编译的3大常见错误
  • 无需翻墙!国内高速镜像站一键拉取开源大模型(含ComfyUI、Three.js)
  • 广西省贵港市自建房设计公司哪家强?2026年最新权威靠谱测评榜单抢先看 - 苏木2025
  • 学习threejs,使用自定义GLSL 着色器,实现抽象艺术特效 - 实践
  • 通俗解释为何未激活的Multisim打不开主数据库
  • Mathtype公式识别升级之路:多模态大模型加持OCR精准解析
  • 广西省来宾市自建房设计公司哪家强?2025最新评测排行榜 + 5 星企业推荐 - 苏木2025
  • 广西省南宁市自建房设计公司评测排行榜:6 家主流企业实地测评,哪家更靠谱? - 苏木2025
  • 一文搞懂YOLOv8:从Git下载、环境配置到模型推理全流程
  • InfoQ专题约稿:争取被收录进AI频道头条推荐
  • 广西省柳州市自建房设计公司排行榜出炉!权威评测 + 真实案例,建房选对不踩坑 - 苏木2025
  • 广西省崇左市自建房设计公司权威评测排行榜:多维度打分+5星企业全解析 - 苏木2025
  • 想在广西省玉林市农村盖房子,靠谱的自建房设计公司口碑推荐 - 苏木2025
  • 技术博客引流实操:用高质量内容吸引潜在客户购买Token
  • CPO偏好优化进阶:控制模型输出风格与伦理边界
  • Yomiuri Shimbun日媒接触:进入日本市场的第一步
  • USBlyzer抓包机制深度剖析:系统学习数据过滤策略
  • 广西省百色市自建房设计公司/机构权威测评推荐排行榜 - 苏木2025
  • ACL/EMNLP会议投稿:以研究成果推动工具影响力