
引导类定义Spring Boot 的引导类也叫启动类 / 主类 是整个 Spring Boot 项目的「入口」和「核心开关」—— 所有 Spring Boot 项目都必须有且仅有一个引导类它负责启动 Spring 容器、加载自动配置、初始化整个应用。代码示例下面是一个Spring Boot 引导类基本结构// 核心注解标记这是 Spring Boot 应用的引导类SpringBootApplicationpublicclassDemo1Application{// 主方法Java 程序的入口也是 Spring Boot 启动的入口publicstaticvoidmain(String[]args){// 启动 Spring Boot 应用加载上下文、初始化容器SpringApplication.run(Demo1Application.class,args);}}SpringBootApplicationSpringBootApplication 是一个「组合注解」由下面3个核心注解组合而成SpringBootConfiguration标记这是一个配置类说明这个类可以定义BeanEnableAutoConfiguration开启自动配置自动识别场景比如引入了 Web Starter自动配置相关组件比如 Tomcat、DispatcherServlet不用手动写 XML/Java 配置ComponentScan组件扫描自动扫描当前包及其子包下的「组件类」比如 Controller、Service、Repository、Component 注解的类并把这些类交给 Spring 容器管理ComponentScan 的扫描范围默认是「引导类所在包 所有子包」。比如引导类在 com.example.demo1 包下那么 com.example.demo1.controller、com.example.demo1.service 都会被扫描如果组件类放在 com.example.other 包下需要手动指定扫描范围SpringBootApplication(scanBasePackagescom.example)// 扫描 com.example 下所有包// 或指定具体包SpringBootApplication(scanBasePackages{com.example.demo1,com.example.other})SpringApplication.run ()初始化 Spring 容器创建 ApplicationContextSpring 上下文这是 Spring 管理所有 Bean 的核心容器加载自动配置根据 EnableAutoConfiguration 扫描并加载所有 Starter 的自动配置类比如 Web Starter 的 WebMvcAutoConfiguration扫描并注册组件根据 ComponentScan 扫描组件把 Controller、Service 等类注册到容器中启动内嵌服务器如果引入了 Web Starter自动启动内置 Tomcat默认端口 8080监听应用事件触发应用启动的各个生命周期事件比如启动前、启动后。学习疑惑Bean// 定义一个服务类publicclassUserService{publicStringsayHello(){returnHello World;}}// 使用时手动new对象publicclassTest{publicstaticvoidmain(String[]args){UserServiceuserServicenewUserService();// 手动创建对象System.out.println(userService.sayHello());}}如果你要创建一个 Service 类的对象需要手动 new。但是如果项目里面有很多个类手动 new 会导致代码混乱还难管理依赖。// 加 Service 注解 → 告诉 Spring这个类是 Bean交给你管理ServicepublicclassUserService{publicStringsayHello(){returnHello World;}}// 控制器中使用 Bean不用 new直接注入RestControllerpublicclassUserController{// Autowired告诉 Spring 容器“把 UserService 这个 Bean 递给我”AutowiredprivateUserServiceuserService;GetMapping(/hello)publicStringhello(){// 直接用容器管理的 Bean不用自己 newreturnuserService.sayHello();}}Spring Boot 中的 Bean只需要给类加特定注解Spring 容器就会自动创建、管理这个类的对象这个对象就是 Bean。哪些类会被 Spring 识别为 BeanSpring Boot 中只要加了以下注解的类都会被 扫描并注册为 Bean引导类的核心功能之一Controller控制器处理接口请求Service服务类处理业务逻辑Repository数据访问类操作数据库比如 MapperComponent通用组件以上注解的父注解Configuration配置类比如定义 Bean 方法spring容器Spring 容器核心是 ApplicationContext是一个 “智能管家”它的核心工作就是1创建 Bean启动 Spring Boot 时引导类的 SpringApplication.run()容器会扫描所有加了注解的类自动创建这些类的对象不用你 new。2管理 Bean 的依赖比如 UserController 里需要 UserService容器会自动把 UserService Bean 注入到 UserController 中Autowired 就是干这个的解决类之间的依赖问题。3控制 Bean 的生命周期从 Bean 的创建 → 初始化 → 使用 → 销毁全由容器把控比如可以通过 PostConstruct 注解指定 Bean 初始化后执行的方法。4加载配置容器会加载 Starter 提供的自动配置类比如 Web Starter 的 Tomcat 配置、application.yml 中的自定义配置让 Bean 按规则运行。组件之间的关联parent统一依赖版本确保容器能识别所有 Starter 引入的类比如 Tomcat、Spring MVCStarter引入功能依赖同时提供自动配置类告诉容器该怎么创建这些依赖的 Bean比如 Tomcat Bean、DispatcherServlet Bean引导类启动时创建 Spring 容器扫描你的业务类Controller/Service并注册为 Bean最终让所有 Bean 协同工作。实操验证SpringBootApplicationpublicclassDemo1Application{publicstaticvoidmain(String[]args){// 启动容器并获取上下文对象ConfigurableApplicationContextcontextSpringApplication.run(Demo1Application.class,args);// 打印所有 Bean 的名称String[]beanNamescontext.getBeanDefinitionNames();for(StringbeanName:beanNames){System.out.println(Bean 名称beanName);}}}parent starter 和引导类的关系