boot spring 启动 文本_(013)Spring Boot之启动时设置默认配置文件属性

springboot中通常在application.properties文件中设置属性。也可以通过SpringApplication的setDefaultProperties方法设置属性,如下:

pom.xml

4.0.0

com.edu.spring

springboot

1.0.0

jar

springboot

http://www.example.com

org.springframework.boot

spring-boot-dependencies

2.1.6.RELEASE

import

pom

UTF-8

1.8

1.8

org.springframework.boot

spring-boot-starter

View Code

App.java

packagecom.edu.spring.springboot;importjava.util.HashMap;importjava.util.Map;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringApplication;importorg.springframework.context.ConfigurableApplicationContext;public classApp

{

@Value("${server.host:localhost}")privateString serverhost;public static voidmain( String[] args)

{

SpringApplication app=new SpringApplication(App.class);

Map map=new HashMap<>();

map.put("server.host","127.0.0.1");

app.setDefaultProperties(map);

ConfigurableApplicationContext context=app.run(args);

System.out.println(context.getBean(App.class).serverhost);

context.close();

}

}

View Code

运行结果如下:

如果在application.properties文件中设置相同的属性,最终获取的是配置文件中的属性,如下:

application.properties

server.host=192.168.1.100

View Code

运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值