SpringBoot概述

SpringBoot

1. SpringBoot出现的原因

由于Spring的强大(主要是IOC和AOP),所以导致它的功能的边界越来越大,但是也造成Spring的项目越来越臃肿.大家在整合SSM项目的时候,如果前期你的业务逻辑比较简单的情况下,可能感觉配置 1 小时,编程 5 分钟,所以为了解决这个痛点,Spring开启了SpringBoot的项目,目的就是做一个基于Spring但是却能做到简化

配置,开箱即用

2. 额外补充版本号代表的意义

到SpringBoot官网看历史版本号,发现有很多不同的标识,比如2.7.0-SNAPSHOT

  • 第一个 2 指的是SpringBoot的大版本号

  • 第二个 7 代表小版本号

  • 第三个 0 代表增量版本

最后可能还会看到SNAPSHOT(快照版本),RELEASE(当前版本的最终版),M(里程碑版本,一般是出现了重大更新),RC(候选版本,基本已经不存在明显bug),GA(General Available正式版),Alpha测试版的最初版本,一般只在开发人员内部使用交流,Beta(相较于Alpha版本已经消除了绝大多数明显的bug),PRE(预览版本)

3 .新建SpringBoot项目

通过https://start.spring.io创建SpringBoot工程,也可以通过IDE继承的插件构建

SpringBoot的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>
    <!-- 父模块的坐标,在SpingBoot中该坐标和当前工程中的SpringBoot依赖相等 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.softeem</groupId>
    <artifactId>vip3-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>vip3-springboot</name>

    <description>vip3学习SpringBoot2</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <exclusions>
                <exclusion>
                    <!-- 因为SpringBoot的测试组建中,既存在Junit4,也存在Junit5,所以最好排除一个,
                    在SpringBoot中推荐排除Junit4-->
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!--这一段插件的配置,是必须给的,因为它负责通过maven去构建运行SpringBoot项目 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4. 配置文件用法

4. 1 基本使用

SpringBoot遵从约定大于配置,所以除非你要更改默认配置,才需要在配置文件中更改
SpringBoot支持两种格式的配置文件:①properties②yml
配置文件的名字一定要叫application,如果两种格式的配置文件都存在(基本不会发生),那么相同的配置以properties为准,不冲突的配置,谁配置了就以谁的为准(只要有就重写)

# Tomcat的端口号改成 8081
server.port=8001
# 还可以使用SpEL的随机数
#server.port=${random.int(8080,8888)}
# 项目的访问路径,必须以/打头
server.servlet.context-path=/vip
# yml注意两点.1.注意缩进 2.注意冒号后面一定要跟空格
server:
  port: 8081
  servlet:
    context-path:

4. 2 自定义属性的注入

## 
##自定义配置#
## 

softeem:
username: muzi
outlook: ${softeem.username}handsome
age: 18

要么通过@Value注入;要么如果有公共的前缀,则可以使用@ConfigurationProperties注入

@Component
//使用@ConfigurationProperties的时候要保证,属性名和配置的名字保持一致
@ConfigurationProperties(prefix = "softeem")
public class MyPropertiesConfig {
// @Value是通过反射注入的,建议放在setter上面
// @Value("${softeem.username}")
private String username;
// @Value("${softeem.outlook}")
private String outlook;
// @Value("${softeem.age}")
private int age;
//省略getter/setter
}

4. 3 自定义的配置文件注入

/resource/jdbc.properties

jdbc.driverClassName= 1111
jdbc.url= 222
jdbc.username= 333
jdbc.password= 444
//<context:property-placeholder location="classpath:jdbc.properties"/>
@Component
@PropertySource(value = "classpath:jdbc.properties")
public class MyPropertiesConfig2 {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值