本文将介绍spring boot profiles的例子。Spring提供了@Profile
注解,我们使用它来创建profile。@Profile
与@Configuration
和spring stereotypes(如@Component
、@Service
等)一起使用。为不同的环境创建不同的配置文件。例如,我们可以有生产、开发和测试等环境。在开发环境中,我们可以启用开发配置文件,在生产环境中,我们可以启用生产配置文件,以此类推。一个配置文件可以使用属性文件**.properties/.yml**、命令行和编程方式激活。我们也可以创建一个默认的配置文件,当没有激活的配置文件时,它将工作。要在属性文件中添加活动的配置文件,我们需要配置spring.profiles.active属性。我们也可以使用spring.profiles.include来配置配置文件,这将包括每个活动的配置文件。当我们使用命令行添加活动配置文件时,在属性文件中添加的活动配置文件将被替换。我们可以通过使用ConfigurableEnvironment
以编程方式添加活动和默认配置文件。在spring boot测试中,我们可以通过使用@ActiveProfiles
注解来添加活动配置文件。我们可以使用application-{profile}.properties
约定的配置文件名称来创建属性文件。这种方法的优点是我们可以配置特定于配置文件的属性。现在请看完整的spring boot profiles的例子。
在我们的例子中,我们将为两个环境创建配置文件,即开发和生产。配置文件被声明为@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);
}
}
我们也在为prod和dev环境创建服务。
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!";
}
}
为了添加活动配置文件,spring boot提供了spring.profiles.active
属性。假设我们想启用dev和animal_dev配置文件,我们可以这样做。
使用application.properties
spring.profiles.active=dev, animal_dev
使用application.yml
spring:
profiles:
active:
- dev
- animal_dev
我们的例子是一个网络项目,其中我们有一个控制器。在控制器中,我们的服务类将在运行时根据活动配置文件自动连接。
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可以为每一个活动的profile包含一套通用的profile。我们可以在我们的属性文件中配置spring.profiles.include属性。现在只要我们使用spring.profiles.active添加活动配置文件,那么默认情况下,由spring.profiles.include配置的配置文件也将被添加。如果我们用命令行替换活动配置文件,由spring.profiles.include配置的配置文件仍将被添加为活动配置文件。我们使用它的方法如下。
使用application.properties
spring.profiles.active=dev
spring.profiles.include: animal_dev
这将添加两个活动配置文件dev和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将从属性文件中获取。
现在我们将使用单元测试案例来测试我们的配置文件。我们已经创建了两个测试方法,一个用于服务类,另一个用于嵌入式服务器配置自定义类。为了添加活动配置文件,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服务应用程序。我们可以直接在我们的测试类中自动连接这个类。但只有当我们的测试类使用元数据@SpringBootTest
进行注解时,它才能发挥作用。
我们可以使用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(...)
以编程方式设置活动配置文件。
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
提供了设置活动和默认配置文件的功能。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);
}
}
我们可以用application-{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
应用-默认.属性
logging.level.org.springframework.web= INFO
logging.level.com.concretepage= INFO
现在,如果我们添加活动配置文件dev,如下所示
application.properties
spring.profiles.active=dev, animal_dev
那么 application-dev.properties
以及 application.properties
将被我们的 spring boot 应用程序使用。
如果我们添加活动配置文件prod,如下所示
application.properties
spring.profiles.active=prod, animal_prod
那么application-prod.properties
和application.properties
将被我们的spring boot应用程序使用。
如果默认配置文件是激活的,那么application-default.properties
和application.properties
将被我们的spring boot应用程序使用。
找到打印屏幕。
以同样的方式,我们可以使用**.yml**,惯例为application-{profile}.yml
。对于配置文件dev和prod,我们可以有**.yml**文件,如下所示。
application-dev.yml
application-prod.yml
application-default.yml
application.yml
祝你学习Spring Boot愉快!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/spring-boot/spring-boot-profiles-example
内容来源于网络,如有侵权,请联系作者删除!