Spring 配置

配置文件最主要的目的 : 解决硬编码的问题(代码写死)

SpringBoot 的配置文件,有三种格式

1.properties 

2.yaml

3.yml(是 yaml 的简写)

SpringBoot 只支持三个文件 

1.application.properties

2.application.yaml

3.application.yml

yaml 和 yml 是一样的,学会一个就行

如果一个项目中同时存在 properties 和 yml ,虽然两个都会生效,但是 properties 的优先级更高

但是正常情况下都只有一个文件,多了容易乱

properties 的代码格式一般为键值对的样式,以 = 分割,单词小写,单词之间用 . 分割

下面是一些配置举例,配置端口号和配置数据库先不细说

我们自定义的配置该如何拿到运用呢?

我们创建一个类

package com.example.ioc.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 PropertiesController {//读取配置文件@Value("${demo.key1}")private String key1;@RequestMapping("/readkey")public String readkey(){return "读取到的配置项key1"+key1;}
}

就能成功拿到了

名字一一对应

什么样的内容适合放在配置文件中呢?

那些可能会发生改变的信息,与我的程序运行没有太大关系的,我们就把它放在配置文件中

但是其实我们发现 properties 有很多冗余的信息 比如

想要解决这个问题,就可以使用 yml 配置文件的格式化了

我们先在 resources 底下创建一个文件,application.yml

yml 文件对比 properties 文件格式,yml 文件把 . 换成冒号+换行,key后面用冒号赋值

这样就可以把端口改为 9090 了

但是我们稍作修改,把9090 前面的空格删掉,再次运行程序,发现修改端口失败了

yml 的格式有严格要求,我们要在值前面的冒号的后面加空格,空格不可省略

我们对数据库相关配置进行修改的样式如下

yml 的自定义配置该如何写并且使用呢?

然后创建一个类

package com.example.ioc.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 YmlController
{@Value("${demo.key1}")public String key1;@RequestMapping ("/readYml")public String readYml(){return key1;}
}

这样就能成功使用了

我们再看看多个数据

 @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法

package com.example.ioc.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;@RestController
public class YmlController
{@Value("${demo.key1}")public String key1;@Value("${demo.key2}")public String key2;@Value("${demo.key3}")public String key3;@RequestMapping ("/readYml")public String readYml(){return key1;}@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("key1:"+key1);System.out.println("key2:"+key2);System.out.println("key3:"+key3);}
}

我们再看看单双引号的区别

双引号里面的 \n 是换行

单引号会对特殊字符进行转义,因为\n 本身表示的意思是换行,但是使用单引号的时候,内容变成了 \n 而不是换行,所以认为是转义

yml 该如何配置对象?

配置文件为

再创建一个student类

package com.example.ioc;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "student")
@Data
public class Student {private Integer id;private String name;private Integer age;
}

然后就能运行了 

package com.example.ioc.controller;import com.example.ioc.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic Student student;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("student:"+student);}
}

yml 如何配置集合呢?

package com.example.ioc.model;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBTypes {private List<String> name;
}

 

package com.example.ioc.controller;import com.example.ioc.model.DBTypes;
import com.example.ioc.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic DBTypes dbTypes;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("dbTypes:"+dbTypes);}
}

用数组去接收也是可以滴

记得要加空格哦

yml 也可以配置map


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.List;@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBTypes {private List<String> name;private HashMap<String,String> map;
}
package com.example.ioc.controller;import com.example.ioc.model.DBTypes;
import com.example.ioc.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic DBTypes dbTypes;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("dbTypes:"+dbTypes);}
}

yml 的优缺点 :

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/195790.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

代码随想录算法训练营第二十八天| 78 子集 90 子集|| 93 复原IP地址

78 子集 由题意可知数组中的元素互不相同&#xff0c;所以在dfs中我们可以将当前的path直接加入到res中。 class Solution {List<List<Integer>>res new ArrayList<>();List<Integer>path new LinkedList<>();public List<List<Integer…

高精度算法【Java】(待更新中~)

高进度加法 在Java中可以使用BigInteger进行高精度计算&#xff0c;除此也可以仿照竖式相加的计算原理进行计算。 BigInteger 提供所有 Java 的基本整数操作符的对应物&#xff0c;并提供 java.lang.Math 的所有相关方法。另外&#xff0c;BigInteger 还提供以下运算&#xff1…

CentOS 7 安装CMake指定版本3.21.2

背景&#xff1a;今天在CentOS 7 电脑上安装C 日志框架SpdLog-1.12.0&#xff0c;提示如下错误信息&#xff1a; [rootlocalhost build]# cmake .. && make -j CMake Error at CMakeLists.txt:3 (cmake_minimum_required):CMake 3.10...3.21 or higher is required. …

5 redis的GEO操作

一、GEO Redis 3.2版本提供了GEO(地理信息定位)功能&#xff0c;支持存储地理位置信息用来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能。 有效纬度从-85.05112878度到85.05112878度 注意&#xff1a;当坐标位置超出上述指定范围时&#xff0c;将会返回一个错误。 …

函数式编程框架 functionaljava 简介

文章目录 一、函数式编程起源二、functionaljava 框架简介 一、函数式编程起源 ​ 函数式编程起源于数理逻辑&#xff08;范畴论&#xff0c;Category Theory&#xff09;&#xff0c;起源于λ演算&#xff0c;这是一种演算法&#xff0c;它定义一些基础的数据结构&#xff0c…

【Java】网络编程基础—InetAddress类和URL编程

&#x1f33a;个人主页&#xff1a;Dawn黎明开始 &#x1f380;系列专栏&#xff1a;Java ⭐每日一句&#xff1a;为了那个远方&#xff0c;你要奋不顾身 &#x1f4e2;欢迎大家&#xff1a;关注&#x1f50d;点赞&#x1f44d;评论&#x1f4dd;收藏⭐️ 文章目录 一.&#x…

Python编程技巧 – 使用字符串(Strings)

Python编程技巧 – 使用字符串&#xff08;Strings) Python Programming Essentials – Using Strings 本文简要介绍如何使用字符串&#xff0c;来进行Python编程。字符串有很多用途&#xff0c;包括输出结果、反馈状态、数据处理以及切片和文本筛选等 1. 字符串 字符串(St…

Java21新增特性

版本介绍 Java 21是Java平台的一个新版本&#xff0c;于2023年9月19日由Oracle公司正式发布。这个版本包含了数千个性能、稳定性和安全性更新&#xff0c;以及几十个新功能和增强。其中&#xff0c;15个增强被赋予了自己的JDK增强提案&#xff08;JEP&#xff09;&#xff0c;…

基于SSM的北海旅游网站设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

CICD 持续集成与持续交付——jenkins

部署 软件下载&#xff1a;https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/ [rootcicd2 ~]# rpm -ivh jdk-11.0.15_linux-x64_bin.rpm[rootcicd2 ~]# yum install -y fontconfig[rootcicd2 ~]# rpm -ivh jenkins-2.432-1.1.noarch.rpm启动服务 [rootcicd2 ~]# systemctl…

python线程和进程

文章目录 版权声明多任务并发概念并行概念 进程多线程的作用进程的创建步骤通过进程类创建进程对象进程执行带有参数的任务获取进程杀死进程进程间不共享全局变量主进程和子进程的结束顺序设置守护进程销毁子进程 线程线程的创建步骤通过线程类创建线程对象线程执行带有参数的任…

T10 数据增强

文章目录 一、准备环境和数据1.环境2. 数据 二、数据增强&#xff08;增加数据集中样本的多样性&#xff09;三、将增强后的数据添加到模型中四、开始训练五、自定义增强函数六、一些增强函数 &#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f…