SpringBoot多环境配置yml文件以及引用外部配置文件的两种方式,并且读取yml配置内容

作者环境:SpringBoot 2.1.4.RELEASE

多环境配置

  1. 不同环境的配置yml文件名不一样:
  • appointment.yml
  • appointment-dev.yml(开发环境)
  • appointment-test.yml(测试环境)
  • appointment-prod.yml(正式环境)
    yml文件
  1. yml示例
    application.yml 示例:
    注意:配置多环境yml 文件必须是application开头文件
    spring:
    	profiles:
    	 	active: @spring.profiles.active@
    
    application-dev.yml 示例:
    server:
    	port: 8081
    
    application-test.yml 示例:
    server:
    	port: 8083
    
    application-prod.yml 示例:
    server:
    	port: 8082
    
    pom文件示例:
    默认激活dev测试环境
    <profiles>
    	<profile>
    		<id>dev</id>
    		<activation>
    			<!--默认激活-->
    			<activeByDefault>true</activeByDefault>
    		</activation>
    		<properties>
    			<spring.profiles.active>dev</spring.profiles.active>
    		</properties>
    	</profile>
    
    	<!--测试环境-->
    	<profile>
    		<id>test</id>
    		<properties>
    			<spring.profiles.active>test</spring.profiles.active>
    		</properties>
    	</profile>
    
    	<!--生产环境-->
    	<profile>
    		<id>prod</id>
    		<properties>
    			<spring.profiles.active>prod</spring.profiles.active>
    		</properties>
    	</profile>
    </profiles>
    
    修改好yml以及pom文件之后,运行 mvn package -P test ,-P 之后值为环境的ID,比如dev是开发环境,prod是正式环境(由于直接runSpringBoot项目主类是开发环境,所以本次采用打包方式),不同环境,只需更改-P环境变量的值即可。
    启动打包好的jar包
    直接运行 java -jar xxx.jar 即可:
    在这里插入图片描述
    可以看到,我们mvn package -P test,打包测试环境的yml配置文件,启动端口正是之前配置的8083,达到了我们实现多环境配置的效果。

引用外部yml配置文件的两种方式

1.第一种:
创建application-common.yml 文件
修改application.yml文件,如:

spring:
  profiles:
    active: @spring.profiles.active@
    include: common

include:common common 是application-后缀,此方式必须是application开头yml文件

测试引用外部配置文件

编辑 application-common.yml,如:

common:
  test: 外部yml文件测试common

编写一个class,Common类,然后使用@Value注入的方式来获取application-common.yml 内容

@Component
public class Common {

    @Value("${common.test}")
    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

编写一个controller,TestController,如:

@RestController()
public class TestController {

    @Autowired
    Common common;

    @GetMapping("/test")
    public String test() {

        return common.getTest();
    }
}

启动项目,并且调用该请求127.0.0.1:8081/test,如:
调用结果
可见成功获取application-common.yml 文件中common.test key值内容
2.第二种引用外部yml文件方式
Spring Framework有两个类加载YAML文件,YamlPropertiesFactoryBean和YamlMapFactoryBean
本次我们使用YamlPropertiesFactoryBean来引用
首先编写一个yml文件,并且在配置文件中写入数据,如config.yml:
在这里插入图片描述
然后,在任意spring 容器启动可加载的地方,编写如下代码:

SpringBootApplication
public class DemoApplication {

	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() {
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		yaml.setResources(new ClassPathResource("config.yml"));
		configurer.setProperties(yaml.getObject());
		return configurer;
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

代码为@Bean 代码块,new ClassPathResource(“你的配置文件名即可”)
接下来我们新建一个Class ,Config 类,如:

public class Config {
    @Value("${config.test}")
    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

新建一个请求,如:

	@Autowired
    Config config;

    @GetMapping("/configTest")
    public String configTest() {

        return config.getTest();
    }

接下来我们启动项目并且访问configTest,如:
在这里插入图片描述
成功访问!

初次写博客,写的不好的地方还请大家见谅,以后不定期和大家分享自己的一些心得,谢谢
参考资料:
1.https://www.jianshu.com/p/d258b26ef889
2.https://yulaiz.com/spring-boot-maven-profiles/

猜你喜欢

转载自blog.csdn.net/qq_36174487/article/details/89419347