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

多环境配置

  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/

Spring Boot中,可以使用多个配置文件来实现多环境的配置。首先,需要创建多个配置文件,例如application-dev.yml(开发环境)、application-prod.yml(生产环境)、application-test.yml(测试环境)。 然后,可以通过指定启动参数来选择使用不同的配置文件。比如,在测试环境中可以使用以下命令启动应用程序:java -jar 项目.jar --spring.profiles.active=test。在生产环境中可以使用以下命令启动应用程序:java -jar 项目.jar --spring.profiles.active=prod。这种方式可以灵活地选择使用不同的配置文件。 另一种方式是在pom.xml文件中指定环境配置。具体来说,可以在application.yml中选择需要使用的配置文件。可以通过在application.yml文件中的spring.profiles.active属性来指定需要使用的配置文件的后缀。例如,如果要使用开发环境的配置,可以将spring.profiles.active设置为dev。 无论使用哪种方式,都需要在配置文件中指定相应环境下的配置内容。例如,在dev配置文件中可以设置数据库的URL、用户名、密码以及服务器端口。类似地,在test和prod配置文件中也可以设置相应环境的配置。 总之,通过创建多个配置文件,并通过指定启动参数或在配置文件中指定需要使用的配置,可以实现Spring Boot的多环境配置。这样可以在不同的环境中灵活地配置应用程序所需的参数,避免重复配置的情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值