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

打印 上一主题 下一主题

主题 1058|帖子 1058|积分 3174

〇、参考资料

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
  1. # Spring Boot端口配置
  2. server.port=9088
  3. # spring数据源配置
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. spring.datasource.password=qaz123
  6. spring.datasource.username=root
  7. spring.datasource.url=jdbc:mysql://192.168.40.111:3306/visualization?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
  8. spring.thymeleaf.prefix=classpath:/templates/
复制代码
(2)读取

Spring Boot自己会读取,配置数据源、thymeleaf等信息
 
2、自定义配置文件

(1)配置-kettle.properties
  1. # properties  https://blog.csdn.net/weixin_42352733/article/details/121830775
  2. environment=xuelei-www
  3. kettle.repository.type=database
  4. kettle.repository.username=admin
  5. kettle.repository.password=admin
复制代码
(2)使用-读取单个值-PropertiesController.java
  1. package com.boulderaitech.controller;
  2. import com.boulderaitech.entity.KettleRepositoryBean;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.context.annotation.PropertySource;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @Api("properties测试")
  12. @RestController //Controller和RestController的区别
  13. @PropertySource("classpath:kettle.properties") //默认是application.properties
  14. //可以将PropertySource注解加入entity,也可以加入controller将bean注入
  15. public class PropertiesController {
  16.     @Value("${environment}")
  17.     private String envName;
  18.     @Autowired
  19.     private KettleRepositoryBean kettleRepositoryBean;
  20.     @RequestMapping("/getEnv")
  21.     @ApiOperation("properties方式获取当前的环境")
  22.     public String getEnv() {
  23.         return "hello " + envName;
  24.     }
  25.     @ApiOperation("properties方式获取当前的环境")
  26.     @RequestMapping("/getRepoInfo")
  27.     public String getRepoInfo() {
  28.         return "hello " + kettleRepositoryBean.toString();
  29.     }
  30. }
复制代码
(3)使用-读取多个值到对象-KettleRepositoryBean.java
  1. package com.boulderaitech.entity;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.stereotype.Component;
  7. @Data
  8. @Component
  9. @NoArgsConstructor
  10. @AllArgsConstructor
  11. @ConfigurationProperties(prefix = "kettle.repository")
  12. public class KettleRepositoryBean {
  13.     private String type;
  14.     private String username;
  15.     private String password;
  16. }
复制代码
三、yml配置

1、默认配置文件

application.yml
2、自定义配置文件

(1)配置-repository.yml
  1. kettle:
  2.   repository:
  3.     repo1:
  4.       type: postgresql
  5.       ip_addr: 192.168.4.68
  6.       port: 5432
  7.       username: admin
  8.       password: admin
  9.       db_name: kettle
  10.   version: 8.0.0
复制代码
(2)实现任意位置读取的工厂类-YamlConfigFactory.java
  1. package com.boulderaitech.factory;
  2. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  3. import org.springframework.core.env.PropertiesPropertySource;
  4. import org.springframework.core.env.PropertySource;
  5. import org.springframework.core.io.support.DefaultPropertySourceFactory;
  6. import org.springframework.core.io.support.EncodedResource;
  7. import java.io.IOException;
  8. import java.util.Properties;
  9. /**
  10. * 在任意位置读取指定的yml文件
  11. * 参考:https://blog.csdn.net/weixin_45168162/article/details/125427465
  12. */
  13. public class YamlConfigFactory extends DefaultPropertySourceFactory {
  14.     //继承父类,可以重载父类方法@Override
  15.     //实现接口,重写方法@Override
  16.     @Override
  17.     public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
  18.         String sourceName = name != null ? name : resource.getResource().getFilename();
  19.         if (!resource.getResource().exists()) {
  20.             return new PropertiesPropertySource(sourceName, new Properties());
  21.         } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
  22.             Properties propertiesFromYaml = loadYml(resource);
  23.             return new PropertiesPropertySource(sourceName, propertiesFromYaml);
  24.         } else {
  25.             return super.createPropertySource(name, resource);
  26.         }
  27.     }
  28.     private Properties loadYml(EncodedResource resource) throws IOException {
  29.         YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  30.         factory.setResources(resource.getResource());
  31.         factory.afterPropertiesSet();
  32.         return factory.getObject();
  33.     }
  34. }
复制代码
(3)使用-读取单个值-KettleEnvBean.java
  1. package com.boulderaitech.entity;
  2. import com.boulderaitech.factory.YamlConfigFactory;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.PropertySource;
  8. import org.springframework.stereotype.Component;
  9. @Data
  10. @Component
  11. @AllArgsConstructor
  12. @NoArgsConstructor
  13. @PropertySource(value = "classpath:repository.yml",factory = YamlConfigFactory.class)//需要通过工厂加载指定的配置文件
  14. public class KettleEnvBean {
  15.     @Value("${kettle.version}")
  16.     private String kettleVersion;
  17. }
复制代码
(4)使用-读取多个值到对象-KettleRepositoryYmlBean.java
  1. package com.boulderaitech.entity;
  2. import com.boulderaitech.factory.YamlConfigFactory;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.context.annotation.PropertySource;
  8. import org.springframework.stereotype.Component;
  9. @Data
  10. @AllArgsConstructor
  11. @NoArgsConstructor
  12. @PropertySource(value = "classpath:repository.yml",factory = YamlConfigFactory.class)//需要通过工厂加载指定的配置文件
  13. @ConfigurationProperties(prefix = "kettle.repository.repo1") //需要添加spring boot的注解,才可以使用
  14. //思考:能否通过反射操作修改注解的参数
  15. @Component()
  16. public class KettleRepositoryYmlBean {
  17.     private String type;
  18.     private String ip_addr; //命名只能用下划线
  19.     private String username;
  20.     private String password;
  21.     private String port;
  22.     private String db_name;
  23. }
复制代码
(5)使用-YmlController.java
  1. package com.boulderaitech.controller;
  2. import com.boulderaitech.entity.KettleEnvBean;
  3. import com.boulderaitech.entity.KettleRepositoryYmlBean;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.PropertySource;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. @RestController //使用controller返回的结果会按照template进行解析
  13. //使用RestController则会返回对应的字符串
  14. public class YmlController {
  15.     @Autowired
  16.     private KettleRepositoryYmlBean kettleRepositoryYmlBean;
  17.     @Autowired
  18.     private KettleEnvBean kettleEnvBean;
  19.     @RequestMapping(value = "/getKettleVersion") //默认是get
  20.     public String getKettleVersion() {
  21.         return "hello " + kettleEnvBean.getKettleVersion();
  22.     }
  23.     // @GetMapping("/getRepoInfoYml"),不支持get方法
  24.     //@RequestMapping(value = "/getRepoInfoYml", method = RequestMethod.POST)
  25.     @RequestMapping(value = "/getRepoInfoYml")
  26.     public String getRepoInfoYml() {
  27.         return "hello " + kettleRepositoryYmlBean.toString();
  28.     }
  29. }
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表