spring学习--spring profile

在系统开发过程中,经常会碰到各种环境问题,其中一种就是,不同的环境需要不同的数据源,不同的环境需要不同的URL,有些是可以通过修改配置文件来做到的。但是当这种切换多的时候,配置起来就容易出错。spring profile 就是个优秀的解决方案。可以根据不同的环境配置不同的bean。
默认情况下bean是不设置环境的,此时spring都会创建,但是那些设置了特殊环境的bean只有在特定环境下才会加载。

一、如何开启spring profile

开启spring profile 需要设置 spring.profiles.default或者spring.profile.active变量的值。若设置了spring.profiles.active的值那么spring 会使用激活的类。若没有设置,那么就使用spring.profiles.default设置的值,激活default的类。

这两个数值有很多设置的方式 任一种都能达到效果。
①设置在web.xml中,作为springMVC的初始化参数

    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value> 
                classpath:spring/spring-mvc.xml 
                </param-value>
            </init-param>
            <init-param>
                <param-name>spring.profiles.default</param-name>
                <param-value>zj</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

②:设置在web.xml中,作为springMVC的初始化参数

    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>zj</param-value>
    </context-param>

③:JVM参数方式: -Dspring.profiles.active=”zj”

二、设置不同环境下不同的bean
开启spring profile 之后 需要把根据环境变化的对象进行设置。

@Configuration
@ComponentScan
public class ConditionCofig {


    @Bean
    @Profile("zj")
    public WorkService getWindowsService(){
        return  new WindowsService();
    }

    @Bean
    @Profile("sj")
    public WorkService getLinuxService(){
        return new LinuxService();
    }
}

三、举例
常用举例:
根据环境设置不同的配置文件:

    <beans profile="zj"  >  
        <context:property-placeholder location="classpath*:zj.properties" />  
    </beans>  
    <beans profile="sj"  >  
        <context:property-placeholder location="classpath*:sj.properties" />  
    </beans>  

设置不同环境良好的避免了 切换环境带来的不变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config Server是一个用于集中管理和提供应用程序配置的服务器。它可以从Git仓库中获取配置文件,并将其提供给客户端应用程序。配置服务器的配置可以在服务端的配置文件中进行设置,如引用\[1\]所示。其中,可以指定Git仓库的URI、搜索路径、分支等信息。 客户端应用程序需要在bootstrap.properties或bootstrap.yml文件中配置相关信息,如引用\[2\]所示。其中,需要指定配置服务器的URI、应用程序名称、激活的配置文件和分支等。 为了集成Config Server,需要在项目的依赖中添加spring-cloud-config-server的依赖项,如引用\[3\]所示。同时,需要编写一个启动类,并使用@EnableConfigServer注解来启用Config Server功能。 在启动类中,可以设置Config Server的配置,如Git仓库的地址、账号和密码等。 启动Config Server后,可以通过访问http://127.0.0.1:8080/{application}/{profile}.yaml来获取相应的配置文件。其中,{application}是应用程序名称,{profile}是激活的配置文件。 以上是关于Spring Cloud Config Server的简要介绍和配置方法。如果您有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *2* [Spring Cloud - 2 (Spring Cloud Config Server)](https://blog.csdn.net/ysl19910806/article/details/96755134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring Cloud配置中心之Config Server](https://blog.csdn.net/mg0324/article/details/122635762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值