之前已经写了一篇读写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文件,中文不会乱码,待验证
标签:currentDbType,resource,SpingBoot,自定义,boot,springframework,import,org,public From: https://www.cnblogs.com/lingyu0702/p/16799174.html