配置文件
server:
port: 8080
servlet:
context-path: /elasticsearch
spring:
application:
name: elasticsearch
在类中使用@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;
@Test
public void test() {
System.out.println(name);
}
}
elasticsearch
创建一个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 {
@Autowired
private MyConf conf;
@Test
public void test() {
System.out.println(JSONUtil.toJsonPrettyStr(conf));
}
}
{
"host": "127.0.0.1",
"port": "6379",
"password": null,
"timeout": "10000"
}
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 {
@Autowired
private Environment env;
@Test
public void test() {
System.out.println(env.getProperty("spring.application.name"));
}
}
elasticsearch
如果有其他的配置文件,需要在配置类上使用@PropertySource注解指定配置文件的路径,然后通过@Value注解或Environment接口来读取配置值(@PropertySource只支持properties文件)。
test:
name: "测试配置文件"
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 {
@Override
public 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();
}
}
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;
@Bean
public 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 {
@Autowired
private MyConf conf;
@Test
public void test() {
System.out.println(JSONUtil.toJsonPrettyStr(conf));
}
}
{
"name": "测试配置文件"
}