JAVA分布式——Springboot(一)Springboot快速入门,application.properties,application.yml核心配置文件的解释,目录解释,自定义配置项,

一、springboot基础知识

1.1 概述

Spring Boot 可以非常容易和快速地创建基于 Spring 框架的应用程序,它让编码,配置,部署,监控。正因为 Spring Boot 它化繁为简,让开发变得极其简单和快速,所以在业界备受关注。

1.2 特性

  • 能够快速创建基于 Spring 的应用程序
  • 能够直接使用 java main 方法启动内嵌的 Tomcat 服务器运行 Spring Boot 程序,不需要部署 war 包文件
  • 提供约定的 starter POM 来简化 Maven 配置,让 Maven 的配置变得简单
  • 自动化配置,根据项目的 Maven 依赖配置,Spring boot 自动配置ssm等
  • 提供了程序的健康检查等功能
  • 基本可以完全不使用 XML 配置文件,采用注解配置

1.3 四大核心

  • 自动配置(重点

  • 起步依赖(重点

  • Actuator(非重点)

  • 命令行界面(非重点)

二、构建springboot框架

2.1 创建一个 Module,选择类型为 Spring Initializr 快速构建

在这里插入图片描述

2.2 设置 GAV 坐标及 pom 配置信息

在这里插入图片描述

2.3 选择 Spring Boot 版本及依赖

在这里插入图片描述

2.4 设置模块名称、Content Root 路径及模块文件的目录

在这里插入图片描述分别为:设置模块名称,设置模块根目录,设置模板文件目录

2.5 创建完毕

在这里插入图片描述

三、配置文件的解释说明

3.1 测试及编码注意形式

  • springboot包中的Application是程序的入口

  • 所有的类必须要在Application的同目录或者子目录下使用

在这里插入图片描述

3.2 pom.xml文件解释

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<!--继承 SpringBoot 框架的一个父项目,所有自己开发的 Spring Boot 都必须的继承-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<!--当前项目的 GAV 坐标-->
	<groupId>com.aiit.springboot</groupId>
	<artifactId>001-springboot-first</artifactId>
	<version>1.0.0</version>
	<name>001-springboot-first</name>

	<!--maven 项目名称,可以删除-->
	<name>001-springboot-first</name>
	<!--maven 项目描述,可以删除-->
	<description>Demo project for Spring Boot</description>
	<!--maven 属性配置,可以在其它地方通过${}方式进行引用-->
	<!--编译环境-->
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--Springboot框架web项目起步依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--springboot框架测试起步依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!--springboot项目打包编译的插件-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3.3 目录解释

在这里插入图片描述

  • .mvn|mvnw|mvnw.cmd:使用脚本操作执行 maven 相关命令,国内使用较少,可删除
  • .gitignore:使用版本控制工具 git 的时候,设置一些忽略提交的内容
  • static|templates:后面模板技术中存放文件的目录
  • application.properties:SpringBoot 的配置文件,很多集成的配置都可以在该文件中
    进行配置,例如:Spring、springMVC、Mybatis、Redis 等。目前是空的
  • Application.java:SpringBoot 程序执行的入口,执行该程序中的 main 方法,SpringBoot就启动了

3.3 核心配置文件properties

  • 默认采用该文件
  • 通过修改 application.properties 配置文件,在修改默认 tomcat 端口号及项目上下文件根键值对的 properties 属性文件配置方式
    在这里插入图片描述

3.4 核心配置文件yml文件

  • yaml 是一种直观的能够被计算机识别的的数据序列化格式,容易被人类阅读
  • yaml 类似于 xml,但是语法比 xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml 后缀也可以使用 yaml 后
    在这里插入图片描述
    在这里插入图片描述

3.5 properties和yml文件同时存在?

  • 首先两种文件的功能都是一样的

  • 当两种格式配置文件同时存在,使用的是.properties 配置文件

3.6 多环境配置(properties与yml文件一样)

  • 在实际开发的过程中,项目会经历很多的阶段(开发->测试->上线
  • 每个阶段的配置也会不同,例如:端口、上下文根、数据库等
  • 为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置

步骤一:为每个环境创建一个配置文件,命名必须以 application-环境标识.properties|yml
在这里插入图片描述
步骤二:在总配置文件 application.properties 进行环境的激活
在这里插入图片描述

四、 自定义配置项

在 SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,然后采用如下注解去读取配置的属性值

4.1 使用@value注解使用

  • 该注解主要用于自定义配置项比较少的情况,可以一个一个赋值
  • properties文件
    在这里插入图片描述
  • yml文件
    在这里插入图片描述

使用@Value()注解

@Controller
public class IndexController {
    @Value("${school.name}")
    private String school_name;
    @Value("${age}")
    private Integer age;
    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello(){
        return "hello world"+school_name;
    }
}

调试结果
在这里插入图片描述

4.2 使用@ConfigurationProperties

将整个文件映射成一个对象,用于自定义配置项比较多的情况

4.2.1 案例

  • properties文件
    在这里插入图片描述

  • 创建一个对象去关联这个配置文件
    在这里插入图片描述

  • 测试并访问
    在这里插入图片描述

  • 使用该注解的方式必须使用前缀。否则不能使用。且前缀统一

4.3 警告解决

  • 我们在使用上述的注解新建的类中顶部会出现警告如下:
    在这里插入图片描述
  • 在pom.xml添加以下配置进行消除
<!--解决使用@ConfigurationProperties 注解出现警告问题-->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
 <optional>true</optional>
</dependency>
  • 警告消除
    在这里插入图片描述

4.4 中文乱码问题解决

如果在 SpringBoot 核心配置(application.properties/yml)文件中有中文信息,会出现乱码

  • 一般在配置文件中,不建议出现中文(注释除外)
  • 如果有,可以先转化为 ASCII 码
    在这里插入图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值