SpringBoot项目学习笔记

1.SpringBoot项目中的pom文件


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0



    org.springframework.boot
    spring-boot-starter-parent
    2.3.3.RELEASE
     

com.jt
springboot_demo01
0.0.1-SNAPSHOT
springboot_demo01
spring Assisgant 创建入门案例


    1.8


    

    
    
        org.springframework.boot
        
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    




    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

2.SpringBoot项目的启动原理

SpringBoot项目学习笔记_第1张图片

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 业务场景

需求:需要指定文件上传操作,需要指定文件上传的目录,如果将上传文件的目录信息写死在代码中,这样代码的耦合性高,不便于扩展。
SpringBoot项目学习笔记_第2张图片

3.3.2 利用@Value方式赋值

SpringBoot项目学习笔记_第3张图片

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配置文件

SpringBoot项目学习笔记_第4张图片

# 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 运行结果

SpringBoot项目学习笔记_第5张图片

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 修改字符集编码格式

SpringBoot项目学习笔记_第6张图片

3.4.4 结果

SpringBoot项目学习笔记_第7张图片

你可能感兴趣的:(springboot)