SpringBoot--properties的使用

Posted 洋子哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot--properties的使用相关的知识,希望对你有一定的参考价值。

项目中经常需要进行一些配置,一般会使用springboot默认的application.properties文件,也可以自己创建配置文件

 

一,application.properties配置 

logging.path=/Users/zhang_guang_yang/IDEA15/Logs/
logging.file=SpringBoot.log

# strings
com.gy.dateFormatter=dd-MM-yyyy

# strings
com.pleasure.local=/files

# array
com.pleasure.urls[0]=http://www.baidu.com/
com.pleasure.urls[1]=http://www.google.com/
com.pleasure.urls[2]=http://www.blog.csdn.com/

# map
com.pleasure.admin[username]=pleasure
com.pleasure.admin[password]=123456

  读取

  1,通过一个配置类来读取: 

package com.example.demo.config;

import org.springframework.boot.autoconfigure.session.StoreType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang_guang_yang on 2018/12/2.
 */

@Component
@ConfigurationProperties(prefix = "com.pleasure")
public class AppProperties {

    private String local;
    private Map<String, String> admin = new HashMap<String ,String>();
    private List<String> urls = new ArrayList<String >();

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public Map<String, String> getAdmin() {
        return admin;
    }

    public void setAdmin(Map<String, String> admin) {
        this.admin = admin;
    }

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }
}

  获取属性 

@RestController
@EnableAutoConfiguration
public class PropertiesController {


    @Autowired
    private AppProperties appProperties;

 
    @RequestMapping("/properties")
    public String properties(){

        System.out.println("local: " + appProperties.getLocal());
        System.out.println("admin: " + appProperties.getAdmin().get("username");
        return success;
     }
}

  2, @Value读取 

  @Value("${com.gy.dateFormatter}")
    private String dateFormat;

  3,通过Environment对象读取

  

   @Autowired
    private Environment evn;

  整个实现

  

package com.example.demo.controllers;

import com.example.demo.config.AppProperties;
import com.example.demo.domain.Info;
import com.sun.tools.javac.comp.Env;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zhang_guang_yang on 2018/11/20.
 */
@RestController
@EnableAutoConfiguration
public class PropertiesController {


    @Autowired
    private AppProperties appProperties;

    @Value("${com.gy.dateFormatter}")
    private String dateFormat;

    @Autowired
    private Environment evn;

    @Autowired
    private Info info;

    @RequestMapping("/properties")
    public String properties(){

        System.out.println("local: " + appProperties.getLocal());
        System.out.println("admin: " + appProperties.getAdmin().get("username") + " " + appProperties.getAdmin().get("password"));
        System.out.println("url: " + appProperties.getUrls().get(0));
        System.out.println("date formatter: " + dateFormat);

        System.out.println(evn.getProperty("com.gy.dateFormatter"));

        System.out.println("info: " + info.getName());
        return "success";
    }
}

  

 

二,自定义properties文件和类

  1,info.properties 

zgy.info.version=1.0
zgy.info.name="good mall"
zgy.info.bundleId="zgy.good.mall"

  2, 定义类

package com.example.demo.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

/**
 * Created by zhang_guang_yang on 2018/11/20.
 */

@ConfigurationProperties(prefix = "zgy.info")
@PropertySource("classpath:config/info.properties")

public class Info {

    private String version;
    private String name;
    private String bundleId;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

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

    public String getBundleId() {
        return bundleId;
    }

    public void setBundleId(String bundleId) {
        this.bundleId = bundleId;
    }


}

  3,注册扫描 

@ComponentScan(basePackages = {"com.example.demo.domain"})
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

  4,获取属性

  

  @Autowired
    private Info info;

  

以上是关于SpringBoot--properties的使用的主要内容,如果未能解决你的问题,请参考以下文章

springboot properties文件中的数据通过@Value()形式注入

springBoot:Properties和YAML配置文件

Springboot2-application.properties文件中文注释都是unicode编码

Springboot-application.properties

秒表的使用方法和技巧(秒表的使用方法)

使用“使用严格”作为“使用强”的备份