StringBoot第一章

SpringBoot

jdk的要求

使用 SpringBoot 必须使用 JDK1.8 以上版本。

引入pom依赖

  1. 父模块: spring-boot-starter-parent
  2. 启动依赖: spring-boot-starter-web
  3. 测试依赖: spring-boot-starter-test
  4. 热部署依赖: spring-boot-devtools
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ysd.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 项目设置:编码格式UTF-8 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--单元测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring Boot SpringMVC框架依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<!--
optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用
devtools,需要重新引入 -->
<scope>true</scope><!-- 热部署 -->
</dependency>
</dependencies>
</project>

打包

1.打包需要引进必要的依赖,而且确认项目运行环境是jdk而非其他
2.打好的 jar 包在项目中的 target 文件夹下打好的jar
3.运行jar包
运行jar包前需要确认,电脑已经配置jdk1.8以上。才能运行。首先将项目中的 demo.jar 复制到某个文件夹下,然后在该文件夹处按 Shif键右击打开 DOC 命令提示框,输入 java -jar demo.jar 命令回车即可,出现下图的界面说明 jar 包运行成功。在这里插入图片描述

配置文件的优先级

经过测试, SpringBoot 项目中配置文件的读取顺序为:

  1. config/application.properties(项目根目录中 config 目录下)
  2. application.properties(项目根目录下)
  3. resources/config/application.properties(项目 resources 目录中 config 目录下)
  4. resources/application.properties(项目的 resources 目录下)

配置文件

主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev dev就是配置文件名中间的名称
项目会应用开发环境中的服务器端口号 8081
tomcat 服务器端口(默认)
server.port=8080
结构图

绑定属性值

定义一个 Student 类需要添加 @Component 注解,让 spring 在启动的时候扫描到该类,并添加到 spring 容器中。接下来为各个属性绑定值,通常有两种方法可以实现:一种是 @Value ,另一种 @ConfigurationProperties
@PropertySource 用于指定配置文件的位置,需要注意的是只能指定后缀为 properties 的配置文件,
不能指定 yaml 文件。

@value的使用

。。。。。自认为不适用,省略介绍

@ConfigurationProperties 的使用

@ConfigurationProperties 用于批量的为属性绑定值,可以用于任何数据类型,使用时需指定配置文件属性
的前缀。 首先需要导入依赖 spring-boot-configuration-processor

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

@ConfigurationProperties( prefix =“student”) prefix是指定前缀,比如student.name

@ConfigurationProperties 用于批量注入值,可以和@Value 一块使用,两者可以互补,
@ConfigurationProperties 的优先级高于@Value。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值