Spring Profile注释示例
时间:2020-02-23 14:35:57 来源:igfitidea点击:
在本教程中,我们将看到Spring @Profile注释。
本教程是Spring启动配置文件示例的扩展名。
配置文件注释表示命名逻辑分组可以通过
- 通过使用
ConfigurableEnvironment.setActiveProfiles(java.lang.String...) - 通过设置
spring.profiles.active属性作为JVM系统属性,作为环境变量,或者在Web中作为servlet上下文参数。
用于Web应用程序的XML。
- 你也可以使用
@ActiveProfiles<同时进行集成测试
@profile注释可以以以下任何方式使用:
- 作为直接或者间接注释的任何类的类型级注释,包括@configuration类
- 作为元注释,出于撰写自定义刻板印象注释的原因
- 它可以使用任何@Bean注释的方法级注释
让我们在一个例子的帮助下看看。
第1步:转到"https://start.spring.io/"并根据下面的屏幕截图创建Spring启动项目。
第2步:在Eclipse中导入Maven项目。
第3步:创建名为"org.igi.theitroad.model"的包创建名为"customer.java"的型号类
package org.igi.theitroad.model;
public class Customer {
int id;
String name;
String email;
public Customer(int id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
第4步:创建名为"org.igi.theitroad.config"的包创建一个名为"configurationDatabase.java"的配置类
package org.igi.theitroad.config;
import org.igi.theitroad.model.Customer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class ConfigurationDatabase {
@Profile("dev")
@Bean(name="customer")
public Customer getDevCustomer()
{
return new Customer(1,"John","[email protected]");
}
@Profile("prod")
@Bean(name="customer")
public Customer getProdCustomer()
{
return new Customer(1,"Martin","[email protected]");
}
}
第5步:在SRC/TEST/JAVA中创建名为"springprofileannotationdevtest的测试类"。
package org.igi.theitroad.SpringProfileAnnotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.igi.theitroad.config.ConfigurationDatabase;
import org.igi.theitroad.model.Customer;
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.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(value="dev")
@ContextConfiguration(classes= {ConfigurationDatabase.class},loader=AnnotationConfigContextLoader.class)
public class SpringProfileAnnotationDevTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testDevDataSource() {
Customer customer = (Customer)applicationContext.getBean("customer");
assertNotNull(customer);
assertEquals("John", customer.getName());
}
}
其中
@springboottest注释用于运行Spring启动测试。
@ContextConfiguration提供类级元数据,以确定如何加载和配置ApplicationContext进行集成测试。
@activeProfiles用于提供在运行集成测试时将激活的配置文件的名称。
步骤6:运行测试时运行上述测试,我们将得到以下输出:
第7步:在src/test/java中创建名为"springprofileannotationProdtest"的另一个测试类。
创建一个名为"springboothelworldapplication.java"的类
package org.igi.theitroad.SpringProfileAnnotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.igi.theitroad.config.ConfigurationDatabase;
import org.igi.theitroad.model.Customer;
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.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(value="prod")
@ContextConfiguration(classes= {ConfigurationDatabase.class},loader=AnnotationConfigContextLoader.class)
public class SpringProfileAnnotationProdTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testDevDataSource() {
Customer customer = (Customer)applicationContext.getBean("customer");
assertNotNull(customer);
assertEquals("Martin", customer.getName());
}
}
步骤8:运行测试时运行上述测试,我们将根据活动配置文件获得不同的bean。

