Configuring Spring without XML

By Max
February 13, 2014

Trying to refresh my knowledge about  the Spring framework, I’ve started to rewrite one of my standalone apps.

Usually the hardest part was the configuration, but as I have discovered, things have changed in the meantime!

So.. Here’s how I have ‘bootstrapped’ my application with just 3 java classes, no XML!

The magic happens due to changes in Servlet 3.0 when @WebServletContextListener annotation was added.

Basically this annotation, along with implementing the ServletContextListener interface provides you with the create/destroy events of the servlet context.

    public class TestServletContextListener implements javax.servlet.ServletContextListener {
        public void contextInitialized(ServletContextEvent sce) {
        public void contextDestroyed(ServletContextEvent sce) {

This is exactly what Spring does with it’s SpringServletContainerInitializer implementation, which looks after classes that implement the WebApplicationInitializer interface to delegate the creation of the context;

Enough with words, let’s see some code:

public class WebAppInitializer implements WebApplicationInitializer {
	public void onStartup(ServletContext servletContext) throws ServletException {
		WebApplicationContext context = getContext();
		servletContext.addListener(new ContextLoaderListener(context));
		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
	private WebApplicationContext getContext() {
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.setConfigLocation(""); //Path for the config classes
		return context;

Basically we first created the Spring context and told him to look in the “net.melikey.config” package for additional configuration and then the dispacher servlet.

And now, that we have our servlet running, let’s configure the application!

public class WebappConfig extends WebMvcConfigurerAdapter {
	public InternalResourceViewResolver setupViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		return resolver;
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

And like that we enabled the WebMvc module in spring, we told Spring to look for annotated components in our app package, and we setup a basic ViewResolver. You may have noticed that i have extended WebMvcConfigurerAdapter, which is actually not mandatory but it provides a simple way to configure the default servlet handling behaviour.

And finally the database config:

public class DatabaseConfig extends WebMvcConfigurerAdapter {
	Environment env;
	//You really need this if your entities reach the view with lazy initialized childs
	public void addInterceptors(InterceptorRegistry registry) {
		OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	// Set up dataSource to be used by Hibernate
	public DataSource getDataSource() {
		BasicDataSource ds = new BasicDataSource();
		ds.setValidationQuery("select 1 from DUAL");
		return ds;
	// Set up JPA and transactionManager
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
		Map<String, Object> opts = emf.getJpaPropertyMap();
		opts.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
		// ... additional vendor configs
		HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
		return emf;
	public PlatformTransactionManager transactionManager() {
		return new JpaTransactionManager(entityManagerFactory().getObject());

This class created the Datasource (we used a property file to fetch the configuration), configured the EntityManagerFactory and the TrasactionManager.

And we are good to go! Nice and simple, the only downside appears to be that the application starts a bit slower than with the xml files.

Happy coding!

Comments: 0

Leave a Reply

Your email address will not be published. Required fields are marked *