目录
一、需求背景
在我们做项目过程中,很多配置数据不能直接写在代码中,需要通过配置文件properties来进行配置,利于代码的维护。
当然,也可以通过数据库获取。
下面,针对此问题来介绍两种我自己常用的解决方法,供大家参考。
二、解决方案
1. 配置信息
目录:resources/config/
创建配置文件:userConfig.properties
user.name=test
user.password=123456
user.nickname=ceshi
2. 方法1
创建配置实体类:UserConfigBean.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @ClassName: UserConfigBean
* @Description:
* @Auther: Thomas
* @Version: 1.0
* @create 2021/6/6 16:51
*/
@Data
@Component
@ConfigurationProperties(prefix = "user")
@PropertySource("classpath:config/userConfig.properties")
public class UserConfigBean {
/**
* 用户名
*/
private String name;
/**
* 密码
*/
private String password;
/**
* 昵称
*/
private String nickname;
}
创建测试文件:TestController.java
import com.gwx.mail.utils.UserConfigBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Properties;
/**
* @ClassName: TestAction
* @Description:
* @Auther: Thomas
* @Version: 1.0
* @create 2021/5/1 22:34
*/
@Slf4j
@RestController
public class TestController {
/**
* 注入属性
*/
@Value("${user.name}")
private String name;
/**
* 密码
*/
@Value("${user.password}")
private String password;
/**
* 昵称
*/
@Value("${user.nickname}")
private String nickname;
/**
* 直接注入对象
*/
@Autowired
private UserConfigBean userConfig;
@RequestMapping("/test")
public String test() {
log.info("用户名为:" + name);
log.info("密码为:" + password);
log.info("昵称为:" + nickname);
log.info("用户对象为:" + userConfig);
return "解析properties文件属性";
}
}
3. 方法2
创建方法类:PropertiesUtil.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @ClassName: PropertiesUtil
* @Description:
* @Auther: Thomas
* @Version: 1.0
* @create 2021/5/30 14:55
*/
public class PropertiesUtil {
public static Properties parsePro(String filePath) {
Properties prop = new Properties();
InputStream in = PropertiesUtil.class.getResourceAsStream(filePath);
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
}
/**
* @ClassName: TestAction
* @Description:
* @Auther: Thomas
* @Version: 1.0
* @create 2021/5/1 22:34
*/
@Slf4j
@RestController
public class TestController {
@RequestMapping("/test2")
public String test2() {
String filePath = "/config/userConfig.properties";
Properties pro = PropertiesUtil.parsePro(filePath);
log.info("用户名为:" + pro.getProperty("user.name"));
log.info("密码为:" + pro.getProperty("user.password"));
log.info("昵称为:" + pro.getProperty("user.nickname"));
log.info("用户对象为:" + pro);
return "解析properties文件属性";
}
}
三、测试
调用接口:
查看日志信息:
2021-06-06 17:05:27.440 INFO 10144 --- [nio-9000-exec-1] com.gwx.mail.controller.TestController : 用户名为:test
2021-06-06 17:05:27.440 INFO 10144 --- [nio-9000-exec-1] com.gwx.mail.controller.TestController : 密码为:123456
2021-06-06 17:05:27.440 INFO 10144 --- [nio-9000-exec-1] com.gwx.mail.controller.TestController : 昵称为:ceshi
2021-06-06 17:05:27.440 INFO 10144 --- [nio-9000-exec-1] com.gwx.mail.controller.TestController : 用户对象为:{user.password=123456, user.name=test, user.nickname=ceshi}