package net.petrikainulainen.springdata.jpa.config;
import net.petrikainulainen.springdata.jpa.common.ConstantDateTimeService;
import net.petrikainulainen.springdata.jpa.common.CurrentTimeDateTimeService;
import net.petrikainulainen.springdata.jpa.common.DateTimeService;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ResourceBundleMessageSource;
/**
* @author Petri Kainulainen
*/
@Configuration
@ComponentScan("net.petrikainulainen.springdata.jpa")
@Import({WebMvcContext.class, PersistenceContext.class, SecurityContext.class})
public class ExampleApplicationContext {
private static final String MESSAGE_SOURCE_BASE_NAME = "i18n/messages";
/**
* These static classes are required because it makes it possible to use
* different properties files for every Spring profile.
*
* See: <a href="http://stackoverflow.com/a/14167357/313554">This StackOverflow answer</a> for more details.
*/
@Profile(Profiles.APPLICATION)
@Configuration
@PropertySource("classpath:application.properties")
static class ApplicationProperties {}
@Profile(Profiles.APPLICATION)
@Bean
DateTimeService currentTimeDateTimeService() {
return new CurrentTimeDateTimeService();
}
@Profile(Profiles.INTEGRATION_TEST)
@Configuration
@PropertySource("classpath:integration-test.properties")
static class IntegrationTestProperties {}
@Profile(Profiles.INTEGRATION_TEST)
@Bean
DateTimeService constantDateTimeService() {
return new ConstantDateTimeService();
}
@Bean
MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE_BASE_NAME);
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
@Bean
PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}