After Spring came out with annotations based Java configuration, I found them very handy. Get rid of the xml Spring configs, as the Java configs are safe with refactoring, more readable and less verbose. I will give some of the examples that I used:

Configuration of Jdbc Connection Pool

@Configuration
public class DaoConfig {
    @Autowired
    private Environment environment;
    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
	BasicDataSource dataSource = new BasicDataSource();
	dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
	dataSource.setUrl(environment.getProperty("jdbc.url"));
	dataSource.setUsername(environment.getProperty("jdbc.username"));
	dataSource.setPassword(environment.getProperty("jdbc.password"));
	dataSource.setMaxActive(100);
	dataSource.setMaxWait(1000);
	dataSource.setPoolPreparedStatements(true);
	dataSource.setDefaultAutoCommit(false);
	dataSource.setValidationQuery(environment.getProperty("jdbc.validationQuery"));
	dataSource.setTestOnBorrow(true);
	return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
	return new JdbcTemplate(dataSource);
    }
}

Configuration of Spring MVC

@Configuration
@EnableWebMvc
@ComponentScan("com.swayam.demo.web.rest")
@PropertySource("classpath:jdbc.properties")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List> converters) {
	Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
	converters.add(new MappingJackson2HttpMessageConverter(jackson2ObjectMapperBuilder.build()));
	Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter = new Jaxb2RootElementHttpMessageConverter();
	converters.add(jaxb2RootElementHttpMessageConverter);
    }
    @Bean
    public ViewResolver viewResolver() {
	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
	viewResolver.setViewClass(JstlView.class);
	viewResolver.setPrefix("/WEB-INF/jsp/");
	viewResolver.setSuffix(".jsp");
	return viewResolver;
    }
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfig() {
	return new PropertySourcesPlaceholderConfigurer();
    }
}

Note the use of WebMvcConfigurerAdapter. It comes in handy when you want to work with Spring MVC. Especially note worthy is the configureMessageConverters() method. You would use that to configure a REST service. It would define how Spring handles the @ResponseBody or @RestController annotation, to translate a POJO to the response type: json, xml, etc. In the above example, we are using MappingJackson2HttpMessageConverter to convert our POJOs to JSON and Jaxb2RootElementHttpMessageConverter to convert them to XML.

Excluding a specific class from the config

Sometimes it so happens that we would like to selectively disable a couple of classes from the annotation config. This is how it is done:

@Configuration
@ComponentScan(basePackages = {"com.swayam.demo.web.xxx",
        "com.swayam.demo.web.yyy"}, excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = ClassToExcludeFromWiring.class) )
public class DaoConfig {
...

 

Sources

The sources can be found here:
https://github.com/paawak/blog/tree/master/code/simple-spring-rest