springboot 控制台程序读取配置文件(原创)
首先新建一个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 控制台程序读取配置文件(原创)的更多相关文章
- 控制台程序读取Excel设置角色权限
摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复283或者20181118可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...
- springboot笔记04——读取配置文件+使用slf4j日志
前言 springboot常用的配置文件有yml和properties两种,当然有必要的时候也可以用xml.我个人更加喜欢用yml,所以我在这里使用yml作为例子.yml或properties配置文件 ...
- Win7和Vista的安全机制对于应用程序读取配置文件相关操作的影响(虚拟重定向技术)
今天构造了一个新版本的XXXX软件,并且在纯净的系统下进行了较为全面的测试.测试中也发现了一些问题.其中包括在Win7测试时程序竟然在另一个目录中创建了文件夹和配置文件,并且进行相关读取操作,却并没有 ...
- SpringBoot两种读取配置文件的方式
方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...
- springboot 使用consul 读取配置文件(遇到的坑太多,没记录)
最终成功版. pom引入mavn依赖: <!--consul--> <dependency> <groupId>org.springframework.cloud& ...
- 控制台程序读取WIKI形式的TXT文件并一表格的形式显示在Word中
'Imports System.Collections.Generic 'Imports System.Text 'Imports System.IO 'Imports office = Micros ...
- C#控制台程序读取项目中文件路径
//使用appdomain获取当前应用程序集的执行目录 string dir = AppDomain.CurrentDomain.BaseDirectory; //使用path获取当前应用程序集的执行 ...
- SpringBoot 读取配置文件及profiles切换配置文件
读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...
- 五、Springboot 之 自定义配置文件及读取配置文件
说明:建议所有的类应该与spring-boot启动程序同级,不如扫描不到 1.核心配置文件是指在resources根目录下的application.properties或application.yml ...
随机推荐
- rsync+inotify百万级文件实时同步
实验环境:Centos7.4 目的:将源服务器的文件实时同步至目标服务器 源服务器:10.11.1.107 目标服务器:10.11.1.106 分别在两个节点安装rsync yum -y instal ...
- 用mysql实现类似于oracle dblink的功能
用mysql实现类似于oracle dblink的功能 首先看看有没有federated 引擎. mysql> show engines; +------------+----------+ ...
- jsbridge与通信模型
三层通信模型: 应用层.解释层.会话层: 通信协议: 通信原语: 报文格式: 网络层: _evaluateJavascript 会话层: #define kQueueHasMessage @&qu ...
- python语言(四)关键字参数、内置函数、导入第三方模块、OS模块、时间模块
一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会 ...
- MongoDB的安装、基本操作
此说明文档针对的community版本是v4.2.0(1)下载下载官网,此时的community版本是v4.2.0https://www.mongodb.com/download-center/com ...
- 面向开发人员的Windows错误报告(WER)
Windows错误报告是更新的Windows XP上Dr.Watson的替代品.它监视故障并收集可以发送到要分析的服务器(如果用户允许)的有用信息.这项功能帮助微软修复了很多错误——由于收到的报告,微 ...
- C++中vector的使用总结
vector简单说明 vector也是一个容器,并且是个顺序容器.顺序容器有可变长数组vector.双向链表list.双端队列deque. 顺序容器的定义,是因为容器元素的位置和他们的值大小无关,也就 ...
- 第01组 团队Git现场编程实战
目录 一.组员职责分工 二.github 的提交日志截图(鼓励小粒度提交) 三.程序运行截图 四.程序运行环境 五.GUI界面 六.基础功能实现 七.鼓励有想法且有用的功能 八.遇到的困难及解决方法 ...
- Java常见集合的默认大小及扩容机制
在面试后台开发的过程中,集合是面试的热话题,不仅要知道各集合的区别用法,还要知道集合的扩容机制,今天我们就来谈下ArrayList 和 HashMap的默认大小以及扩容机制. 在 Java 7 中,查 ...
- 为什么不要使用==比较Integer?
比较Integer的时候,不要用==. 查看Integer的源码,如下: /** * Returns an {@code Integer} instance representing the spec ...