〇、參考資料
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
需要權限請聯系:[email protected]
二、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 = "https://www.cnblogs.com/liujinhui/archive/2022/12/01/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 = "https://www.cnblogs.com/liujinhui/archive/2022/12/01/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 = "https://www.cnblogs.com/getKettleVersion") //默認是get
public String getKettleVersion() {
return "hello " + kettleEnvBean.getKettleVersion();
}
// @GetMapping("/getRepoInfoYml"),不支持get方法
//@RequestMapping(value = "https://www.cnblogs.com/getRepoInfoYml", method = RequestMethod.POST)
@RequestMapping(value = "https://www.cnblogs.com/getRepoInfoYml")
public String getRepoInfoYml() {
return "hello " + kettleRepositoryYmlBean.toString();
}
}
本文來自博客園,作者:哥們要飛,轉載請注明原文鏈接:https://www.cnblogs.com/liujinhui/p/16940265.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/538824.html
標籤:其他
