日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 控制台程序读取配置文件(原创)

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 控制台程序读取配置文件(原创) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先新建一個(gè)springboot項(xiàng)目,此處省略。

1.新建一個(gè)application.properties

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

?

2.新建一個(gè)類,自動(dòng)讀取對(duì)應(yīng)字段的值

有兩種方式,

第一種

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啟動(dòng)時(shí),添加注解 @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.啟動(dòng)主程序:

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);}@AutowiredPersonProperties personProperties;@AutowiredPerson2Properties person2Properties;@Overridepublic void run(String... args) {System.out.println("程序?qū)嶋H上的入口在這里。");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.運(yùn)行,輸出結(jié)果:

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: 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) 程序?qū)嶋H上的入口在這里。 name:kevin age:6 sex:male 2-name:kevin 2-age:6 2-sex:maleProcess finished with exit code 0

?

鳴謝

參考文獻(xiàn):https://www.cnblogs.com/V1haoge/p/7183408.html?

轉(zhuǎn)載于:https://www.cnblogs.com/wangqiideal/p/11054439.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的springboot 控制台程序读取配置文件(原创)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。