Created
April 8, 2011 11:42
-
-
Save cbeams/909683 to your computer and use it in GitHub Desktop.
100% @Configuration-based application (no component-scanning)
This file contains 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 | |
@PropertySource("classpath:/com/company/app/db.properties") | |
public class UnifiedConfig { | |
@Inject Environment env; | |
// ---- configure data infrastructure ---- | |
@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")); | |
} | |
@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 ---- | |
@Bean | |
public FooService fooService() { | |
/* Note that FooServiceImpl is @Transactional */ | |
return new FooServiceImpl(fooRepository()); | |
} | |
@Bean | |
public FooRepository fooRepository() { | |
return new HibernateFooRepository(sessionFactory()); | |
} | |
// ---- configure MVC controllers and infrastructure ---- | |
@Bean | |
public FooController fooController() { | |
return new FooController(fooService()); | |
} | |
@Bean | |
public FormattingConversionService conversionService() { | |
return new DefaultFormattingConversionService(); | |
} | |
@Bean | |
public Validator validator() { | |
return new LocalValidatorFactoryBean(); | |
} | |
@Bean MessageCodesResolver messageCodesResolver() { | |
return new TestMessageCodesResolver(); | |
} | |
@Feature | |
public MvcAnnotationDriven annotationDriven() { | |
return new MvcAnnotationDriven() | |
.conversionService(conversionService()) | |
.messageCodesResolver(messageCodesResolver()) | |
.validator(validator()) | |
.messageConverters(new StringHttpMessageConverter()) | |
.argumentResolvers(new TestWebArgumentResolver()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment