SPRINGBOOT读取PROPERTIES配置文件数据过程详解

这篇文章主要介绍了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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • spring boot使用i18n时properties文件中文乱码问题的解决方法

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产.开发这样的程序的过程,就称为国际化. 在springboot使用i18n进行国际化文件配置时,文件名为messages_zh_CN.properties的文件中填写中文信息,当使用浏览器进行访问时,出现中文乱码,此时在idea中进行修改setting

  • SpringBoot获取yml和properties配置文件的内容

    (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> <dependency> <groupId>org.springframework.boot</groupId>

  • Spring Boot2.0 @ConfigurationProperties使用详解

    引言 Spring Boot的一个便捷功能是外部化配置,可以轻松访问属性文件中定义的属性.本文将详细介绍@ConfigurationProperties的使用. 配置项目POM 在pom.xml中定义Spring-Boot 为parent <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>

  • 详解Spring Boot加载properties和yml配置文件

    一.系统启动后注入配置 package com.example.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframewo

  • Spring Boot的properties配置文件读取

    我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了. 具体启动过程以前的博客写过Spring Boot启动过程(一),这次入口在SpringApplication类中: private ConfigurableEnvironment prepareEnvironment( SpringApplicationRunListeners listeners, Applicatio

  • 详解spring boot 使用application.properties 进行外部配置

    application.properties大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成war包或者jar用于生产环境时,我们可以手动修改环境变量而不用再重新编译. spring boo默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是"/"等等,可以在这里看spring boot默认的配置信息http://docs.spring.io/spring-boot/docs/curr

  • Spring Boot中配置文件application.properties使用

    一.配置文档配置项的调用 启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容. 二.绑定对象bean调用 有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解@ConfigurationProperties(prefix = "com")来指明使用哪个 @ConfigurationProperties(prefix = &quo

  • 在SpringBoot下读取自定义properties配置文件的方法

    SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource

  • SPRINGBOOT读取PROPERTIES配置文件数据过程详解

    这篇文章主要介绍了SPRINGBOOT读取PROPERTIES配置文件数据过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.使用@ConfigurationProperties来读取 1.Coffer entity @Configuration @ConfigurationProperties(prefix = "coffer") @PropertySource("classpath:config/coffer.p

  • Android读取properties配置文件的实例详解

    Android读取properties配置文件的实例详解 因为一些配置信息,多处用到的.且以后可能变更的,我想写个.prorperties配置文件给管理起来. 我把配置文件放在了assets文件夹下 appConfig.properties: serverUrl=http://192.168.1.155 import java.io.InputStream; import java.util.Properties; import android.content.Context; /** * 读取

  • spring无法读取properties文件数据问题详解

    1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-context.xml配置文件中获取的:service中注入的@Value配置可以从applicationContext.xml中获取的.所以,如果要在controller中注入属性配置,需要在相应servlet文件中添加配置,同applicationContext.xml中一样. <bean class="org.springframework.be

  • JDBC连接MySQL数据库批量插入数据过程详解

    这篇文章主要介绍了JDBC连接MySQL数据库批量插入数据过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.读取本地json数据 2.jdbc理解数据库 3.批量插入 maven 引入jar包: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2

  • Springboot集成mybatis与jsp过程详解

    目录 什么是Spring Boot? springboot特点 springboot快速搭建项目 新建项目springboot_mybatis_jsp 项目配置 配置项目目录 配置工作目录(working directory) 配置pom.xml 配置application.properties 编写代码 建表t_user 编写User.java 编写UserMapper.xml 编写UserService.java.UserServiceImpl.java 编写Controller 什么是Sp

  • JavaScript处理解析JSON数据过程详解

    JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON 数据不需要任何特殊的 API 或工具包. JSON的规则很简单: 对象是一个无序的"'名称/值'对"集合.一个对象以"{"(左括号)开始,"}"(右括号)结束.每个"名称"后跟一个":"(冒号):"

  • SpringBoot配置拦截器实现过程详解

    目录 如何配置拦截器 拦截器设置容易出现的问题 如何取消拦截操作 实例-登录验证 如何配置拦截器 step1: 自定义拦截器 /** * 自定义拦截器 */ public class MyInterceptor implements HandlerInterceptor { private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class); /** * 在请求匹配controller之前执行,返回t

  • SpringBoot读取properties文件配置项过程解析

    使用SpringBoot开发过程中,难免需要配置相关数据项,然后在Java代码中@Autowired注入并使用. 我们应该如何读取properties文件中的配置项呢? 基于SpringBoot项目,配置项一般都存放在application.properties文件中.有2种常用的方法: 1.使用@Value注解标注在Field上面 2.使用@ConfigurationProperties注解标注在类或者方法上 为了讲解方便,附上application.properties文件配置好的数据项 如

  • SpringBoot整合Netty心跳机制过程详解

    前言 Netty 是一个高性能的 NIO 网络框架,本文基于 SpringBoot 以常见的心跳机制来认识 Netty. 最终能达到的效果: 客户端每隔 N 秒检测是否需要发送心跳. 服务端也每隔 N 秒检测是否需要发送心跳. 服务端可以主动 push 消息到客户端. 基于 SpringBoot 监控,可以查看实时连接以及各种应用信息. IdleStateHandler Netty 可以使用 IdleStateHandler 来实现连接管理,当连接空闲时间太长(没有发送.接收消息)时则会触发一个

  • IDEA搭建SpringBoot多模块聚合工程过程详解(多模块聚合工程)

    目录 一.搭建环境 (1)IDEA创建maven工程 (2)修改pom依赖 (4)创建app-pojo子工程 (5)创建app-mapper子工程 (6)创建app-service子工程 (7)创建app-web子工程 (8)统一依赖管理 (9)添加web依赖 (10)关联子工程依赖 (11)测试子工程依赖是否成功 二.集成MyBatis (1)父工程中添加MyBatis依赖管理 (2)app-mapper子工程引入MyBatis (4)配置mybatis和mysql数据源 (5)service

随机推荐