首页 > 其他分享 >Spring Boot实现任意位置的properties及yml文件内容配置与获取

Spring Boot实现任意位置的properties及yml文件内容配置与获取

时间:2022-12-05 14:08:58浏览次数:63  
标签:String Spring Boot springframework private import org com 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文件★

4、springBoot 读取yml 配置文件的三种方式(包含以及非component下)★

5、SpringBoot集成Swagger的详细步骤

一、项目介绍

1、项目框架

 

Spring Boot实现任意位置的properties及yml文件内容配置与获取_java

2、技术栈

 Spring Boot+Swagger+Lombok+Hutool

3、项目地址

​ https://gitee.com/ljhahu/kettle_processor.git​

二、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  
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文件
*/
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();
}
}

 

作者:​​哥们要飞



标签:String,Spring,Boot,springframework,private,import,org,com,yml
From: https://blog.51cto.com/liujinhui/5911980

相关文章

  • Spring Boot整合log4j实战(一):排除自带依赖、日志重定向、测试类验证
    〇、参考资料1、springboot整合log4j全过程详解2、SpringBoot全局排除spring-boot-starter-logging依赖一、项目概述1、项目框架2、技术栈SpringBoot+log4j+SpringBoot......
  • Spring中@Async注解使用及配置
    Spring中@Async注解使用及配置参考文章:https://blog.csdn.net/weixin_42272869/article/details/123082657一、@Async注解的使用在使用spring框架中,可以非常简单方便的......
  • extjs4,spring mvc3上传文件
    本文讲解下extjs4结合springmvc3的注解完成上传文件的例子。1页面文件  <!--ExtJSFiles--><linkrel="stylesheet"type="text/css......
  • Spring Boot注入静态变量
    SpringBoot注入静态变量@value或者@Autowired不能直接注入值给静态属性,spring不允许/不支持把值注入到静态变量中;spring支持set方法注入,我们可以利用非静态setter方法......
  • Spring
    组成SpringCore:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建、装配,销毁等功能。SpringContext:实现了ApplicationContext接口,Spring的上下文,拓展了核心容......
  • 源码解析:Dubbo3 的 Spring 适配原理与初始化流程
    Dubbo国内影响力最大的开源框架之一,非常适合构建大规模微服务集群的,提供开发框架、高性能通信、丰富服务治理等能力。同时Dubbo无缝支持Spring、SpringBoot模式的开......
  • Spring Cloud Vault Config
    9.自定义要公开为属性源的机密后端SpringCloudVault使用基于属性的配置来创建键值和发现的秘密后端。​​PropertySource​​发现的后端提供bean来描述使用机密后端的......
  • Spring中获取request的几种方法,及其线程安全性分析
    前言本文将介绍在SpringMVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性。目录概述如何测试线程安全性方法1:Controller中加参数方法2:自动注入方法3......
  • 如何使用 Spring Cloud Zookeeper 进行服务发现和分布式配置
    该项目通过以下方式为SpringBoot应用程序提供Zookeeper集成自动配置并绑定到Spring环境和其他Spring编程模型习语。通过一些注释,您可以快速启用和配置常见模式......
  • 扒一扒使用boostrap-fileinput上传插件遇到的坑,Bootstrap-fileinput上传插件的使用详
    扒一扒使用boostrap-fileinput上传插件遇到的坑,Bootstrap-fileinput上传插件的使用详解,「建议收藏」发布于2022-06-2611:56:21阅读 3970 由于公司项目的需求,......