发布于2021-05-29 19:52 阅读(558) 评论(0) 点赞(12) 收藏(1)
为了方便多环境
适配,springboot简化了profile功能。
默认配置文件 application.yaml
,任何时候都会加载
指定环境配置文件 application-{环境名称}.yaml,例如:
application-dev.yaml
application-prod.yaml
application-test.yaml
环境配置文件只有激活指定环境才会生效
配置文件激活方式
默认配置文件
中选择
激活那个环境配置文件 ,如下:
spring.profiles.active=prod
# 指定激活环境和默认环境配置文件都会生效
java -jar xxx.jar --spring.profiles.active=prod
注意:
没有激活指定配置环境
,则默认配置环境要提供配置所需的一切功能,例如:封装值的Bean重复的属性
,默认是依次覆盖
,也就是会使用最后一个配置文件中的配置。
@Profile条件装配功能:
public interface Person {
String getName();
Integer getAge();
}
@RestController
public class HelloController {
@Value("${person.name:李四}")
private String name;
@Autowired
private Person person;
@GetMapping("/")
public String hello(){
return person.getClass().toString();
}
@GetMapping("/person")
public Person person(){
return person;
}
}
//在配置文件中激活prod环境这个类才会生效
@Profile("prod")
@Component
@ConfigurationProperties("person")
@Data
public class PersonProd{
private String name;
private Integer age;
}
//在配置文件中激活test环境这个类才会生效
@Profile("test")
@Component
@ConfigurationProperties("person")
@Data
public class PersonTest{
private String name;
private Integer age;
}
prod
)server.port=8080
spring.profiles.active=prod
person:
name: AISMALL-Prod
age: 20
server:
port: 8000
http://localhost:8000/person
返回结果:
{"name":"AISMALL-Prod","age":20}
同时加载多个配置文件
,如果出现重复配置,配置依次覆盖pring.profiles.active=myprod
# myprod分组
spring.profiles.group.myprod[0]=prod
spring.profiles.group.myprod[1]=dev
# test分组
spring.profiles.group.mytest[0]=test
其实就是配置文件
我们一般不愿意
在类中将属性的值写死,这样不利于修改,所有我们采用配置文件给属性赋值的方式,就是外部化配置
可以采用哪些文件作为外部化配置文件:
properties
文件YAML
文件为什么要自定义starter,其实就是为了方便,换句话说就是懒,当官放给我提供的场景启动器不能满足我们的要求,我们还需要导入额外的依赖包,就可以通过自定义的场景启动器一次性导入进来。
可以参考官方提供的启动器为例,找个简单点的启动器把:spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
自动配置的包
:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
autoconfigure包中:配置
META-INF/spring.factories
中的EnableAutoConfiguration
的值:org.springframework.boot.autoconfigure.EnableAutoConfiguration
=\com.aismall.hello.auto.HelloServiceAutoConfiguration
当我们引入这个场景启动器后,使得项目启动加载指定的自动配置类
编写自动配置类 xxxAutoConfiguration -> xxxxProperties
总体步骤:
引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项
我们定义一个启动器,引入场景后可以给人打招呼
参考上面的步骤:
maven
工程作为父工程Maven工程即可
):hello-spring-boot-starter
SpringBoot工程
):hello-spring-boot-starter-autoconfigure
<dependencies>
<dependency>
<groupId>com.aismall</groupId>
<artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
@Configuration
//默认HelloProperties放在容器中
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration{
//当容器中没有这个bean才注入
@ConditionalOnMissingBean(HelloService.class)
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
//配置类绑定
@ConfigurationProperties("aismall.hello")
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;
}
}
//当容器中没有这个bean才会将这个bean注入
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName){
return helloProperties.getPrefix() + ":"+userName+"》"+helloProperties.getSuffix();
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.aismall.hello.auto.HelloServiceAutoConfiguration
SpringBoot不是一个单纯的框架,它更相当于一个boot(启动项),SpringBoot内部整合了许多框架,例如:
如果想很好的理解SpringBoot,这些技术也需要理解:
断点打在启动类,进行Debug:
创建 SpringApplication
初始启动引导器
(List<Bootstrapper>
):去spring.factories文件中找Bootstrapper
。应用上下文初始化器
,去spring.factories找 ApplicationContextInitializer
应用监听器
,去spring.factories找 ApplicationListener
运行 SpringApplication
创建引导上下文
(Context环境)createBootstrapContext()
RunListener
,运行监听器,为了方便所有Listener进行事件感知
我们可以定义这些组件,自定义完之后在配置文件中配置它们即可,这个三个组件的配置位置:resources/META-INF/spring.factories
自定义组件:
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("MyApplicationContextInitializer ....initialize.... ");
}
}
public class MyApplicationListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("MyApplicationListener.....onApplicationEvent...");
}
}
public class MySpringApplicationRunListener implements SpringApplicationRunListener {
private SpringApplication application;
public MySpringApplicationRunListener(SpringApplication application, String[] args){
this.application = application;
}
@Override
public void starting(ConfigurableBootstrapContext bootstrapContext) {
System.out.println("MySpringApplicationRunListener....starting....");
}
@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
System.out.println("MySpringApplicationRunListener....environmentPrepared....");
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....contextPrepared....");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....contextLoaded....");
}
@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....started....");
}
@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....running....");
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("MySpringApplicationRunListener....failed....");
}
}
org.springframework.context.ApplicationContextInitializer=\
com.aismall.boot.listener.MyApplicationContextInitializer
org.springframework.context.ApplicationListener=\
com.aismall.boot.listener.MyApplicationListener
org.springframework.boot.SpringApplicationRunListener=\
com.aismall.boot.listener.MySpringApplicationRunListener
自定义组件:
@Order(2)
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner....run....");
}
}
@Order(1)
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("MyApplicationRunner...run...");
}
}
到此为止就结束了,不得不说SpringBoot的自动配置功能真的强大,源码分析部分有时间最好看看,跟着debug走一遍,知其然知其所以然才能运用自如,我还需要加强修炼。
记录一下:2021-5-27-15:50
原文链接:https://blog.csdn.net/weixin_45583303/article/details/117317339
作者:小泽圈儿郎
链接:http://www.javaheidong.com/blog/article/207242/8de9f56a75a2958eb9ee/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!