springboot基础配置
Spring Boot一些基础配置
全局配置文件
在src/main/resources目录下,Spring Boot提供了一个名为application.properties或.yml的全局配置文件,可对一些默认配置的配置值进行修改。
附:application.properties中可配置所有官方属性
自定义属性值
Spring Boot允许我们在application.properties下自定义一些属性,比如.properties中格式为:
zcb.name = Bai
zcb.title = Spring-Boot-Learn
.yml中为:
zcb:
name: Bai
title: Spring-Boot-Learn
定义一个实体类,通过@Value("${属性名}")
来加载配置文件中的自定义属性值:
@Component
@Data
public class Zcb {
@Value("${zcb.name}")
private String name;
@Value("${zcb.title}")
private String title;
}
编写IndexController,注入该Bean:
@RestController
public class IndexController {
@Autowired
private Zcb zcb;
@GetMapping()
public String getData() {
return zcb.getName() + "\t" + zcb.getTitle();
}
}
效果如下:
在属性非常多的情况下,也可以定义一个和配置文件对应的实体:
@ConfigurationProperties(prefix = "zcb")
@Data
public class ZcbConfig {
private String name;
private String title;
}
通过注解@ConfigurationProperties(prefix="zcb")
指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。@Data注解是lombok中的可以省略get、set方法。
除此之外还需在Spring Boot入口类加上注解@EnableConfigurationProperties({ConfigBean.class})
来启用该配置:
@SpringBootApplication
@EnableConfigurationProperties({ZcbConfig.class})
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
属性之间的引用
.properties中
zcb.name = Bai
zcb.title = Spring-Boot-Learn
zcb.all = ${zcb.name}--${zcb.title}
.yml中
zcb:
name: Bai
title: Spring-Boot-Learn
all : ${zcb.name}--${zcb.title}
自定义配置文件
在src/main/resources目录下可以自定义配置文件,如test.yml:
.properties中
test.age=22
test.name=bai
.yml中
test:
age: 22
name: bai
定义一个对应该配置文件的实体:
@Component
@Data
@ConfigurationProperties(prefix = "test")
@PropertySource(value = "classpath:test.yml", factory = YamlPropertySourceFactory.class)
//@PropertySource("classpath:test.properties")
public class TestConfig {
private String name;
private int age;
}
注解@PropertySource("classpath:test.properties")
指明了使用哪个配置文件,这个注解只能读取.properties不能读取.yml,想读取.yml就要自定义工厂。要使用该配置Bean,同样也需要在入口类里使用注解@EnableConfigurationProperties({TestConfigBean.class})
来启用该配置。
自定义读取yml工厂:
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties ymlProperties = factory.getObject();
String propertyName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(propertyName, ymlProperties);
}
}
使用xml配置
虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解@ImportResource({"classpath:some-application.xml"})
来引入xml配置文件。
Profile配置
Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以application-{profile}.properties
的格式命,其中{profile}
为环境标识。比如定义两个配置文件:
-
application-dev.properties:开发环境
server.port=8080
-
application-prod.properties:生产环境
server.port=8081
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active
属性来设置,其值对应{profile}
值。
如:spring.profiles.active=dev
就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令java -jar xxx.jar --spring.profiles.active={profile}
切换不同的环境配置。