栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring Boot】Spring Boot Profiles 示例 | 配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring Boot】Spring Boot Profiles 示例 | 配置文件

文章目录
  • 创建 Spring 配置文件
  • 使用属性文件 (.properties/.yml) 添加活动配置文件
  • 创建控制器和主类
  • 使用命令行添加活动配置文件
  • 包括活动配置文件
  • 使用 @ActiveProfiles 的活动配置文件单元测试
  • 使用 SpringApplication.setAdditionalProfiles(...) 以编程方式设置活动配置文件
  • 使用 ConfigurableEnvironment.setActiveProfiles(...) 以编程方式设置活动配置文件
  • 设置默认配置文件
  • 配置文件特定属性文件
  • 参考文献
  • 源码下载

此页面将介绍 Spring Boot 配置文件示例。

Spring 提供了 @Profile 注释,我们使用它来创建配置文件。

@Profile 与@Configuration 和spring 构造型如@Component、@Service 等一起使用。

为不同的环境创建不同的配置文件。例如,我们可以拥有生产、开发和测试等环境。

在开发环境中我们可以启用开发配置文件,在生产环境中我们可以启用生产配置文件等等。

可以使用属性文件 .properties/.yml、命令行和以编程方式激活配置文件。

我们还可以创建一个默认配置文件,当没有活动配置文件时该配置文件将起作用。要在属性文件中添加活动配置文件,我们需要配置 spring.profiles.active 属性。

我们还可以使用 spring.profiles.include 配置配置文件,该配置文件将包含在每个活动配置文件中。

当我们使用命令行添加活动配置文件时,属性文件中添加的活动配置文件将被替换。

我们可以使用 ConfigurableEnvironment 以编程方式添加活动和默认配置文件。

在 Spring Boot 测试中,我们可以使用 @ActiveProfiles 注释添加活动配置文件。

我们可以使用约定 application-{profile}.properties 使用配置文件名称创建属性文件。

这种方法的优点是我们可以配置特定于配置文件的属性。

现在逐步找到完整的 spring 引导配置文件示例。

创建 Spring 配置文件

在我们的示例中,我们将为两个环境创建配置文件,即开发和生产。

配置文件被声明为在类级别注释的@profile("profile-name")。

我们正在创建四个配置文件,它是dev, prod, animal_dev和 animal_prod。

在我们的示例中,我们正在创建一个将配置服务器端口和上下文路径的两个组件,我们将有两个服务类。

查找将为生产环境配置服务器端口和上下文路径的组件。

ProdCustomizer.java

package com.concretepage.config;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("prod")
public class ProdCustomizer implements EmbeddedServletContainerCustomizer {
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setContextPath("/spring-boot-prod");
		container.setPort(8585);
	}
}  

查找将为开发环境配置服务器端口和上下文路径的组件。

DevCustomizer.java

package com.concretepage.config;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("dev")
public class DevCustomizer implements EmbeddedServletContainerCustomizer {
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setContextPath("/spring-boot-dev");
		container.setPort(8484);
	}
}  

我们还为生产和开发环境创建服务。

Animal.java

package com.concretepage.service;
public interface Animal {
	String getMessage();
} 

Elephant.java

package com.concretepage.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("animal_dev")
public class Elephant implements Animal {
	@Override
	public String getMessage() {
		return "Hello Elephant!";
	}

} 

Lion.java

package com.concretepage.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("animal_prod")
public class Lion implements Animal {
	@Override
	public String getMessage() {
		return "Hello Lion!";
	}
} 
使用属性文件 (.properties/.yml) 添加活动配置文件

要添加活动配置文件,spring boot提供spring.profiles.active属性。假设我们想启用dev和animal_dev配置,我们可以执行以下操作。

使用 application.properties

spring.profiles.active=dev, animal_dev 

使用 application.yml

spring:
  profiles:
    active:
     - dev
     - animal_dev 
创建控制器和主类

我们的示例是一个web项目,其中有一个控制器。在控制器中,我们的服务类将在运行时根据活动配置文件自动连接。

HelloController.java

package com.concretepage.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.concretepage.service.Animal;
@RestController
public class HelloController {
   @Autowired
   private Animal animal;	
   @GetMapping("/")	
   public String getMessage() {
	   return animal.getMessage();
   }
} 

使用下面的类运行演示。

MyApplication.java

package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
	SpringApplication.run(MyApplication.class, args);
    }       
} 

访问URL

http://localhost:8484/spring-boot-dev/ 

输出

Hello Elephant! 
使用命令行添加活动配置文件

我们可以使用带有java命令的命令行添加活动配置文件。

在这种情况下,在属性文件中配置的活动配置文件将替换为在命令行中传递的活动配置文件。

在我们的属性文件中,我们添加了dev环境的活动配置文件。

所以默认情况下,应用程序将从dev环境开始。

现在,我们将使用命令行添加prod活动配置文件。

假设我们有一个名为spring-boot-demo-0.0.1-SNAPSHOT.jar的可执行JAR。我们将参数传递为-Dspring.profiles.active=animal_prod。

java -jar -Dspring.profiles.active="prod, animal_prod" spring-boot-demo-0.0.1-SNAPSHOT.jar 

对于单个配置文件,我们可以使用如下命令。

java -jar -Dspring.profiles.active=animal_prod spring-boot-demo-0.0.1-SNAPSHOT.jar 
包括活动配置文件

Spring boot可以为每个活动配置文件包含一组通用配置文件。

我们可以在属性文件中配置spring.profiles.include属性。

现在,每当我们使用spring.profiles.active添加活动配置文件时,默认情况下也会添加由spring.profiles.include配置的配置文件。

如果我们用命令行替换活动配置文件,由spring.profiles.include配置的配置文件仍将添加为活动配置文件。

我们使用它如下。

使用 application.properties

spring.profiles.active=dev
spring.profiles.include=animal_dev 

使用 application.yml

spring:
  profiles:
    active: dev
    include: animal_dev 

运行命令

java -jar -Dspring.profiles.active=prod spring-boot-demo-0.0.1-SNAPSHOT.jar 

上面的命令行将添加两个活动配置文件prod和animal_dev。

animal_dev将从属性文件中获取。

使用 @ActiveProfiles 的活动配置文件单元测试

现在,我们将使用单元测试用例测试我们的配置文件。

我们创建了两个测试方法,一个用于服务类,另一个用于嵌入式服务器配置定制器类。

为了添加活动配置文件,spring测试框架提供@ActiveProfiles注释。

找到测试类。

ActiveProfileTest.java

package com.concretepage;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.concretepage.service.Animal;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"prod","animal_prod"})
public class ActiveProfileTest {
    @Autowired
    private Animal animal;	
	@Autowired
	private TestRestTemplate restTemplate;
	@Test
	public void serviceTest() {
		String message = animal.getMessage();
		assertThat(message).isEqualTo("Hello Lion!");
	}
	@Test
	public void webAppTest() {
		String url = "http://localhost:8585/spring-boot-prod/";
		String body = this.restTemplate.getForObject(url, String.class);
		assertThat(body).isEqualTo("Hello Lion!");
	}
} 

@SpringBootTest: 它用于为基于spring boot的应用程序运行测试用例。测试类将由@SpringBootTest注释进行注释。

@ActiveProfiles: Spring测试框架提供了这个注释,以便在我们的测试用例中使用活动配置文件。

TestRestTemplate : Spring测试框架提供TestRestTemplate来测试基于Spring boot的REST Web服务应用程序。我们可以在测试类中直接自动连接这个类。但是,只有当我们的测试类使用元数据webEnvironment=WebEnvironment.RANDOM_PORT端口用@SpringBootTest注释时,它才会起作用。

使用 SpringApplication.setAdditionalProfiles(…) 以编程方式设置活动配置文件

我们可以使用SpringApplication.setAdditionalProfiles(...)以编程方式设置活动配置文件,如下所示。

MyApplication.java

package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.setAdditionalProfiles("dev","animal_dev");
	application.run(args);
    }       
} 

我们需要在run()方法之前调用setAdditionalProfiles()方法。在这里,我们添加了活动配置文件dev和animal_dev。

使用 ConfigurableEnvironment.setActiveProfiles(…) 以编程方式设置活动配置文件

我们可以使用ConfigurableEnvironment.setActiveProfiles(...)以编程方式设置活动配置文件,如下所示。

MyApplication.java

package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	ConfigurableEnvironment environment = new StandardEnvironment();
	environment.setActiveProfiles("dev","animal_dev");
	application.setEnvironment(environment);
	application.run(args);
    }       
} 

ConfigurableEnvironment提供设置活动和默认配置文件的功能。

要设置活动配置文件,请使用setActiveProfiles(…)方法。

ConfigurableEnvironment是一个接口,它有许多实现。

我们在这里使用的是StandardEnvironment,它是ConfigurableEnvironment的实现类。

设置默认配置文件

如果没有启用配置文件,则应加载默认配置文件。

我们可以设置默认配置文件,当我们没有配置任何活动配置文件时,spring boot应用程序将使用该默认配置文件。

我们可以使用ConfigurableEnvironment的setDefaultProfiles(…)方法设置默认配置文件,如下所示。

MyApplication.java

package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	ConfigurableEnvironment environment = new StandardEnvironment();
	environment.setDefaultProfiles("dev","animal_dev");
	application.setEnvironment(environment);
	application.run(args);
    }       
} 
配置文件特定属性文件

我们可以使用约定应用程序{profile}.properties使属性文件特定于配置文件。

通过这种方式,我们可以为不同的环境创建单独的属性文件。

如果我们添加了一个活动的配置文件,那么spring boot应用程序将只使用相应的属性文件。

我们也可以有一个默认配置文件的属性文件。

假设我们有用于开发环境的dev和用于生产环境的prod的配置文件。

然后我们可以有如下配置文件特定的属性。

application-dev.properties

logging.level.org.springframework.web= DEBUG
logging.level.com.concretepage= DEBUG 

application-prod.properties

logging.level.org.springframework.web= ERROR
logging.level.com.concretepage= INFO

logging.path = concretepage/logs 

application-default.properties

logging.level.org.springframework.web= INFO
logging.level.com.concretepage= INFO 

现在,如果我们添加活动配置文件dev,如下所示

application.properties

spring.profiles.active=dev, animal_dev 

然后,我们的spring boot应用程序将使用application-dev.properties和application.properties。

如果我们按如下方式添加活动配置文件prod

application.properties

spring.profiles.active=prod, animal_prod 

然后,我们的spring boot应用程序将使用application-prod.properties以及application.properties。

如果默认配置文件处于活动状态,则我们的spring boot应用程序将使用application-default.properties以及application.properties。

同样,我们可以将.yml与约定应用程序{profile}.yml一起使用。对于概要文件dev和prod,我们可以有如下.yml文件。

application-dev.yml
application-prod.yml
application-default.yml
application.yml
参考文献

【1】Spring Boot Features:Profiles
【2】Spring Boot Profiles Example

源码下载

spring-boot-profiles-example.zip

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305552.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号