SpringBoot_关于pom文件,配置文件,运行原理的说明

1.SpringBoot项目中的pom文件

<?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>
<!--parent标签作用:定义了SpringBoot中所有关联项目的版本号-->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.3.3.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jt</groupId>
<artifactId>springboot_demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo01</name>
<description>spring Assisgant 创建入门案例</description>

<properties>
	<java.version>1.8</java.version>
</properties>

	<!--开箱即用:SpringBoot项目中只需要引入少量的jar包文件和配置即可拥有其功能
		Spring-boot-starter 拥有开箱即用的功能
					-jdbc
					-aop
		maven项目中依赖具有传递性
		A 依赖 B 依赖 C 导入A BC会自动依赖
	-->
<dependencies>
	<!--直接依赖了Web SpringMvc配置-->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<!--SpringBoot-star SpringBoot的启动项-->
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!--SpringBoot 重构了测试方法,可以在测试类中,直接引入测试案例-->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>org.junit.vintage</groupId>
				<artifactId>junit-vintage-engine</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
</dependencies>

<!--在项目打包部署的时候生效,如果不添加build标签,那么程序在发布的时候会报
	没有main方法的错误
-->
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

2.SpringBoot项目的启动原理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tiPfbt7u-1598918001654)(/img/bVbL8Ry)]

3.SpringBoot配置文件的说明

3.1 properties文件

1.Properties文件本身是Key-value结构的,身都是字符串定义

server.port=80

2.SpringBoot程序在读取Pro文件默认使用iso-8859-1格式,如果需要读取的话需要设定为UTF-8格式

3.2 yml文件
# yml 1.Key-value结构
#     2.Key-value之间使用“:”加空格的方法连接
#     3.YML配置文件有缩进的效果
#     4.YML配置文件默认采用UTF-8编码

server:
  serverlet:
    context-path: / #设定项目的发布路径
  port: 80

3.3 配置文件为属性赋值的方式
3.3.1 业务场景

需求:需要指定文件上传操作,需要指定文件上传的目录,如果将上传文件的目录信息写死在代码中,这样代码的耦合性高,不便于扩展。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7pUB4Kj1-1598918001659)(/img/bVbL81y)]

3.3.2 利用@Value方式赋值

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irGtFanF-1598918001662)(/img/bVbL88q)]

package com.jt.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileController {
    //问题:如何配置文件作为动态获取??
    @Value("${image.localDir}") //spel表达式,Spring框架所提供的
    private String localDir;  //= "F:\\JT-SOFT\\images";//如果放在这里代码耦合性较高

    @RequestMapping("/getPath")
    public String getPath(){
        System.out.println("指定图片的地址:"+localDir);
        return "图片的地址位:"+localDir;

    }

}

3.3.2.1 编辑YML配置文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fTHFMgf3-1598918001666)(/img/bVbL88H)]


```

```
# yml 1.Key-value结构
#     2.Key-value之间使用“:”加空格的方法连接
#     3.YML配置文件有缩进的效果
#     4.YML配置文件默认采用UTF-8编码

server:
  serverlet:
    context-path: / #设定项目的发布路径
  port: 80

# 配置图片上传的路径
image:
  localDir: F:/FOREVERUPWARD/JAVA/JAVA_CGBTN200529_frame03/PICTURE
```

```

3.3.2.2 运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tR5AQhtE-1598918001669)(/img/bVbL9kr)]

3.4 指定配置文件为属性赋值
3.4.1 业务场景

需求:YMl配置文件是SpringBoot整合第三方配置文件,如果将业务配置与YML配置写到一起,则不方便管理,能否在指定的配置文件(pro)中实现属性的赋值

3.4.2 编辑Pro文件
#  Key-value
image.localDir=F:/FOREVERUPWARD/JAVA/JAVA_CGBTN200529_frame03/PICTURE 图片地址

package com.jt.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//导入配置文件,之后由Spring容器扫描
@PropertySource(value = "classpath:/properties/image.properties",
encoding = "UTF-8")
public class FileController {
    //问题:如何配置文件作为动态获取??
    @Value("${image.localDir}") //spel表达式,Spring框架所提供的
    private String localDir;  //= "F:\\JT-SOFT\\images";//如果放在这里代码耦合性较高

    @RequestMapping("/getPath")
    public String getPath(){
        System.out.println("指定图片的地址:"+localDir);
        return "图片的地址位:"+localDir;

    }

}

3.4.3 修改字符集编码格式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-whOMWdA3-1598918001672)(/img/bVbL9iB)]

3.4.4 结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ABMrp698-1598918001674)(/img/bVbL9jL)]



猜你喜欢

转载自blog.csdn.net/weixin_43388956/article/details/108333321