springboot 配置文件profile

Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的,例如,数据库的配置)。

(1)通过设定Environment的ActiveProfiles来设定当前context需要使用的配置环境。在开发中使用@Profile注解类或者方法,达到在不同情况下选择实例化不同的Bean。

(2)通过设定jvm的spring.profiles.active参数来设置配置环境。

(3)Web项目设置在Servlet的context parameter中。

Servlet 2.5及以下:
 

<servlet>      

    <servlet-name>dispatcher</servlet-name>  

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      

    <init-param>          

        <param-name>spring.profiles.active</param-name>          

        <param-value>production</param-value>      

    </init-param>  

</servlet>

Servlet 3.0及以上:

public class WebInit implements WebApplicationInitializer {      

    @Override      
    public void onStartup(ServletContext container) throws ServletException 
    {   
       
        container.setInitParameter("spring.profiles.default", "dev"); 
    
    }  

} 

 

示例Bean

DemoBean.java

package com.shrimpking;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/8 10:32
 */
public class DemoBean
{
    private String content;

    public DemoBean(String content)
    {
        super();
        this.content = content;
    }

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content = content;
    }
}

配置类

ProfileConfig.java

package com.shrimpking;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/8 10:33
 */
@Configuration
public class ProfileConfig
{
    @Bean
    @Profile("dev")
    public DemoBean dev()
    {
        return new DemoBean("run development profile");
    }

    @Bean
    @Profile("prod")
    public DemoBean prod()
    {
        return new DemoBean("run production profile");
    }
}

代码解释

①Profile为dev时实例化dev。

②Profile为prod时实例化prod。

运行

package com.shrimpking;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class Springboot45ProfileApplication
{

    public static void main(String[] args)
    {
        //SpringApplication.run(Springboot45ProfileApplication.class, args);
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext();

        //设置激活状态的profile为dev
        context.getEnvironment().setActiveProfiles("dev");
        //注册
        context.register(ProfileConfig.class);
        //容器刷新
        context.refresh();

        DemoBean demoBean = context.getBean(DemoBean.class);
        System.out.println(demoBean.getContent());

        context.close();
    }

}

代码解释

①先将活动的Profile设置为prod。

②后置注册Bean配置类,不然会报Bean未定义的错误。

③刷新容器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值