当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Springboot配置文件的使用

详解Springboot配置文件的使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

如果使用idea创建springboot项目,默认会在resource目录下创建application.properties文件,在springboot项目中,也可以使用yml类型的配置文件代替properties文件

一、单个的获取配置文件中的内容

在字段上使用@value("${配置文件中的key}")的方式获取单个的内容

1.在resource目录下创建application.yml文件,并添加一些配置,在yml文件中,key:后面需要添加一个空格,然后是value值,假设配置如下

#注意:在yml文件中添加value值时,value前面需要加一个空格 
ip: 127.0.0.0 
port: 8080 

2.创建一个configcontroller类,获取配置文件中的内容并赋值给相应的字段

package com.example; 
import org.springframework.beans.factory.annotation.value; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
@restcontroller 
public class configcontroller { 
 @value("${ip}")//获取application.yml文件中名为ip的value值 
 private string ip; 
 @value("${port}")//获取application.yml文件中名为port的value值,并且自动完成数据类型转换 
 private integer port; 
 @requestmapping("/config") 
 public string config() { 
  return "ip:"+ip+",port:"+port; 
 } 
} 

3.在srpingbootdemoapplication中启动项目

package com.example; 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.springbootapplication; 
//入口 
@springbootapplication 
public class springbootdemoapplication { 
 public static void main(string[] args) { 
  springapplication.run(springbootdemoapplication.class, args); 
 } 
} 

4.在浏览器中输入,可以看到输出了配置文件中配置的内容

二、使用bean自动注入获取配置文件中的内容

假如配置文件中有很多内容,一个一个获取将会很麻烦,那么我们另外一种方式去获取配置文件中的信息

1.在配置文件中添加以下信息(注意格式),此处我们使用了一个名为devconfig的前缀

devconfig: 
 ip: 127.0.0.0 
 port: 8080 

2.创建configbean,在类中添加@componet和@configurationproperties注解,其中prefix设置为devconfig,将会获取yml中前缀为devconfig下的配置信息

package com.example; 
 import org.springframework.boot.context.properties.configurationproperties; 
import org.springframework.stereotype.component; 
@component 
@configurationproperties(prefix = "devconfig")//获取前缀为devconfig下的配置信息 
public class configbean { 
 private string ip;//名字与配置文件中一致 
 private integer port; 
 public string getip() { 
  return ip; 
 } 
 public void setip(string ip) { 
  this.ip = ip; 
 } 
 public integer getport() { 
  return port; 
 } 
 public void setport(integer port) { 
  this.port = port; 
 } 
} 

3.在configcontroller中使用@autowrite对bean自动注入,实例化bean

import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.beans.factory.annotation.value; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
@restcontroller 
public class configcontroller { 
// @value("${ip}")//获取application.yml文件中名为ip的value值 
// private string ip; 
// 
// @value("${port}")//获取application.yml文件中名为port的value值,并且自动完成数据类型转换 
// private integer port; 
 //自动注入,实例化bean 
 @autowired 
 private configbean configbean; 
 @requestmapping("/config") 
 public string config() { 
  return "另一种方式: ip:"+configbean.getip()+",port:"+configbean.getport(); 
 } 
} 

4.运行程序,输入进行测试

三、多个配置文件切换使用

1.假设开发环境使用ip为:127.0.0.0 使用端口为:8080

          生产环境使用ip为:127.0.0.1 使用端口为:8081

下面来修改配置文件,在resource目录下创建一个名为application-dev.yml文件开发环境使用配置文件和application-produce.yml生产环境配置文件

application-dev.yml

config: 
 ip: 127.0.0.0 
 port: 8080 

application-produce.yml

config: 
 ip: 127.0.0.1 
 port: 8081 

application.yml中配置生效的配置文件,此处设为produce,也就是使用application-produce.yml文件

spring: 
 profiles: 
 active: produce 

2.修改configbean的prefix为config

package com.example; 
import org.springframework.boot.context.properties.configurationproperties; 
import org.springframework.stereotype.component; 
@component 
@configurationproperties(prefix = "config") 
public class configbean { 
 private string ip;//名字与配置文件中一致 
 private integer port; 
 public string getip() { 
  return ip; 
 } 
 public void setip(string ip) { 
  this.ip = ip; 
 } 
 public integer getport() { 
  return port; 
 } 
 public void setport(integer port) { 
  this.port = port; 
 } 
} 

3.运行程序,在浏览器输入进行测试

4.也可通过启动jar包时添加参数来更改生效的配置文件,命令为

java -jar xxx.jar --spring.profiles.active=poduce

以上所述是小编给大家介绍的详解springboot配置文件的使用,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网