Spring应用 2 profile多环境配置管理

本文详细介绍了Spring应用中如何使用profile进行多环境配置管理,包括不同环境加载不同配置文件、接口实现及日志级别。通过示例展示了如何在XML配置中定义profile,并提供了激活profile的多种方式,如ENV、JVM参数、servlet上下文和Junit标注。同时,文章也提及了Spring的@Profile注解在组件和配置类上的应用,以及maven多环境打包配置。
摘要由CSDN通过智能技术生成
Spring应用 2 profile加载不同环境配置

profile的应用

通过Spring profile属性可以帮助我们快速方便的切换环境。使得开发、测试、生产环境得到快速切换。
1.不同环境加载不同的配置文件(数据库连接地址、api接口、用户密码等)
2.不同环境加载不同的接口实现类(数据源、业务逻辑、结果处理)
3.不同环境定义不同的日志级别

通过下面的示例中,
可以通过profile可以选择加载不同的配置文件,需要将不同环境的配置文件放置在不同的目录下。
可以在不同环境下加载不同的数据源和工具。例如在开发环境,数据源可能是txt文本,但是在线上环境的数据源就能自动替换成mysql 的数据源。
可以提供不同的工具,例如在开发环境需要将数据落地到本地txt,在线上环境则需要更新到mysql数据库中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

     <beans profile="dev">
          <bean id="instance" class="com.mdq.util.DevInstance" /> 
          <bean id="dataSourse" class="com.mdq.data.TxtDataSource" />     
          <context:property-placeholder location="classpath*:dev/*.properties" ignore-unresolvable="true" />   
     </beans>

     <beans profile="prod">
          <bean id="instance" class="com.mdq.util.ProdInstance" />
          <bean id="dataSourse" class="com.mdq.data.MysqlDataSource" />
          <context:property-placeholder location="classpath*:test/*.properties" ignore-unresolvable="true" />       
     </beans>

     //以下两个实例不受profile的影响,和正常情况下加载实例一样。
     <bean id="common1" class="com.mdq.util.Common1" />
     <bean id="common2" class="com.mdq.util.Common2" />
     
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值