SpringBoot核心功能与基础配置

SpringBoot简介

原先的Spring程序缺点,包括依赖设置繁琐,每项jar的引用都需要自己撰写。并且配置繁琐,配置文件中也需要自己写加载bean等。由此针对原始的Spring程序,Pivotal团队提供的全新框架——SpringBoot,其设计目的是用来简化Spring应用的初始搭建以及开发过程

1. SpringBoot的核心功能及优点

  • 起步依赖(简化依赖配置)
  • 自动配置(简化常用工程相关配置)
  • 辅助功能(内置服务器,……)

1.1 Parent

在进行开发时,对依赖版本具有固定搭配,故SpringBoot将所有基本版本常见的方案进行了整理,开发者可直接使用其提供的版本方案,不用担心冲突。故SpringBoot提供了parentparent自身具有很多个版本,每个parent版本中包含有几百个其他技术的版本号。parent帮助开发者统一的进行各种技术的版本管理。

注意parent定义出来以后,并不是直接使用,仅仅是给开发者一个说明书。

总结:继承spring-boot-starter-parent,其中定义了若干个依赖管理。继承parent模块可以避免多个依赖使用相同技术时出现依赖版本冲突。

  • 在pom.xml文件中,继承了一个spring-boot-starter-parent的坐标
    在这里插入图片描述

  • 打开后可以查阅到其中又继承了一个spring-boot-dependencies的坐标
    在这里插入图片描述

  • 打开 spring-boot-dependencies ,可以看到其 properties 下,列出了各式各样的依赖版本号属性,定义了若干个技术的依赖版本号。
    在这里插入图片描述

  • 而后的 dependencyManagement 中,列出了依赖坐标信息,其中依赖没有具体的依赖版本号,而是引用了上面定义的依赖版本属性值
    在这里插入图片描述

1.2 Starter

在实际开发时,对于依赖坐标的使用往往都有一些固定的组合方式,繁琐且格式固定,没有技术含量。由此SpringBoot提供了starter,定义了使用某种技术时对于依赖的固定搭配格式,一次配置就可引入一组依赖,使用starter可以帮助开发者减少依赖配置

命名规则:spring-boot-starter-技术名称

注意:

  1. starter是一个坐标中定了若干个坐标,以前写多个的,现在写一个,是用来减少依赖配置的书写量的
  2. parent是定义了几百个依赖版本号,以前写依赖需要自己手工控制版本,现在由SpringBoot统一管理,这样就不存在版本冲突了,是用来减少依赖冲突的
  • 在pom.xml文件中,引入 spring-boot-starter-web在这里插入图片描述
  • 打开 spring-boot-starter-web 后,可以看到其又定义了若干个具体依赖的坐标
    在这里插入图片描述

1.3 引导类

SpringBoot程序运行的入口就是工程创建时自带的,带有main方法的类,运行这个类就可以启动SpringBoot工程的运行。

package com.ty;import com.ty.controller.DemoController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class SpringbootDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDemoApplication.class, args);}
}

pringBoot本身是为了加速Spring程序的开发的,而Spring程序运行的基础是需要创建自己的Spring容器对象(IoC容器)并将所有的对象交给Spring的容器管理,也就是 Bean。当前SpringBoot启动类运行后就会产生一个Spring容器对象,通过容器对象直接操作Bean。

package com.ty;import com.ty.controller.DemoController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class SpringbootDemoApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(SpringbootDemoApplication.class, args);DemoController bean = run.getBean(DemoController.class);System.out.println(bean);}
}

1.4 内嵌tomcat

1.4.1 定位tomcat
在 SpringBoot 中,引入了 spring-boot-starter-web ,当打开 spring-boot-starter-web 便能看到,又引入了 spring-boot-starter-tomcat ,再打开这个starter,其中的 tomcat-embed-core,叫做tomcat内嵌核心,便是这个奖tomcat功能引入到了我们SpringBoot项目中。

在这里插入图片描述
1.4.2 更换内嵌Tomcat

SpringBoot提供了3款内置的服务器

  • tomcat(默认):apache出品,粉丝多,应用面广,负载了若干较重的组件

  • jetty:更轻量级,负载性能远不及tomcat

  • undertow:负载性能勉强跑赢tomcat

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>
</dependencies>

总结

  1. 内嵌Tomcat服务器是SpringBoot辅助功能之一
  2. 内嵌Tomcat工作原理是将Tomcat服务器作为对象运行,并将该对象交给Spring容器管理
  3. 变更内嵌服务器思想是去除现有服务器,添加全新的服务器

2. SpringBoot基础配置

2.1 配置文件分类

SpringBoot支持的配置文件:

  • properties格式:

    server.port=80
    
  • yml格式:

    server:port: 81
    
  • yaml格式:

    server:port: 82
    

2.2 配置文件优先级

  1. 配置文件间的加载优先级 properties> yml > yaml
  2. 不同配置文件中相同配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留

2.3 读取配置数据

2.3.1 Environment 读取全部配置数据

  • application.yml 中配置:

    datasource:driver: com.mysql.jdbc.Dirverurl: jdbc:mysql://localhost/springboot_dbusername: rootpassword: 123
    
  • 直接通过自动装配注解,将配置文件中的配置数据封装到 Environment 对象中。

    package com.ty.controller;import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;@RestController
    @RequestMapping("/demo")
    public class DemoController {@Autowiredprivate Environment environment;@GetMappingpublic String getByIds(){String id = "TY Demo SpringBoot is Running...";System.out.println(id);System.out.println("Driver" + environment.getProperty("driver"));System.out.println("Url" + environment.getProperty("url"));System.out.println("Username" + environment.getProperty("username"));System.out.println("Password" + environment.getProperty("password"));return id;}
    }
    

2.3.2 @ConfigurationProperties 读取配置对象数据

SpringBoot 可将一组配置数据封装为一个Java对象。

  • application.yml 中配置:

    datasource:driver: com.mysql.jdbc.Dirverurl: jdbc:mysql://localhost/springboot_dbusername: rootpassword: 123
    
  • 首先定义一个配置参数封装类,使用注解 @ConfigurationProperties,指定对象加载的配置文件中的信息。@ConfigurationProperties 必须告诉他加载的数据前缀,这样当前前缀下的所有属性就封装到这个对象中。

    package com.ty;import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;@Component
    @Data
    @ConfigurationProperties(prefix = "datasource")
    public class MyDataSource {private String driver;private String url;private String username;private String password;@Overridepublic String toString() {return "MyDataSource{" +"driver='" + driver + '\'' +", url='" + url + '\'' +", username='" + username + '\'' +", password='" + password + '\'' +'}';}
    }
    
  • 直接通过自动装配注解,引入 MyDataSource对象。

    package com.ty.controller;import com.ty.MyDataSource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;@RestController
    @RequestMapping("/demo")
    public class DemoController {@Autowiredprivate Environment environment;@AutowiredMyDataSource myDataSource;@GetMappingpublic String getById() {String id = "TY Demo SpringBoot is Running...";System.out.println(id);System.out.println("Driver" + myDataSource.getDriver());System.out.println("Url" + myDataSource.getUrl());System.out.println("Username" + myDataSource.getUsername());System.out.println("Password" + myDataSource.getPassword());return id;}
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/139486.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【笔记-OrCAD】WARNING(ORCAP-36038)解决办法

问题描述&#xff1a; OrCAD16.6绘制好原理图后&#xff0c;点击“*.dsn”文件可以生成网表&#xff0c;在存放原理图的文件内找到allegro文件夹&#xff0c;用记事本打开netlist.log文件&#xff0c;可以看到具体的警告原因&#xff0c;例如&#xff1a; WARNING(ORCAP-36038)…

C++11 正则表达式详解

目录 1 正则表达式语法1.1 字符和特殊字符1.2 限定符1.3 定位符1.4 选择和反向引用 2 C正则表达式标准库常用接口3 C正则表达式模板的使用3.1 匹配&#xff08;Match&#xff09;3.2 搜索&#xff08;Search&#xff09;3.3 分词&#xff08;Tokenize&#xff09;3.4 替换&…

多维时序 | MATLAB实现SSA-CNN-GRU-Attention多变量时间序列预测(SE注意力机制)

多维时序 | MATLAB实现SSA-CNN-GRU-Attention多变量时间序列预测&#xff08;SE注意力机制&#xff09; 目录 多维时序 | MATLAB实现SSA-CNN-GRU-Attention多变量时间序列预测&#xff08;SE注意力机制&#xff09;预测效果基本描述模型描述程序设计参考资料 预测效果 基本描述…

项目播报 | 璞华科技助力苏州巨迈科构建数字化管理体系

项目播报 近日&#xff0c;苏州巨迈科智能科技有限公司&#xff08;以下简称&#xff1a;苏州巨迈科&#xff09;签约璞华科技实施PLM项目&#xff0c;建立苏州巨迈科统一的研发管理平台&#xff0c;实现产品数据在部门间的共享&#xff0c;提升企业技术管理水平和综合竞争力&…

Python学习笔记——存储容器

食用说明&#xff1a;本笔记适用于有一定编程基础的伙伴们。希望有助于各位&#xff01; 列表 列表类似数组&#xff0c;其中可以包含不同类型的元素&#xff0c;写法如下&#xff1a; list1 [Google, Runoob, 1997, 2000] list2 [1, 2, 3, 4, 5 ] list3 ["a", …

【Linux】UDP协议

文章目录 &#x1f4d6; 前言1. 再谈端口号1.1 端口号划分范围&#xff1a;1.2 端口和进程的关系&#xff1a;1.2 - 1 netstat1.2 - 2 pidof 1.3 源端口和目的端口&#xff1a; 2. UDP协议2.1 UDP协议格式&#xff1a; 3. 再谈write/read4. UDP需要接收/发送缓冲区吗5. UDP使用…

Operator 开发实践 四 (WebHook)

1. WebHook介绍 我们知道访问Kubernetes API有好几种方式&#xff0c;比如使用kubectl命令、使用client-go之类的开发库、直接通过REST请求等。不管是一个使用kubectl的真人用户&#xff0c;还是一个Service Account&#xff0c;都可以通过API访问认证&#xff0c;这个过程官网…

5+非肿瘤分析,分型+WGCNA+机器学习筛选相关基因

今天给同学们分享一篇非肿瘤分型机器学习WGCNA实验的生信文章“Identification of diagnostic markers related to oxidative stress and inflammatory response in diabetic kidney disease by machine learning algorithms: Evidence from human transcriptomic data and mou…

竞赛选题 深度学习中文汉字识别

文章目录 0 前言1 数据集合2 网络构建3 模型训练4 模型性能评估5 文字预测6 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习中文汉字识别 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xf…

缺失找不到msvcr71.dll无法执行代码,应用程序无法启动的解决方法

最近我在使用电脑时遇到了一个问题&#xff0c;提示我缺少 msvcr71.dll 这个文件。这个文件是系统中的一个动态链接库文件&#xff0c;常用于支持一些运行在 Windows 系统上的程序。 当我发现这个问题时&#xff0c;我感到有点困惑和焦虑。因为我需要使用的软件要求系统中必须…

一键式AI智能剪辑,轻松处理视频,释放无限创意!“

想象一下&#xff0c;您可以在几秒钟内完成一个复杂的视频剪辑&#xff0c;而无需投入大量的时间和精力。现在&#xff0c;这个梦想已经成为现实&#xff01;我们的新一代AI智能剪辑技术&#xff0c;将使视频处理变得轻松无压力。 第一步&#xff1a;首先进入好简单批量智剪主…

漫谈下一代防火墙与Web应用防火墙的区别

如今&#xff0c;Web应用程序变得越来越复杂&#xff0c;更是黑客非常感兴趣的目标。在谈到网络安全的话题时&#xff0c;我们总会讨论下一代防火墙与Web应用防火墙的区别。当已经拥有下一代防火墙&#xff08;NGFW&#xff09;时&#xff0c;为什么需要Web应用程序防火墙&…