Springboot读取properties配置文件数据

一.使用@ConfigurationProperties来读取

1、Coffer entity

@Configuration
@ConfigurationProperties(prefix = "coffer")
@PropertySource("classpath:config/coffer.properties")
public class Coffer {
    private String brand;
    private Double length;
    private Double width;
    private Double height;                    //省略了get/set方法
    private String[] contains;
    private ArrayList<Fruit> fruits;
    private HashMap<String,Object> map;
}

2、Fruit entity

@Configuration
@ConfigurationProperties(prefix = "coffer.fruits")
@PropertySource("classpath:config/coffer.properties")
public class Fruit {
    private String fruitName;
    private String fruitColor;               //省略了get/set方法 }

3、coffer.properties

coffer.brand=Camel
coffer.length=100.00
coffer.width=80.00
coffer.height=60.00
coffer.contains[0]=Raincoat
coffer.contains[1]=trousers
coffer.contains[2]=hat
coffer.contains[3]=glove
coffer.contains[4]=scarf
coffer.contains[5]=hood
coffer.fruits[0].fruitName=apricot
coffer.fruits[0].fruitColor=yellow
coffer.fruits[1].fruitName=plum
coffer.fruits[1].fruitColor=green
coffer.fruits[2].fruitName=pineapple
coffer.fruits[2].fruitColor=yellow
coffer.fruits[3].fruitName=watermelon
coffer.fruits[3].fruitColor=green
coffer.fruits[4].fruitName=strawberry
coffer.fruits[4].fruitColor=red
coffer.map.name=xiaomao
coffer.map.age=22
coffer.map.gender=female

4、springbootApplicationTest

@SpringBootTest
class SpringbootApplicationTests {

    @Autowired
    private ApplicationContext ioc;

    @Autowired
    private Coffer coffer;

    @Test
    public void springbootTest(){
        System.out.println(coffer);
    }
}

5、result

Coffer{  brand=‘Camel‘,   length=100.0,   width=80.0,   height=60.0,   contains=[Raincoat, trousers, hat, glove, scarf, hood],   fruits=[       Fruit{fruitName=‘apricot‘, fruitColor=‘yellow‘},        Fruit{fruitName=‘plum‘, fruitColor=‘green‘},        Fruit{fruitName=‘pineapple‘, fruitColor=‘yellow‘},        Fruit{fruitName=‘watermelon‘, fruitColor=‘green‘},        Fruit{fruitName=‘strawberry‘, fruitColor=‘red‘}      ],   map={age=22, gender=female, name=xiaomao}}

二、使用@Value来读取

在springTest中无法使用@Value来读取配置属性,需要放到Controller中去读取

@PropertySource("classpath:config/coffer.properties")
@RestController
public class SpringbootController {

    @Value("${coffer.brand}")
    private String brand;
    @Value("${coffer.height}")
    private Double height;

    @RequestMapping("/test")
    public String springbootTest() {
        return brand+"====="+height;
    }
}

原文地址:https://www.cnblogs.com/xiaomaomao/p/12074442.html

时间: 2024-08-30 02:33:37

Springboot读取properties配置文件数据的相关文章

java读取properties配置文件

java读取.properties配置文件 这两天做java项目,用到属性文件,到网上查资料,好半天也没有找到一个满意的方法能让我读取到.properties文件中属性值,很是郁闷,网上讲的获取属性值大概有以下方法,以下三种方法逐渐优化,以达到最好的效果以下都以date.properties文件为例,该文件放在src目录下,文件内容为 startdate=2011-02-07 totalweek=25 方法一: public class Stweek { static private Strin

java读取properties配置文件总结

java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1).使用getResourceAsStream()方法读取配置文件. (2).使用InputStream()流去读取配置文件. 注意:在使用getResourceAsStream()读取配置文件时,要特别注意配置文件的路径的写法. this.getClass.getResourceAsStream(f

java读取.properties配置文件的几种方法

读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put.putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的.因此java.util.Properties类提供了getProperty()和setPr

Java 读取Properties配置文件

读取Properties配置文件的方法,经常忘记,记录下来备忘一下: package utils; import java.io.IOException;import java.io.InputStream; import java.util.Properties; public class PropertyConfig { private static Properties config; static { config = new Properties(); InputStream in =

如题,properties配置文件在项目中是经常用到的,那么读取properties配置文件的方法有哪些呢?

方法一:可以通过java.util.Properties类的load()方法 1 InputStreamin=lnewBufferedInputStream(newFileInputStream(name)); 2 Propertiesp=newProperties(); 3 p.load(in); 方法二:利用spring来读取properties配置文件org.springframework.beans.factory.support.PropertiesBeanDefinitionRead

Java读取properties配置文件常用方法

在开发中对properties文件的操作还是蛮经常的,所以总结了几种操作方法,为后面的开发可以进行参考. 1.通过java.util.ResourceBundle类来读取 这边测试用到了枚举类进行传入文件的key值,然后获取value,可以进行灵活的配置. 通过这种方式读取properties文件不需要加.properties后缀名,只需文件名即可,如果有放在某一个包下,要加包的限定名,如放在com.frame.util包下,则要路径要用com/fram/util config.properti

java中读取properties配置文件用例

在近期需要部署一个项目,所以用到了配置文件. 对于读取配置文件的过程,考虑到效率问题,决定在程序启动时将配置文件内的键值读写入变量. 这样一来,之后程序每次对键值的访问就不用在读配置文件了,而是直接取变量值. 如下是简化之后的用例,展示了一种对properties文件的读取使用方法: 1.创建配置文件data.properties,文件内容如下: user=BUPT pwd=100876 2.创建存储配置文件键值用到的文件Conf.java 1 public class Conf { 2 3 p

读取.properties配置文件(转载)

读取.properties 文件 配置文件的一种,内容以键值对的形式存在,且每个键值对独占一行.#号作为行注释的起始标志,中文注释会自动进行unicode编码.示例: # ip and port of server socket ip=127.0.0.1 port=9999 # error message msg=I'm sorry, bye bye! 通过properties对象操作 public class Demo{ public static void main(String[] arg

Java读取.properties配置文件

1.源码 /** * 读取配置文件[目前只测了.properties配置文件]的工具类 * Created by li on 2015/9/13. */ public class PropertiesUtils { private static PropertiesConfiguration propertiesConfiguration; static { try { //初始化实例 propertiesConfiguration = new PropertiesConfiguration("