扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Spring Boot读取配置文件
1)通过注入ApplicationContext 或者 Environment对象来读取配置文件里的配置信息。
package com.ivan.config.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Autowired ApplicationContext context; @Autowired Environment environment; @RequestMapping(value="/config", method={RequestMethod.GET}) public String getConfigContent(){ String name = context.getEnvironment().getProperty("db.user.name"); return name; } @RequestMapping(value="/configEnv", method={RequestMethod.GET}) public String getConfigEnvironment(){ String name = environment.getProperty("db.user.name"); return name; } }
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流