嗯我遇到的暫時就這么多情況,自定義properties文件名,靜態工具類中使用,和自定義properties文件位置
1. self-config.propreties
這里是配置文件
config.taskUrl=http://localhost:1314/task/
config.filePath=C:/dev/terminal/files/
2. SelfConfig.propreties
接收配置文件,另外網傳的獲取配置文件工具類一般不建議使用,如果在配置文件中修改屬性名很難在使用的地方進行同步修改
package com.self.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author Xiaoyan
* @date 2019/10/10 17:14
*/
@Component
@ConfigurationProperties(prefix = "config")
// 自定義位置&文件名
@PropertySource("classpath:/self-config.properties")
@Data
public class SelfConfig {
private String taskUrl;
private String filePath;
}
3. 正常使用:
不用詳細介紹了吧
@Autowired
private SelfConfig config;
4. 工具類中使用:
package com.self.util;
import com.self.config.SelfSimpleFieldConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Xiaoyan
* @date 2019/9/23 18:47
*/
@Component
public class FileUtil {
private static SelfSimpleFieldConfig config;
@Autowired
public void setConfig(SelfSimpleFieldConfig config) {
FileUtil.config = config;
}
public static void printConfig() {
System.out.println("config.toString() = " + config.toString());
}
}