SpringBoot 读取配置文件的4种方式
- 1 使用@Value注解
- 2 使用@ConfigurationProperties注解
- 3 使用Environment
- 4 使用@PropertySource注解
- 4.0 new.yml
- 4.1 @PropertySource 支持 yml/yaml 文件
- 4.2 使用@PropertySource注解
配置文件
server:port: 8080servlet:context-path: /elasticsearchspring:application:name: elasticsearch
1 使用@Value注解
在类中使用@Value注解来注入配置值
package com.xu.test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringBootTests {@Value("${spring.application.name}")private String name;@Testpublic void test() {System.out.println(name);}}
elasticsearch
2 使用@ConfigurationProperties注解
创建一个Java Bean类,并使用@ConfigurationProperties注解指定配置文件的前缀,然后Spring Boot会自动将配置值注入到该Bean中。
package com.xu.test.conf;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @author hyacinth*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class MyConf {private String host;private String port;private String password;private String timeout;}
package com.xu.test;import cn.hutool.json.JSONUtil;
import com.xu.test.conf.MyConf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringBootTests {@Autowiredprivate MyConf conf;@Testpublic void test() {System.out.println(JSONUtil.toJsonPrettyStr(conf));}}
{"host": "127.0.0.1","port": "6379","password": null,"timeout": "10000"
}
3 使用Environment
package com.xu.test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;@SpringBootTest
class SpringBootTests {@Autowiredprivate Environment env;@Testpublic void test() {System.out.println(env.getProperty("spring.application.name"));}}
elasticsearch
4 使用@PropertySource注解
如果有其他的配置文件,需要在配置类上使用@PropertySource注解指定配置文件的路径,然后通过@Value注解或Environment接口来读取配置值(@PropertySource只支持properties文件)。
4.0 new.yml
test:name: "测试配置文件"
4.1 @PropertySource 支持 yml/yaml 文件
package com.xu.test.conf;import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.core.io.support.ResourcePropertySource;import java.io.IOException;
import java.util.Properties;/*** @author hyacinth*/
public class YamlConfigFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {name = (name != null) ? name : resource.getResource().getFilename();if (StrUtil.isBlank(name)) {throw new RuntimeException("配置文件不存在!");}if (!resource.getResource().exists()) {return new PropertiesPropertySource(name, new Properties());} else if (StrUtil.containsAnyIgnoreCase(name, ".yml", ".yaml")) {Properties yml = loadYml(resource);return new PropertiesPropertySource(name, yml);} else {return new ResourcePropertySource(name, resource);}}private Properties loadYml(EncodedResource resource) {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(resource.getResource());factory.afterPropertiesSet();return factory.getObject();}}
4.2 使用@PropertySource注解
package com.xu.test.conf;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;/*** @author hyacinth*/
@Data
@Component
@PropertySource(value = "classpath:new.yml", factory = YamlConfigFactory.class)
public class MyConf {@Value("${test.name}")private String name;@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}}
package com.xu.test;import cn.hutool.json.JSONUtil;
import com.xu.test.conf.MyConf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringBootTests {@Autowiredprivate MyConf conf;@Testpublic void test() {System.out.println(JSONUtil.toJsonPrettyStr(conf));}}
{"name": "测试配置文件"
}