欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot——读取.properties配置文件解决方案

程序员文章站 2022-05-02 11:41:19
...

解决方案

Spring Boot 读取properties配置文件时,默认读取的是application.properties。 

方法一:@ConfigurationProperties注解方式

@Component 表示将该类标识为Bean

@ConfigurationProperties(prefix = "demo")用于绑定属性,其中prefix表示所绑定的属性的前缀。

@PropertySource(value = "config.properties")表示配置文件路径。

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

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

/**
 * @date 2017年6月1日 下午4:34:18 
 * @version V1.0
 * @since JDK : 1.7
 */
@Component
@ConfigurationProperties(prefix = "com.zyd")
// PropertySource默认取application.properties
// @PropertySource(value = "config.properties")
public class PropertiesConfig {

    public String type3;
    public Map<String, String> login = new HashMap<String, String>();
    public List<String> urls = new ArrayList<>();

    public String getType3() {
        return type3;
    }

    public String getTitle3() {
        try {
            return new String(title3.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return title3;
    }


    public Map<String, String> getLogin() {
        return login;
    }

    public void setLogin(Map<String, String> login) {
        this.login = login;
    }

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

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

} 

方法二:@Value注解方式

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date 2017年6月1日 下午3:49:30 
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {

    @Value("${com.zyd.type}")
    private String type;

    /**
     * 
     * 第二种方式:使用`@Value("${propertyName}")`注解
     * 
     */
    @RequestMapping("/value")
    public Map<String, Object> value() throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", type);
        return map;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(Applaction.class);
        application.run(args);
    }
}

注:如果@Value${}所包含的键名在application.properties配置文件中不存在的话,会抛出异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.name}"

 方法三:Environment

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date 2017年6月1日 下午3:49:30
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {

    @Autowired
    private Environment env;

    /**
     * 
     * 第三种方式:使用`Environment`
     * 
     */
    @RequestMapping("/env")
    public Map<String, Object> env() throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", env.getProperty("com.zyd.type2"));
        return map;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(Applaction.class);
        application.run(args);
    }
}

方法四:PropertiesLoaderUtils

https://blog.csdn.net/thc1987/article/details/78789426

参考文章

https://blog.csdn.net/thc1987/article/details/78789426

https://blog.csdn.net/dkbnull/article/details/81953190

https://blog.csdn.net/liuchuanhong1/article/details/78106648