Spring Boot 读取配置文件

本文介绍了Spring Boot中如何读取核心配置文件application.properties及自定义配置文件config.properties的信息,并展示了两种常用读取方法。

了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息。

Spring-Boot读取配置文件的方式:
1.读取核心配置文件信息application.properties的内容
     核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

    配置文件application.properties内容如下:

test.msg=Hello World SpringBoot  

    方式一:使用@Value方式(常用)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebController {
	@Value("${test.msg}")
	private String msg;
	
	@RequestMapping("/index1") 
	public String index1(){
		return "方式一:"+msg;
	}
}

    注意:在@Value的${}中包含的是核心配置文件中的键名。

    http://localhost:8088/index1时得到:"方式一:Hello World SpringBoot"

    方式二:使用Environment方式


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebController {
	@Autowired
	private Environment env;
	
	@RequestMapping("/index2") 
	public String index2(){
		return "方式二:"+env.getProperty("test.msg");
	}
}

    注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。
    访问:http://localhost:8088/index2时得到:"方式二:Hello World SpringBoot"


2.读取自定义配置文件信息,例如:config.properties
        为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件config.properties

        resources/config.properties内容如下:

user.name=lvgang
user.age=35

        创建实体类:


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


@Component
@ConfigurationProperties(prefix = "user",locations = "classpath:config.properties")   
public class MyWebConfig{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

        注意:在@ConfigurationProperties注释中有两个属性:
                locations:指定配置文件的所在位置
                prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)
        使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

        创建测试类:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller  
public class ConfigController {
	@Autowired
	private MyWebConfig conf;
	
	@RequestMapping("/test") 
	public @ResponseBody String test() {
		return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge(); 
	}
}

        注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。
        访问:http://localhost:8088/test时得到:"Name:lvgang---Age:35"

转载于:https://my.oschina.net/sdlvzg/blog/1553472

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值