首先新建一个springboot项目,此处省略。

1.新建一个application.properties

person.name=kevin
person.age=6
person.sex=male

2.新建一个类,自动读取对应字段的值

有两种方式,

第一种

package cn.wq;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; @Configuration
@EnableConfigurationProperties
@PropertySource("classpath:application.properties")
public class Person2Properties {
@Value("${person.name}")
private String name; @Value("${person.age}")
private String age; @Value("${person.sex}")
private String sex; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}

第二种

package cn.wq;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "person") /*如果使用prefix,则属性名中不能使用@Value来注解,但是必须在spring启动时,添加注解 @EnableConfigurationProperties(PersonProperties.class)*/
public class PersonProperties { private String name;
private String age;
private String sex;
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}

3.启动主程序:

package cn.wq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; @Configuration
@EnableConfigurationProperties(PersonProperties.class)
@SpringBootApplication
public class Application2 implements CommandLineRunner {
public static void main(String[] args){
SpringApplication.run(Application2.class,args);
} @Autowired
PersonProperties personProperties; @Autowired
Person2Properties person2Properties; @Override
public void run(String... args) {
System.out.println("程序实际上的入口在这里。");
System.out.println("name:"+personProperties.getName());
System.out.println("age:"+personProperties.getAge());
System.out.println("sex:"+personProperties.getSex()); System.out.println("2-name:"+person2Properties.getName());
System.out.println("2-age:"+person2Properties.getAge());
System.out.println("2-sex:"+person2Properties.getSex());
}
}

4.运行,输出结果:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE) 2019-06-19 20:20:50.348 INFO 18344 --- [ main] cn.wq.Application2 : Starting Application2 on cnki5213 with PID 18344 (C:\AppConsoleSpringBoot\target\classes started by Administrator in C:\AppConsoleSpringBoot)
2019-06-19 20:20:50.354 INFO 18344 --- [ main] cn.wq.Application2 : No active profile set, falling back to default profiles: default
2019-06-19 20:20:51.303 INFO 18344 --- [ main] cn.wq.Application2 : Started Application2 in 1.564 seconds (JVM running for 3.74)
程序实际上的入口在这里。
name:kevin
age:6
sex:male
2-name:kevin
2-age:6
2-sex:male Process finished with exit code 0

鸣谢

参考文献:https://www.cnblogs.com/V1haoge/p/7183408.html

springboot 控制台程序读取配置文件(原创)的更多相关文章

  1. 控制台程序读取Excel设置角色权限

    摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复283或者20181118可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...

  2. springboot笔记04——读取配置文件+使用slf4j日志

    前言 springboot常用的配置文件有yml和properties两种,当然有必要的时候也可以用xml.我个人更加喜欢用yml,所以我在这里使用yml作为例子.yml或properties配置文件 ...

  3. Win7和Vista的安全机制对于应用程序读取配置文件相关操作的影响(虚拟重定向技术)

    今天构造了一个新版本的XXXX软件,并且在纯净的系统下进行了较为全面的测试.测试中也发现了一些问题.其中包括在Win7测试时程序竟然在另一个目录中创建了文件夹和配置文件,并且进行相关读取操作,却并没有 ...

  4. SpringBoot两种读取配置文件的方式

    方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...

  5. springboot 使用consul 读取配置文件(遇到的坑太多,没记录)

    最终成功版. pom引入mavn依赖: <!--consul--> <dependency> <groupId>org.springframework.cloud& ...

  6. 控制台程序读取WIKI形式的TXT文件并一表格的形式显示在Word中

    'Imports System.Collections.Generic 'Imports System.Text 'Imports System.IO 'Imports office = Micros ...

  7. C#控制台程序读取项目中文件路径

    //使用appdomain获取当前应用程序集的执行目录 string dir = AppDomain.CurrentDomain.BaseDirectory; //使用path获取当前应用程序集的执行 ...

  8. SpringBoot 读取配置文件及profiles切换配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...

  9. 五、Springboot 之 自定义配置文件及读取配置文件

    说明:建议所有的类应该与spring-boot启动程序同级,不如扫描不到 1.核心配置文件是指在resources根目录下的application.properties或application.yml ...

随机推荐

  1. input提示字在有焦点消失或输入改变时消失

    一:获取焦点时 提示字消失 <input type="text" name="textfield" value="这里是提示内容" o ...

  2. Weltanschauung--世界观是人类知觉的基础架构

    世界观(德文:Weltanschauung)意思是「著眼世界之上」.这是德国知识论中所使用的语言,指的是一个「广泛世界的观念」. 它指涉的是一种人类知觉的基础架构,透过它,个体可以理解这个世界并且与它 ...

  3. [Algorithm] Bitwise Operators

    "|" can be used as assign "&" can be used as check // Read, Write, Execute / ...

  4. plotly-dash 简单使用(一)

    plotly-dash 是一个很不错的dashboard 开发平台,基于python 编写,提供了很便捷的dashboard 开发模型 同时扩展上也比较灵活我们可以编写自己的组件. 以下是一个简单的项 ...

  5. RookeyFrame 整个运行流程

    准备开始整理一下这个项目的整体框架,很久没研究这个框架了,心里还是念着的,今儿乘有时间弄一下. 一丁点建议: 先自己一个一个的搬作者的类库,这样就能很好的理解作者的项目结构 每搬一个类库都运行一下哦, ...

  6. 洛谷 P5408 【模板】第一类斯特林数·行

    传送门 首先,有 \[ x^{\overline n}=\sum_k\begin{bmatrix}{n\\ k}\end{bmatrix}x^{k}\\ \] 那么我们只需要求出\(x^{\overl ...

  7. YAML语法基础(K8s基础)

    对于YAML语言,网上有很多将YAML转换为JSON格式的在线转换器,以下内容都可直接验证,另外若有兴趣更深入学习YAML,可到其官方站点去学习,下面介绍的仅仅是比较常用的内容,并非YAML语法的全部 ...

  8. linux 搭建局域网YUM源仓库服务器

    yum简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服 ...

  9. Nginx目录文件列表显示

    项目中使用了tomcat,Nginx,测试阶段,生产阶段经常会有些bug需要调查.需要有些日志管理工具,在没有ELK的情况下,可以通过配置nginx来实现基本的日常查看.不需要登录到Linux服务器上 ...

  10. 将jar包导入maven本地仓库

    https://blog.csdn.net/lvdaan/article/details/79760976 一.首先配置环境变量 在path 中添加 maven的bin 的路径 例如:D:\soft\ ...