[email protected]

[email protected]@[email protected]@[email protected][email protected]..

目录
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
三、多Profile资源文件

[email protected]

[email protected]

开发环境

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("development")
public class DevelopmentConfig {
    @Bean
    public AppData getAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("development");

        return appData;
    }
}

正式环境

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("production")
public class ProductionConfig {

    @Bean
    public AppData getAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("production");

        return appData;
    }
}

[email protected]

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class AppConfig {
    // 开发环境
    @Bean("appConfigData")
    @Profile("development")
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

    // 正式环境
    @Bean("appConfigData")
    @Profile("production")
    public AppData getProductionAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app production");

        return appData;
    }
}

[email protected]

1、定义注解

开发环境

package com.example.demo.annotation;

import org.springframework.context.annotation.Profile;

import Java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("development")
public @interface Development {
}

正式环境

package com.example.demo.annotation;

import org.springframework.context.annotation.Profile;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

2、使用注解

package com.example.demo.config;

import com.example.demo.annotation.Development;
import com.example.demo.annotation.Production;
import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    // 开发环境
    @Bean("appConfigData")
    @Development
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

    // 正式环境
    @Bean("appConfigData")
    @Production
    public AppData getProductionAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app production");

        return appData;
    }
}

[email protected]

[email protected]

application.properties

spring.profiles.active=production 

application.yml

spring:
  profiles:
    active: production

[email protected]

java -jar target/demo-0.0.1-SNAPSHOT.jar  --spring.profiles.active=production

三、多Profile资源文件

配置文件

# 公共配置
application.properties

# development
application-development.properties

# production
application-production.properties

application.properties

# 激活配置文件
spring.profiles.active=production

application-development.properties

server.port=8081

application-production.properties

server.port=8082

完整代码 https://github.com/mouday/spring-boot-demo/tree/master/SpringBoot-Profile

参考
[email protected]

[email protected]文章就介绍到这了,更多相关SpringBoot 多环境配置内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

延伸阅读:

上一篇:Java CAS机制详解

下一篇:SpringBoot读写操作yml配置文件方法