欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

SpringBoot基础篇(二):HelloWorld细节探究

发布时间:2025/3/8 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SpringBoot基础篇(二):HelloWorld细节探究 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、场景启动器

1.1依赖

<!--Hello World项目的父工程是org.springframework.boot--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version><relativePath/></parent><!--org.springframework.boot他的父项目是spring-boot-dependencies他来真正管理Spring Boot应用里面的所有依赖版本;Spring Boot的版本仲裁中心;以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要声明版本号)--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.1.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath></parent>

1.2、SpringBoot场景启动器

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>

​ spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器.

2、自动配置

package com.lizhengi;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** springBootApplication:标注一个主程序类,表示这个是一个Springboot应用*/@SpringBootApplication public class HelloWorldApplication {public static void main(String[] args) {//Spring应用启动SpringApplication.run(HelloWorldApplication.class, args);} }

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

看一下@SpringBootApplication这个注解类的源码

@Target({ElementType.TYPE}) //可以给一个类型进行注解,比如类、接口、枚举 @Retention(RetentionPolicy.RUNTIME) //可以保留到程序运行的时候,它会被加载进入到 JVM 中 @Documented //将注解中的元素包含到 Javadoc 中去。 @Inherited //继承,比如A类上有该注解,B类继承A类,B类就也拥有该注解@SpringBootConfiguration@EnableAutoConfiguration/* *创建一个配置类,在配置类上添加 @ComponentScan 注解。 *该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>。 */ @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} ), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication
  • @SpringBootConfiguration:Spring Boot的配置类;标注在某个类上,表示这是一个Spring
    Boot的配置类;
  • @SpringBootConfiguration:Spring Boot的配置类;标注在某个类上,表示这是一个Spring
    Boot的配置类;
  • @EnableAutoConfiguration:开启自动配置功能:以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;
  • @AutoConfigurationPackage:自动配置包。

总结

以上是生活随笔为你收集整理的SpringBoot基础篇(二):HelloWorld细节探究的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。