1.Environment 可以通过Environment的getProperty()方法来获取想要的配置信息。
application.properties文件
server.port=8081
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Environment env;
@GetMapping("/hellow")
public String hello(){
String point = env.getProperty("server.port");
return point;
}
}
输出
2.value.
@Value("${server.port}")
private String point;
@GetMapping("/hellow")
public String hello(){
return point;
}
3.@ConfigurationProperties ,prefix定义配置得前缀
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix="com.test")
@Component
public class HelloConfig {
private String name;
private String node;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String showInfo(){
return name+"--"+node;
}
}
@Autowired
private HelloConfig con;
@GetMapping("/hollow")
public String hello(){
return con.showInfo();
}
输出
以上三种方法配置文件都是application.
4.自定义配置文件。用@PropertySource指定配置文件位置。这个注解不能读取.yml的配置文件,需要重新定义factory。
配置文件为hello.yml
package com.example.demo.config;
import lombok.Data;
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;
@PropertySource(value={"classpath:hello.yml"},factory = YmlConfigFactory.class)
@Component
@Data
public class HelloConfig {
@Value("${com.test.name}")
private String name;
@Value("${com.test.node}")
private String node;
public String showInfo(){
return name+"--"+node;
}
}
重新定义工厂类如下
package com.example.demo.config;
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.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
@RestController
public class HelloController {
@Autowired
private HelloConfig con;
@GetMapping("/hollow")
public String hello(){
return con.showInfo();
}
}
输出