常用的读取配置文件参数的方法有:
1.使用 @Value 注解读取配置
2.使用 @ConfigurationProperties 注解读取配置
3.使用 Environment 对象读取配置
4.使用 PropertiesLoaderUtils 工具读取配置
代码实例:
application.yml
user:
id: 1
name: zhangsan
age: 18
Demo.java
package com.sai.demo.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class Demo {
@Value("${user.id}")
private String id;
@Value("${user.name}")
private String name;
@Value("${user.age}")
private String age;
}
Demo2.java
package com.sai.demo.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class Demo2 {
private String id;
private String name;
private String age;
}
DemoController
package com.sai.demo.controller;
import com.sai.demo.entity.Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Properties;
/**
* @param
* @param
* @param
* @author lfc
* @return
* @date 2023/4/26
*/
@Slf4j
@RequestMapping("/user")
@RestController
public class DemoController {
@Autowired
private Demo demo;
@Autowired
private Demo demo2;
@Autowired
private Environment environment;
/**
* @Value形式
* @return
*/
@GetMapping("/getUser")
public String getUser(){
System.out.println(demo.toString());
return demo.toString();
}
/**
* @ConfigurationProperties形式
* @return
*/
@GetMapping("/getUser2")
public String getUser2(){
System.out.println(demo2.toString());
return demo2.toString();
}
/**
* 使用 Environment 读取配置形式
* @return
*/
@GetMapping("/getUser3")
public String getUser3(){
String id = environment.getProperty("user.id");
String name = environment.getProperty("user.name");
String age = environment.getProperty("user.age");
System.out.println("Demo: (id: " + id + ",name: " + name + ",age: " + age + ")");
return "Demo: (id: " + id + ",name: " + name + ",age: " + age + ")";
}
/**
* 使用 PropertiesLoaderUtils 读取配置形式
* @return
*/
@GetMapping("/getUser4")
public String getUser4(){
String id = null;
String name = null;
String age = null;
try {
ClassPathResource resource = new ClassPathResource("application.yml");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
id = properties.getProperty("id");
name = properties.getProperty("name");
age = properties.getProperty("age", "100");
} catch (IOException e) {
log.error("", e);
}
System.out.println("Demo: (id: " + id + ",name: " + name + ",age: " + age + ")");
return "Demo: (id: " + id + ",name: " + name + ",age: " + age + ")";
}
}