在本文中,我们将向您展示如何在Spring Boot中使用@Profile
以及如何对其进行测试。
经过测试:
- Spring Boot 2.1.2发布
- Maven 3
1.项目结构
标准的Maven项目结构。
2.项目依赖性
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-profile</artifactId>
<packaging>jar</packaging>
<name>Spring Boot Profiles Example</name>
<description>Spring Boot Profiles Example</description>
<url>https://www.mkyong.com</url>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
3. Spring Boot
在Spring Boot中,默认配置文件是' default
'。 查看以下天气服务。
3.1接口。
WeatherService.java
package com.mkyong.service;
public interface WeatherService {
String forecast();
}
3.2配置文件:晴天且默认。
SunnyDayService.java
package com.mkyong.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile({"sunny", "default"})
public class SunnyDayService implements WeatherService {
@Override
public String forecast() {
return "Today is sunny day!";
}
}
3.3简介:下雨。
RainingDayService.java
package com.mkyong.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("raining")
public class RainingDayService implements WeatherService {
@Override
public String forecast() {
return "Today is raining day!";
}
}
3.4启动Spring Boot应用程序。
Application.java
package com.mkyong;
import com.mkyong.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private WeatherService weatherService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) {
System.out.println(weatherService.forecast());
}
}
3.5属性文件。
application.properties
# default profile is 'default'
#spring.profiles.active=sunny
logging.level.=error
spring.main.banner-mode=off
4.单元测试
一些单元测试示例。
4.1单元测试服务等级。 通过@ActiveProfiles
设置活动配置文件
TestWeatherService.java
package com.mkyong;
import com.mkyong.service.WeatherService;
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.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("raining")
public class TestWeatherService {
@Autowired
WeatherService weatherService;
@Test
public void testRainingProfile() {
String output = weatherService.forecast();
assertThat(output).contains("Today is raining day!");
}
}
4.2对Spring Boot应用程序进行单元测试。 您可以通过属性spring.profiles.active
设置活动配置文件
TestApplication.java
package com.mkyong;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.rule.OutputCapture;
import static org.assertj.core.api.Assertions.assertThat;
public class TestApplication {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Test
public void testDefaultProfile() {
Application.main(new String[0]);
String output = this.outputCapture.toString();
assertThat(output).contains("Today is sunny day!");
}
@Test
public void testRainingProfile() {
System.setProperty("spring.profiles.active", "raining");
Application.main(new String[0]);
String output = this.outputCapture.toString();
assertThat(output).contains("Today is raining day!");
}
@Test
public void testRainingProfile_withDoption() {
Application.main(new String[]{"--spring.profiles.active=raining"});
String output = this.outputCapture.toString();
assertThat(output).contains("Today is raining day!");
}
@After
public void after() {
System.clearProperty("spring.profiles.active");
}
}
PS 归功于此Spring Boot SampleProfileApplicationTests
5.演示
打包并运行它。
$ mvn package
#default profile, sunny day!
$ java -jar target/spring-boot-profile-1.0.jar
Today is sunny day!
# set a profile
$ java -jar -Dspring.profiles.active=raining target/spring-boot-profile-1.0.jar
Today is raining day!
下载源代码
$ git clone https://github.com/mkyong/spring-boot.git
$ cd profile-简单
$ mvn软件包
$ java -jar -Dspring.profiles.active = raining目标/spring-boot-profile-1.0.jar
参考文献
翻译自: https://mkyong.com/spring-boot/spring-boot-profiles-example/