Spring boot 读取配置文件properties

摘要: Spring boot 读取配置文件, 有如下几种方法1. 直接利用Spring @Value 注解2. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合3. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合采用这三种常见的方法,甚至可以直接用配置文件组装复杂的对象都可以

Spring boot 读取配置文件, 有如下几种方法
1. 直接利用Spring @Value 注解
2. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
3. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
采用这三种常见的方法,甚至可以直接用配置文件组装复杂的对象都可以

一,直接利用Spring @Value 注解
直接在使用的类中使用,比如:

@Service
public class MyCommandService {
	
	@Value("${name:unknown}")
    private String name;

    public String getMessage() {
        return getMessage(name);
    }

    public String getMessage(String name) {
        return "Hello " + name;
    }

}



二. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
package com.yihaomen.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


@Component
@PropertySource("classpath:application.properties")
public class GlobalProperties {
	
	@Value("${name}")
    private String name;
	
	@Value("${address}")
	private String address;	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}



三. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
package com.yihaomen.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties
public class GlobalProperties {
    private String name;	
	private String address;	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}



这样做了之后,在其他类中怎么调用呢,其实直接注入就可以了。 记住我们用 @Component 来注解了这个properties 类,所以可以在其他类中用 @Autowired 注入:
package com.yihaomen.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.yihaomen.springboot.GlobalProperties;


@Service
public class MyCommandService {
	
	@Value("${name:unknown}")
    private String name;
	
	@Autowired
	private GlobalProperties prop;

    public String getMessage() {
        return getMessage(name);
    }

    public String getMessage(String name) {
        return "Hello " + name + " || address: " + prop.getAddress();
    }

}


如何mapping 一个复杂的对象呢,比如有如下一个配置文件:
name=yihaomen-aaa
address=wuhan

#App
app.menus[0].title=Home
app.menus[0].name=Home
app.menus[0].path=/
app.menus[1].title=Login
app.menus[1].name=Login
app.menus[1].path=/login

app.compiler.timeout=5
app.compiler.output-folder=/temp/

app.error=/error/


注解得到对应的类如下:
@Component
@ConfigurationProperties("app") // 前缀找 app.* , 没有前缀,找根
public class AppProperties {

    private String error;
    private List menus = new ArrayList<>();
    private Compiler compiler = new Compiler();

    public static class Menu {
        private String name;
        private String path;
        private String title;

        //getters and setters

        @Override
        public String toString() {
            return "Menu{" +
                    "name='" + name + '\'' +
                    ", path='" + path + '\'' +
                    ", title='" + title + '\'' +
                    '}';
        }
    }

    public static class Compiler {
        private String timeout;
        private String outputFolder;

        //getters and setters

        @Override
        public String toString() {
            return "Compiler{" +
                    "timeout='" + timeout + '\'' +
                    ", outputFolder='" + outputFolder + '\'' +
                    '}';
        }

    }

    //getters and setters
}


spring boot properties 源码下载:
spring boot properties sample download

上一篇: Django 1.10 以上版本 url 配置注意事项
下一篇: 很齐全的spring-cloud学习源码,来自github
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号