Spring Boot实现任意位置的properties及yml文件内容配置与获取
〇、参考资料
1、Spring Boot 中文乱码问题解决方案汇总
https://blog.51cto.com/u_15236724/5372824
2、spring boot读取自定义配置properties文件★
https://www.yisu.com/zixun/366877.html
3、spring boot通过配置工厂类,实现读取指定位置的yml文件★
https://blog.csdn.net/weixin_45168162/article/details/125427465
4、springBoot 读取yml 配置文件的三种方式(包含以及非component下)★
https://blog.csdn.net/weixin_44131922/article/details/126866040
5、SpringBoot集成Swagger的详细步骤
https://blog.csdn.net/m0_67788957/article/details/123670244
一、项目介绍
1、项目框架

2、技术栈
Spring Boot+Swagger+Lombok+Hutool
3、项目地址
https://gitee.com/ljhahu/kettle_processor.git
需要权限请联系:liujinhui-ahu@foxmail.com
二、properties配置与使用
1、默认配置文件
(1)配置-application.properties
# Spring Boot端口配置
server.port=9088
# spring数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=qaz123
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://192.168.40.111:3306/visualization?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.thymeleaf.prefix=classpath:/templates/
(2)读取
Spring Boot自己会读取,配置数据源、thymeleaf等信息
2、自定义配置文件
(1)配置-kettle.properties
# properties https://blog.csdn.net/weixin_42352733/article/details/121830775
environment=xuelei-www
kettle.repository.type=database
kettle.repository.username=admin
kettle.repository.password=admin
(2)使用-读取单个值-PropertiesController.java
package com.boulderaitech.controller;
import com.boulderaitech.entity.KettleRepositoryBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Api("properties测试")
@RestController //Controller和RestController的区别
@PropertySource("classpath:kettle.properties") //默认是application.properties
//可以将PropertySource注解加入entity,也可以加入controller将bean注入
public class PropertiesController {
@Value("${environment}")
private String envName;
@Autowired
private KettleRepositoryBean kettleRepositoryBean;
@RequestMapping("/getEnv")
@ApiOperation("properties方式获取当前的环境")
public String getEnv() {
return "hello " + envName;
}
@ApiOperation("properties方式获取当前的环境")
@RequestMapping("/getRepoInfo")
public String getRepoInfo() {
return "hello " + kettleRepositoryBean.toString();
}
}
(3)使用-读取多个值到对象-KettleRepositoryBean.java
package com.boulderaitech.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "kettle.repository")
public class KettleRepositoryBean {
private String type;
private String username;
private String password;
}
三、yml配置
1、默认配置文件
application.yml
2、自定义配置文件
(1)配置-repository.yml
kettle:
repository:
repo1:
type: postgresql
ip_addr: 192.168.4.68
port: 5432
username: admin
password: admin
db_name: kettle
version: 8.0.0
(2)实现任意位置读取的工厂类-YamlConfigFactory.java
package com.boulderaitech.factory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
/**
* 在任意位置读取指定的yml文件
* 参考:https://blog.csdn.net/weixin_45168162/article/details/125427465
*/
public class YamlConfigFactory extends DefaultPropertySourceFactory {
//继承父类,可以重载父类方法@Override
//实现接口,重写方法@Override
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
(3)使用-读取单个值-KettleEnvBean.java
package com.boulderaitech.entity;
import com.boulderaitech.factory.YamlConfigFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@Component
@AllArgsConstructor
@NoArgsConstructor
@PropertySource(value = "classpath:repository.yml",factory = YamlConfigFactory.class)//需要通过工厂加载指定的配置文件
public class KettleEnvBean {
@Value("${kettle.version}")
private String kettleVersion;
}
(4)使用-读取多个值到对象-KettleRepositoryYmlBean.java
package com.boulderaitech.entity;
import com.boulderaitech.factory.YamlConfigFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@PropertySource(value = "classpath:repository.yml",factory = YamlConfigFactory.class)//需要通过工厂加载指定的配置文件
@ConfigurationProperties(prefix = "kettle.repository.repo1") //需要添加spring boot的注解,才可以使用
//思考:能否通过反射操作修改注解的参数
@Component()
public class KettleRepositoryYmlBean {
private String type;
private String ip_addr; //命名只能用下划线
private String username;
private String password;
private String port;
private String db_name;
}
(5)使用-YmlController.java
package com.boulderaitech.controller;
import com.boulderaitech.entity.KettleEnvBean;
import com.boulderaitech.entity.KettleRepositoryYmlBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController //使用controller返回的结果会按照template进行解析
//使用RestController则会返回对应的字符串
public class YmlController {
@Autowired
private KettleRepositoryYmlBean kettleRepositoryYmlBean;
@Autowired
private KettleEnvBean kettleEnvBean;
@RequestMapping(value = "/getKettleVersion") //默认是get
public String getKettleVersion() {
return "hello " + kettleEnvBean.getKettleVersion();
}
// @GetMapping("/getRepoInfoYml"),不支持get方法
//@RequestMapping(value = "/getRepoInfoYml", method = RequestMethod.POST)
@RequestMapping(value = "/getRepoInfoYml")
public String getRepoInfoYml() {
return "hello " + kettleRepositoryYmlBean.toString();
}
}
Spring Boot实现任意位置的properties及yml文件内容配置与获取的更多相关文章
- Spring Boot笔记二:快速创建以及yml文件自动注入
上个笔记写了如何自己去创建Spring boot,以及如何去打jar包,其实,还是有些麻烦的,我们还自己新建了几个文件夹不是. Idea可以让我们快速的去创建Spring boot应用,来看 一.快速 ...
- Spring Boot项目application.yml文件数据库配置密码加密
在Spring boot开发中,需要在application.yml文件里配置数据库的连接信息,或者在启动时传入数据库密码,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了 ...
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...
- Spring Boot干货系列:(七)默认日志框架配置
Spring Boot干货系列:(七)默认日志框架配置 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候, ...
- Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置
0 引言 本文主要在Spring Boot 基础项目的基础上,添加 Mysql .MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作. 1 创建数据表 这个过程就暂时省略了. 2 ...
- Spring Boot教程(三十八)使用MyBatis注解配置详解(1)
之前在Spring Boot中整合MyBatis时,采用了注解的配置方式,相信很多人还是比较喜欢这种优雅的方式的,也收到不少读者朋友的反馈和问题,主要集中于针对各种场景下注解如何使用,下面就对几种常见 ...
- 【spring boot】使用@Value映射properties文件属性
描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...
- 在spring boot中使用自定义的properties
1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- Spring Boot属性配置文件:application.properties 详解
学习资料 网址 官方说明文档 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-pro ...
随机推荐
- SEO知识点
SEO中的长尾理论 长尾关键词就是包含关键信息,但是搜索量比较少的句子或词组. 每一个长尾关键词都可能会为网站带来流量.一般一个较大的网站,流量的主要来源可能都由长尾关键词构成,因为网站除了目标关键词 ...
- Winsw将jar包部署为windows服务
1. 下载Winsw https://github.com/winsw/winsw/releases 下载winsw官网上的xml文件和.exe文件 2. 编辑配置文件 创建一个文件夹demo,将所需 ...
- [题解] HDU 5115 Dire Wolf 区间DP
考虑先枚举所有的物品中最后拿走的,这样就分成了2个子问题,即先拿完左边的,再拿完右边的,最后拿选出的那个.令dp(i,j)表示拿完[i,j]所有物品的最小代价.你可能会说,我们拿[i,j]这一段物品的 ...
- css事件穿透
代码: pointer-events: none; 解义:你可以看的到第一个元素,但是你看不到下面的元素着,点击第一个元素会穿透触发到第二个元素的元素
- 2022.9.10-2022.9.12 Java第一次课总结
本节课中的问题总结如下: 1.Java的基本运行单位是类还是方法? 答:Java的基本运行单位是类. 2.类由什么组成? 答:类由变量/方法/属性/事件等部分组成,其中方法就是我们所熟悉的函数,属性即 ...
- .NET 7 RC 2 发布,倒计时一个月发布正式版
微软2022-10-22 发布了 .NET 7 RC 2,下一站是.NET 7正式发布,就在下个月Net Conf 2022(11月8日)期间正式发布. 经过长达一年时间的开发,.NET 7 规划的所 ...
- Pytest进阶使用
fixture 特点: 命令灵活:对于setup,teardown可以省略 数据共享:在conftest.py配置里写方法可以实现数据共享,不需要import导入,可以跨文件共享 scope的层次及神 ...
- PCA降维的原理及实现
PCA可以将数据从原来的向量空间映射到新的空间中.由于每次选择的都是方差最大的方向,所以往往经过前几个维度的划分后,之后的数据排列都非常紧密了, 我们可以舍弃这些维度从而实现降维 原理 内积 两个向量 ...
- <一>从指令角度了解函数堆栈调用过程
代码 点击查看代码 #include <iostream> using namespace std; int sum(int a,int b){ int temp=0; temp= a + ...
- JAVA语言学习-面向对象(1)
类与对象 类 类是JAVA语言中重要的复合型数据类型().类的实现包括两个部分:成员变量和成员方法("方法"可以看作是C语言中的函数) 类的声明 要使用类,首先得对其声明.声明一个 ...