Someone asked for my full Spring 3.1 annotation configuration. I’ve stripped all domain-specific information, but the overall structure is intact.

  • SpringConfig.java – this is the top level class, is empty except for @ComponentScan and @Import statements. The web.xml references this.
  • SpringMvcConfiguration – Any MVC related configuration
  • DatabaseConfiguration
@Configuration
@ComponentScan(basePackageClasses = { SpringConfig.class})
@Import({ SpringMvcConfiguration.class, DatabaseConfiguration.class})
public class SpringConfig {

}
@Configuration
@EnableWebMvc
@Import({ MvcComponents.class,BeanConfiguration.class })
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
	@Qualifier("generalMapper")
	@Autowired ObjectMapper mapper;

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		//Our app has some custom message converters
//		converters.add(...);
	}

	@Bean
	public HttpMessageConverter<Object> jsonConverter() {
		MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
		ObjectMapper m = new ObjectMapper();
		//We have to customize our mapper a bit
		converter.setObjectMapper(m);
		return converter;
	}
}
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackageClasses = { DatabaseConfiguration.class }, excludeFilters = @Filter(Configuration.class))
public class DatabaseConfiguration {
	@Autowired
	private DataSource dataSource;
	@Autowired
	private boolean hibernateShowSql;

	private final String hibernateDialect = "org.hibernate.dialect.SQLServerDialect";//This app uses SQL Server

	@Bean
	public AbstractSessionFactoryBean paymentSessionFactory() {
		AnnotationSessionFactoryBean b = new AnnotationSessionFactoryBean();
		b.setDataSource(dataSource);
		b.setPackagesToScan(new String[] { this.getClass().getPackage().getName() + ".model" });//All our models are in a sub-package "model"
		Properties hiberProps = new Properties();
		hiberProps.setProperty("hibernate.show_sql", Boolean.toString(hibernateShowSql));
		hiberProps.setProperty("hibernate.dialect", hibernateDialect);
		b.setHibernateProperties(hiberProps);
		return b;
	}

	@Bean
	@Autowired
	public AbstractPlatformTransactionManager transactionManager(AbstractSessionFactoryBean bean) {
		HibernateTransactionManager tm = new HibernateTransactionManager();
		tm.setSessionFactory(bean.getObject());
		return tm;
	}

	@Configuration
	@Profile({ "production", "stage", "qa", "test" })
	// For normal use.
	public static class TomcatLinuxSetup {
		@Resource(mappedName = "jdbc/dataSourceNameHere")
		private DataSource dataSource;

		@Bean
		public DataSource dataSource() {
			return dataSource;
		}

		@Bean
		public boolean hibernateShowSql() {
			return false;
		}
	}

	/**
	 * Beans for testing.
	 */
	@Configuration
	@Profile("development")
	// Only unit tests/local app server
	public static class DevelopmentSetup {
		@Bean
		public DataSource dataSource() {
			DriverManagerDataSource delegate = new DriverManagerDataSource();
			delegate.setDriverClassName("...");
			delegate.setUrl("...");
			delegate.setUsername("...");
			delegate.setPassword("...");
			return delegate;
		}

		@Bean
		public boolean hibernateShowSql() {
			return true;
		}
	}
}