springboot入门_获取属性文件中的值
Posted wlzq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot入门_获取属性文件中的值相关的知识,希望对你有一定的参考价值。
在上一篇文章中,记录了用springboot实现输出一个hello world到前台的程序,本文记录学习springboot读取属性文件中配置信息。
框架属性文件(application.properties)
创建一个springboot项目,并引入相关依赖,POM文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 9 <groupId>org.allen.learn</groupId> 10 <artifactId>springboot_propertiesparam</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 13 <packaging>war</packaging> 14 15 <!-- Inherit defaults from Spring Boot --> 16 <parent> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-parent</artifactId> 19 <version>2.0.4.RELEASE</version> 20 </parent> 21 22 <!-- Add typical dependencies for a web application --> 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-devtools</artifactId> 32 <optional>true</optional> 33 </dependency> 34 35 </dependencies> 36 37 </project>
在resources路径下创建application.properties文件,并写入我们的属性名称和值,内容如:
allen.properties.type=springboot
allen.properties.title=springboot获取属性文件值
写一个class来接收属性文件中的值,代码如下:
1 package org.allen.learn.property; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 @Component 7 @ConfigurationProperties(prefix="allen.properties")//指定前缀是allen.properties 8 public class PropertiesConfig { 9 10 public String type; 11 12 public String title; 13 14 public String getType() { 15 return type; 16 } 17 18 public void setType(String type) { 19 this.type = type; 20 } 21 22 public String getTitle() { 23 return title; 24 } 25 26 public void setTitle(String title) { 27 this.title = title; 28 } 29 30 }
在controller使用我们上边类中接收到的属性文件中的值,代码:
1 package org.allen.learn.controller; 2 3 import java.io.UnsupportedEncodingException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import org.allen.learn.property.PropertiesConfig; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 @RestController 13 public class PropertiesController { 14 15 @Autowired 16 private PropertiesConfig propertiesConfig; 17 18 @RequestMapping("/c1") 19 public String getProperties1() { 20 Map<String, Object> map = new HashMap<String, Object>(); 21 map.put("属性type", propertiesConfig.getType()); 22 try { 23 //application.properties 默认编码格式iso-8859-1,此处中文转码 24 map.put("属性title", new String(propertiesConfig.getTitle().getBytes("iso-8859-1"), "utf-8")); 25 } catch (UnsupportedEncodingException e) { 26 e.printStackTrace(); 27 } 28 return map.toString(); 29 } 30 31 }
启动项目,在浏览器中请求 http://localhost:8080/c1
在页面上可以看到我们在属性文件中默认给的值。这种取值方式我们需要创建一个类来关联值,还有一中方法就是使用@Value来获取属性文件中的值,代码:
1 package org.allen.learn.controller; 2 3 import java.io.UnsupportedEncodingException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 @RestController 12 public class ValueController { 13 14 @Value("${allen.properties.type}")//指定取值属性名 15 private String type; 16 @Value("${allen.properties.title}") 17 private String title; 18 19 @RequestMapping("/c2") 20 public String getProperties1() { 21 Map<String, Object> map = new HashMap<String, Object>(); 22 map.put("value获取属性type", type); 23 try { 24 //application.properties 默认编码格式iso-8859-1,此处中文转码 25 map.put("value获取属性title", new String(title.getBytes("iso-8859-1"), "utf-8")); 26 } catch (UnsupportedEncodingException e) { 27 e.printStackTrace(); 28 } 29 return map.toString(); 30 } 31 32 }
启动项目,在浏览器中请求 http://localhost:8080/c2 可以在浏览器中看到
自定义属性文件
在resources路径下创建一个myjdbc.properties文件,并写入内容,代码如:
myjdbc.username=root
myjdbc.password=123456
编写一个class来接收属性,代码:
1 package org.allen.learn.property; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.context.annotation.PropertySource; 5 import org.springframework.stereotype.Component; 6 7 @Component 8 @PropertySource(value="classpath:myjdbc.properties")//指定自定义属性文件 9 @ConfigurationProperties(prefix="myjdbc")//前缀 10 public class MyJdbcProperties { 11 12 private String username; 13 14 private String password; 15 16 public String getUsername() { 17 return username; 18 } 19 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 24 public String getPassword() { 25 return password; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 32 33 }
创建一个controller来获取值,代码:
1 package org.allen.learn.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.allen.learn.property.MyJdbcProperties; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 @RestController 12 public class MyPropertiesController { 13 14 @Autowired 15 private MyJdbcProperties myJdbcProperties; 16 17 @RequestMapping("/c3") 18 public String getMyProperties() { 19 Map<String, Object> map = new HashMap<String, Object>(); 20 map.put("myUsername", myJdbcProperties.getUsername()); 21 map.put("myPassword", myJdbcProperties.getPassword()); 22 return map.toString(); 23 } 24 25 }
启动项目,在浏览器中输入请求地址 http://localhost:8080/c3 浏览器中会输出内容:
下载源码 https://files.cnblogs.com/files/wlzq/springboot_propertiesparam.zip
以上是关于springboot入门_获取属性文件中的值的主要内容,如果未能解决你的问题,请参考以下文章
3.springboot:springboot配置文件(配置文件YAML属性文件值注入<@Value@ConfigurationProperties@PropertySource,@Im(代码片
3springboot:springboot配置文件(配置文件YAML属性文件值注入<@Value@ConfigurationProperties@PropertySource,@Imp(代码片