为了方便多环境开发,springboot简化了profile功能
application-prod.yaml 开发环境配置
server: port: 5001
application-test.yaml 测试环境配置
server: port: 8080
application.yaml 使用profile
spring:
profiles:
active: test或者prod
第二种激活方式, 命令行激活
java -jar xxxx.jar spring.profiles.active=prod/test
2.条件装配@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {
// ...
}
3.profile分组
spring.profiles.group.production[0]=proddb spring.profiles.group.production[1]=prodmq 使用:--spring.profiles.active=production 激活配置加载优先级
将一些所需要的信息(数据库账号密码等)抽取出来放在一个文件中,放在外面集中管理。就称为外部化配置
- Default properties (specified by setting SpringApplication.setDefaultProperties).
- @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
- Config data (such as application.properties files)
- A RandomValuePropertySource that has properties only in random.*.
- OS environment variables.
- Java System properties (System.getProperties()).
- JNDI attributes from java:comp/env.
- ServletContext init parameters.
- ServletConfig init parameters.
- Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
- Command line arguments.
- properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
- @TestPropertySource annotations on your tests.
- Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
Java文件、Yaml文件、环境变量、命令行参数
@Value("${MAVEN_HOME}") //可以读取环境变量中maven的安装路径
private String home
配置文件查找位置
1.classpath 根目录
2.classpath 根路径下的config目录
3.jar包当前目录
4.jar包当前目录的config目录
5./config子目录的直接子目录
配置文件加载顺序:-
当前jar包内部的application.properties和application.yml
-
当前jar包内部的application-{profile}.properties 和 application-{profile}.yml
-
引用的外部jar包的application.properties和application.yml
-
引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml
指定环境优先、外部优先、排下面的可以覆盖上面的同名配置项
自定义starter细节 1.Starter启动原理starter-pom 引入autoConfigurer 包
2.自定义starter创建一个空项目 里面有一个hello-spring-boot-starter和一个hello-spring-boot-starter-autoconfigurer
hello-spring-boot-starter中什么都不用写,只需在pom.xml中
com.study hello-spring-boot-starter-autoconfigurer 0.0.1-SNAPSHOT
hello-spring-boot-starter-autoconfigurer
先写一个配置文件Bean
@ConfigurationProperties ("study.starter")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
再写一个service
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String username){
return helloProperties.getPrefix() + ":" +username+ helloProperties.getSuffix();
}
}
然后写一个自动配置类
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties会放在容器中
public class HelloServiceAutoConfiguration {
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
因为springboot会去meta-INF/FACTORIES寻找需要自动装配的类,所以还需要在类路径下写一个meta-INF/FACTORIES
#Auto Configure com.study.hellospringbootstarterautoconfigurer.auto.HelloServiceAutoConfiguration
然后clean-install
创建一个测试项目
pom文件中导入hello-spring-boot-starter
写一个properties
study.starter.prefix=JOU study.starter.suffix=Hello
controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(){
String s = helloService.sayHello("zhangsan");
return s;
}
}
如果在测试类中写一个新的config,那么springboot就会走myconfig中的helloservice
@Configuration
public class MyConfig{
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService
}
}



