Close

Spring Framework - @Profile Examples

Spring Framework 

@Configuration
@Import(DataConfig.class)
public class AppConfig {

@Bean
@Profile(PROFILE_SWING)
public CustomerUi swingCustomerUi () {
return new SwingCustomerUi();
}

@Bean
@Profile(PROFILE_JAVA_FX)
public CustomerUi javaFxCustomerUi () {
return new JavaFxCustomerUi();
}

@Bean
public CustomerService customerService () {
return new CustomerService();
}

public static void main (String[] args) {

AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment()
.setActiveProfiles(PROFILE_SWING, DataConfig.PROFILE_LOCAL);
context.register(AppConfig.class);
context.refresh();

CustomerUi bean = context.getBean(CustomerUi.class);
bean.showUi();
}

public static final String PROFILE_SWING = "swing";
public static final String PROFILE_JAVA_FX = "javafx";
}




@Configuration
public class AppConfig {

@Bean
@Profile(PROFILE_LOCAL)
public DataService dataServiceLocal() {
return DataServiceLocal.Instance;
}

@Bean
@Profile(PROFILE_REMOTE)
public DataService dataServiceRemote() {
return DataServiceRemote.Instance;
}

public static final String PROFILE_LOCAL = "local";
public static final String PROFILE_REMOTE = "remote";
}
Original Post




@Configuration
@Import(DataConfig.class)
public class AppConfig {

@Bean
@Profile(PROFILE_SWING)
public CustomerUi swingCustomerUi () {
return new SwingCustomerUi();
}

@Bean
@Profile(PROFILE_JAVA_FX)
public CustomerUi javaFxCustomerUi () {
return new JavaFxCustomerUi();
}

@Bean
public CustomerService customerService () {
return new CustomerService();
}

public static void main (String[] args) {

AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment()
.setActiveProfiles(PROFILE_SWING, DataConfig.PROFILE_LOCAL);
context.register(AppConfig.class);
context.refresh();

CustomerUi bean = context.getBean(CustomerUi.class);
bean.showUi();
}

public static final String PROFILE_SWING = "swing";
public static final String PROFILE_JAVA_FX = "javafx";
}




import com.logicbig.example.service.Dao;
import com.logicbig.example.service.InMemoryCustomerDao;
import com.logicbig.example.service.JpaCustomerDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@Configuration
public class DataConfig {

@Bean
@Profile(PROFILE_LOCAL)
public Dao<Customer> inMemoryCustomerDao() {
return new InMemoryCustomerDao();
}

@Bean
@Profile(PROFILE_DEV)
public Dao<Customer> jpaDao() {
return new JpaCustomerDao();

}

@Bean
@Profile(PROFILE_DEV)

public EntityManager devEntityManager() {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("objectdb:customerDev.odb");
return emf.createEntityManager();
}

@Bean
@Profile(PROFILE_PROD)
public EntityManager prodEntityManager(){
//todo: get DataSource from JNDI, prepare and return EntityManager
return null;
}


public static final String PROFILE_LOCAL = "local";
public static final String PROFILE_DEV = "dev";
public static final String PROFILE_PROD = "prod";
}




See Also