Step 01:
package com.config.enviroment.profile.annotation;
public class User {
private String userName;
private String email;
private Long salary;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
}
Step 02:
package com.config.enviroment.profile.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* For Development Environment
* @author hieunt29
*
*/
@Configuration
@Profile(\
"DEV\")
public class UserDevEnviroment {
/*
* Injected bean
*/
@Bean(name = \
"userBean\")
public User addUser() {
User user =
new User();
user.setUserName(\
"Nguyen Trung Hieu DEV\");
user.setSalary(123456789l);
user.setEmail(\
"DEV-hieunt29@fsoft.com.vn\");
return user;
}
}
Step 03:
package com.config.enviroment.profile.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* For Production Environment
*
* @author hieunt29
*
*/
@Configuration
@Profile(\
"PROD\")
public class UserProdEnviroment {
/*
* Injected bean
*/
@Bean(name = \
"userBean\")
public User addUser() {
User user =
new User();
user.setUserName(\
"Nguyen Trung Hieu PROD\");
user.setSalary(999999999l);
user.setEmail(\
"PROD-hieunt29@fsoft.com.vn\");
return user;
}
}
Step 04: src/test/java
package com.config.enviroment.profile.annotation.TEST;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.config.enviroment.profile.annotation.User;
import com.config.enviroment.profile.annotation.UserDevEnviroment;
import com.config.enviroment.profile.annotation.UserProdEnviroment;
/**
* For development enviroment
*
* @author hieunt29
*
*/
@RunWith(SpringJUnit4ClassRunner.
class)
// ApplicationContext will be loaded from \"UserDevEnviroment.class, UserProdEnviroment.class\"
@ContextConfiguration(classes = { UserDevEnviroment.
class,
UserProdEnviroment.
class })
//DEV environment will be actived
@ActiveProfiles(\
"DEV\")
public class UserBeanDevTest {
@Autowired
User userBean;
@Test
public void testAddUser() {
assertThat(userBean.getUserName(), equalTo(\
"Nguyen Trung Hieu DEV\"));
assertThat(userBean.getSalary(), equalTo(123456789L));
assertThat(userBean.getEmail(), equalTo(\
"DEV-hieunt29@fsoft.com.vn\"));
}
}
Step 05:src/test/java
package com.config.enviroment.profile.annotation.TEST;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.config.enviroment.profile.annotation.User;
import com.config.enviroment.profile.annotation.UserDevEnviroment;
import com.config.enviroment.profile.annotation.UserProdEnviroment;
/**
* For production enviroment
*
* @author hieunt29
*
*/
@RunWith(SpringJUnit4ClassRunner.
class)
//ApplicationContext will be loaded from \"UserDevEnviroment.class, UserProdEnviroment.class\"
@ContextConfiguration(classes = { UserDevEnviroment.
class,
UserProdEnviroment.
class })
//PROD environment will be actived
@ActiveProfiles(\
"PROD\")
public class UserBeanProdTest {
@Autowired
User userBean;
@Test
public void testAddUser() {
assertThat(userBean.getUserName(), equalTo(\
"Nguyen Trung Hieu PROD\"));
assertThat(userBean.getSalary(), equalTo(999999999l));
assertThat(userBean.getEmail(), equalTo(\
"PROD-hieunt29@fsoft.com.vn\"));
}
}