# Spring-Boot-learning
**Repository Path**: li_VillageHead/Spring-Boot-learning
## Basic Information
- **Project Name**: Spring-Boot-learning
- **Description**: Spring Boot实战
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2019-08-02
- **Last Updated**: 2025-06-13
## Categories & Tags
**Categories**: Uncategorized
**Tags**: 微服务学习, Java, JavaScript, HTML, SQL
## README
# Spring-Boot-learning
* Spring Boot实战
* https://gitee.com/sliesu/spring-boot-learning
* https://github.com/kaiwenyan007/spring-boot2-learning-master
* https://github.com/yudaocode/SpringBoot-Labs
*

本书涵盖 Spring、Spring MVC以及Spring Boot基础、核心、数据访问、Web开发、企业级开发、监控、部署、Spring Cloud入门等。

什么是Spring Boot
=================
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使
用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是spring boot其实不是什么新的框架,
它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。
Spring Boot特性理解
-------------------
* 为基于Spring的开发提供更快的入门体验。
* 开箱即用,没有代码生成,也无需XML配置,同时也可以修改默认值来满足特定的需求。
* 提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
* Spring Boot并不是对Spring功能上的增强,而是提供一种快速使用Spring的方式。
Spring Boot优缺点:
------------------
# 优点:
* (1)、快速构建项目;
* (2)、对主流开发框架的无配置集成;
* (3)、项目可独立运行,无须外部依赖Servlet容器。
* (4)、提供运行时的应用监控;
* (5)、极大地提高了开发、部署效率;
* (6)、与云计算的天然集成。
# 缺点:
* (1)、书籍文档较少且不够深入;
* (2)、如果不认同Spring框架,这也是它的缺点,但建议一定要使用Spring框架。
* 项目结构介绍:

* 另外,spingboot建议的目录结果如下:
* com
* +- example
* +- myproject
* +- Application.java
>* |
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- controller
| +- CustomerController.java
|
1、Application.java 建议放到跟目录下面,主要用于做一些框架配置。
2、domain目录主要用于实体(Entity)与数据访问层(Repository)。
3、service 层主要是业务类代码。
4、controller 负责页面访问控制。
采用默认配置可以省去很多配置,当然也可以根据自己的喜欢来进行更改最后,启动Application main方法,至此一个java项目搭建好了!
* Spring Boot核心
基本配置
=======
## 入口类和@SpringBootApplication
SpringBoot通常有一个名为*Application的入口类,入口类有一个面方法,这个main方法
其实就是一个标准的Java应用入口方法。在main方法中使用SpringApplication.run(Chapter52Application.class, args),
启动Spring Boot应用项目。
* @SpringBootApplication是Spring Boot的核心注解,它是一个组合注解,源码如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}),
@Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})})
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class>[] exclude() default {};
* @SpringBootApplication注解主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan;若不适用
@SpringBootApplication注解,则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。
* 其中,@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖当前项目进行自动配置。
例如,添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,Spring Boot会对Tomcat和Spring MVC
进行自动配置。
* Spring Boot配置文件
Spring Boot使用一个全局的配置文件application.properties或application.yml,放置在src/mian/resources目录或者类路径
的/config下。
Spring Boot不仅支持常规的properties配置文件,还支持yaml语言的配置文件。yaml是以数据为中心的语言,在配置数据的时候具有
面向对象的特征。
Spring Boot全局配置文件的作用是对一些默认的配置值进行修改。
## 使用Maven插件实现分环境配置和版本检查
git-commit-id-plugin
官网:https://github.com/git-commit-id/git-commit-id-maven-plugin
这个插件在分布式部署环境非常有用,可以通过此插件看出打包所处的git环境,分支、仓库、提交的head信息等。
包含两个目标:
1.git-commit-id:revision:将构建时的信息保存到指定文件中或maven的属性中。
2.git-commit-id:validateRevision:校验属性是否符合预期值,默认绑定阶段:verify。(这个不常用)
1. 增加git.properties信息和增加Controller查看信息
https://www.cnblogs.com/qlqwjy/p/13974847.html
### 类信息如下:
build.host是build的计算机名称。下面是提交信息以及分支信息、总提交次数等信息。
可以通过Controller暴露信息,这样可以通过Controller查看打包信息。
```java
package cn.qlq.git;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
/**
* @author: MI
* @date: 2023/10/13 12:45
* @description:
*/
@Configuration
@PropertySource(value = "classpath:git.properties", ignoreResourceNotFound = true)
public class BuildInfoProperties {
@Autowired
private Environment env;
@Bean
public BuildInfo getBuildInfo() {
BuildInfo buildInfo = new BuildInfo();
buildInfo.gitDirty = env.getProperty("git.dirty");
buildInfo.gitRemoteOriginUrl = env.getProperty("git.remote.origin.url");
buildInfo.gitTags = env.getProperty("git.tags");
buildInfo.gitBranch = env.getProperty("git.branch");
buildInfo.gitCommitUserName = env.getProperty("git.commit.user.name");
buildInfo.gitCommitTime = env.getProperty("git.commit.time");
buildInfo.gitCommitMessageFull = env.getProperty("git.commit.message.full");
buildInfo.gitCommitId = env.getProperty("git.commit.id");
buildInfo.gitBuildVersion = env.getProperty("git.build.version");
buildInfo.gitBuildUserName = env.getProperty("git.build.user.name");
buildInfo.gitBuildTime = env.getProperty("git.build.time");
buildInfo.gitBuildHost = env.getProperty("git.build.host");
return buildInfo;
}
@Data
public static class BuildInfo {
private String gitDirty;
private String gitRemoteOriginUrl;
private String gitTags;
private String gitBranch;
private String gitCommitUserName;
private String gitCommitTime;
private String gitCommitMessageFull;
private String gitCommitId;
private String gitBuildVersion;
private String gitBuildUserName;
private String gitBuildTime;
private String gitBuildHost;
}
}
```
### Controller信息如下:
```java
package cn.qlq.git;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: MI
* @date: 2023/10/13 12:46
* @description:
*/
@RestController
public class BuildInfoController {
@Autowired
private BuildInfoProperties.BuildInfo buildInfo;
@GetMapping(value = "/buildinfo", produces = {"application/json;charset=UTF-8"})
public String getBuildInfo() {
return buildInfo.toString();
}
}
```