【SpingBoot学习笔记】SpingBoot之读取resource/config目录下自定义properties/yml文件(注解方式)
之前已经写了一篇读写properties文件的文章,见《Java读取properties配置文件写法》,但如果是springboot项目,配置统一在resource/config目录下,使用注解如何读取呢,写法如下
打开IDEA,新建maven项目readproperties
项目结构
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>readproperties</artifactId> <version>1.0-SNAPSHOT</version> <!--手动引入--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <!--手动引入--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--手动引入--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> </project>
新建springboot启动类
package test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class,args); } }
新建my.properties,放在resource/config目录
#MY CONFIG
#########################################
current.dbType = Oracle
创建配置类
package test.init; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/my.properties") public class MyConfig { //获取配置中的信息 @Value("${current.dbType}") private String currentDbType; public String getCurrentDbType() { return currentDbType; } public void setCurrentDbType(String currentDbType) { this.currentDbType = currentDbType; } }
创建测试类
package test.webservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import test.init.MyConfig; @RestController public class HelloWorldRestController { @Autowired private MyConfig myConfig; @RequestMapping(value = "/",method = RequestMethod.GET) public String hello(){ String currentDbType=myConfig.getCurrentDbType(); return "current.dbType为:"+currentDbType; } }
然后启动项目,在浏览器输入http://localhost:8080/,执行结果如下
内容换成中文(修改为:current.dbType = Oracle数据库),果然也会乱码
依照网上的解决方法,修改MyConfig类的PropertySource注解
@PropertySource(value = "classpath:config/my.properties",encoding ="UTF-8")
然后encoding报红,提示不支持,应该是当前springboot版本太低了,因此还需修改pom.xml
<!--手动引入--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent>
然后重启,浏览器器上查看,已正常
希望对大家有所帮助!
ps:据说使用yml文件,中文不会乱码,验证如下
增加配置类MyConfigYML,配置文件my.yml
current:
dbType : Oracle数据库文件
package test.init; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value = "classpath:config/my.yml") public class MyConfigYML { //获取配置中的信息 @Value("${current.dbType}") private String currentDbType; public String getCurrentDbType() { return currentDbType; } public void setCurrentDbType(String currentDbType) { this.currentDbType = currentDbType; } }
测试类中增加
@Autowired private MyConfigYML myConfigYML; @RequestMapping(value = "/yml",method = RequestMethod.GET) public String helloYml(){ String currentDbType=myConfig.getCurrentDbType(); System.out.println(currentDbType); return "my.yml中current.dbType为:"+currentDbType; }
运行,执行结果为
果然不会乱码,这么来看yml的优势比较明显,哈哈,总觉得写了不少废话。
本文来自博客园,作者:泠雨0702,转载请注明原文链接:https://www.cnblogs.com/lingyu0702/p/16799174.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库