1.Spring读取resources下的properties文件
项目中遇到将一些固定数据放在properties中怎么办?可以采用property、Resource、ResourceBundle等多种方式。
config.properties
key=marlon
value=18
PropertiesUtil.java
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* @author marlon
* @create 2023-11-03 20:37
* @description: 解析properties
*/
public class PropertiesUtil {
public static void main(String[] args) throws IOException {
PropertiesUtil util = new PropertiesUtil();
util.testResource();
util.testClassLoader();
util.testSystemClassLoader();
util.testNewFile();
util.testBundle();
}
public void testResource() throws IOException {
Resource resource = new ClassPathResource("config.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
System.out.println("===================ClassPathResource===========================");
System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
}
public void testClassLoader() throws IOException {
Properties properties = new Properties();
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties");
properties.load(inputStream);
System.out.println("===================ClassLoader.getSystemResourceAsStream===========================");
System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
}
public void testSystemClassLoader() throws IOException {
Properties properties = new Properties();
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
properties.load(inputStream);
System.out.println("===================ClassLoader.getSystemClassLoader===========================");
System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
}
public void testNewFile() throws IOException {
Properties properties = new Properties();
FileInputStream inputStream = new FileInputStream(new File("src/main/resources/config.properties"));
properties.load(inputStream);
System.out.println("===================FileInputStream===========================");
System.out.println(properties.getProperty("key")+"==="+properties.getProperty("value"));
}
public void testBundle() {
ResourceBundle rb = ResourceBundle.getBundle("info");
System.out.println("===================ResourceBundle===========================");
for(String key : rb.keySet()){
String value = rb.getString(key);
System.out.println(key + ":" + value);
}
}
}
总结:new FileInputStream的方式是项目的根目录,需要加上src/main/resources路径;其他方式都是项目的resources资源下,ResourceBundle更特殊,可以不用写文件后缀。
2.springboot绑定properties
2.1 创建properties类
创建一个ClientProperties类,并添加注解@Configuration、@ConfigurationProperties(prefix = "aft.client")
通过prefix设置前缀,前缀下的属性名要和ClientProperties类中的属性名保持一致,并且必须有set方法,也可以用lombok的@Data注解代替
@Configuration
@ConfigurationProperties(prefix = "aft.client")
@Data
public class ClientProperties {
private String serviceAddr;
private String managerAddr;
private String id;
private String encrypt;
}
2.2 属性注入
在需要使用的地方使用@Autoware
注解进行属性注入
@Autowired
private ClientProperties clientProperties;
2.3 编写yaml或者properties配置文件
application.yaml
如下:
aft:
client:
enabled: true
serviceAddr: 111
managerAddr: 222
id: 333
encrypt: 444
application.properties
如下:
aft.client.enabled = true
aft.client.serviceAddr = 111
aft.client.managerAddr = 222
aft.client.id = 333
aft.client.encrypt = 444
2.4 启动类中开启config注解
@EnableConfigurationProperties