Created
April 8, 2011 12:05
-
-
Save cbeams/909712 to your computer and use it in GitHub Desktop.
Polymorphic @configuration with @Profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration@TxAnnotationDriven | |
public abstract class AppConfig { | |
@Inject Environment env; | |
// ---- configure data infrastructure ---- | |
public abstract DataSource dataSource(); | |
@Configuration | |
@Profile("prod") | |
@PropertySource("classpath:/com/company/app/db.properties") | |
static class ProdConfig extends AppConfig { | |
@Override | |
@Bean | |
public DataSource dataSource() { | |
SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); | |
dataSource.setDriverClass(env.getPropertyAsClass("db.driverClass")); | |
dataSource.setUrl(env.getProperty("db.url")); | |
dataSource.setUsername(env.getProperty("db.username")); | |
dataSource.setPassword(env.getProperty("db.password")); | |
} | |
} | |
@Configuration | |
@Profile("dev") | |
static class DevConfig extends AppConfig { | |
@Override | |
@Bean | |
public DataSource dataSource() { | |
return new EmbeddedDatabaseBuilder() | |
.setType(EmbeddedDatabaseType.HSQL) | |
.addScript("db-schema.sql") | |
.build(); | |
} | |
} | |
@Bean | |
public SessionFactory sessionFactory() { | |
return new AnnotationSessionFactoryBuilder() | |
.setDataSource(dataSource()) | |
.setAnnotatedClasses(com.company.app.domain.Foo.class) | |
.buildSessionFactory(); | |
} | |
@Bean | |
public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor() { | |
return new PersistenceExceptionTranslationPostProcessor(); | |
} | |
@Bean | |
public PersistenceExceptionTranslator exceptionTranslator() { | |
return new HibernateExceptionTranslator(); | |
} | |
@Bean | |
public PlatformTransactionManager txManager() { | |
return new HibernateTransactionManager(sessionFactory()); | |
} | |
// ---- configure service and repository layers ---- | |
// ... | |
// ---- configure MVC controllers and infrastructure ---- | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment