About Steve Hanson

Steve is a software developer interested in web development and new technologies. He currently works as a Java consultant at Credera in Dallas, TX.

Spring Java Configuration

I have found that a lot of Spring developers I know still do not know about or use Spring Java Configuration (aka JavaConfig). Spring 3.0 introduced this feature which allows Spring to be configured entirely in Java – no more XML!! I really enjoy using JavaConfig because, well… it’s Java! This means it has all the benefits of Java type-safety and IDE support:
 
 
 
 
 
 

  • Typos generate compile-error – no more waiting till load time to find XML typos
  • IDE refactoring tools will automatically update JavaConfig because it is regular Java
  • IDE automatic import feature can be used instead of typing fully-qualified class names in XML
  • IDE drill-down feature
  • IDE auto-complete feature
  • I think you get it, anything that works with Java..

Some of the features above do come built into more advanced IDEs that have Spring support, but it is just nice to not rely on custom IDE Spring support to get these features. The compile-time checking is a big one for me. I refactor my code frequently, and changing field names or moving or renaming a Java file frequently breaks my XML Spring configuration. Of course, I usually don’t realize it until I deploy the application and get a Spring initialization error. Now, my IDE automatically updates the JavaConfig references when I refactor my code, because it’s just regular java. Also, if I forget to use my IDE’s refactor tools, I still get a compile error in the JavaConfig which alerts me before deployment that I have a problem.

Let me show a basic example of how XML configuration translates into JavaConfig. First, XML configuration:

<beans>
  <bean id="PersonService" class="com.codetutr.PersonService">
    <constructor-arg ref="personDao" />
  </bean>

  <bean id="personDao" class="com.codetutr.dao.PersonDao">
    <property name="endpoint" value="localhost" />
  </bean>
</beans>

Translated to JavaConfig:

package com.codetutr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.codetutr.dao.PersonDao;
import com.codetutr.service.PersonService;

@Configuration
public class AppConfig {

    @Bean
    public PersonService personService() {
        return new PersonService(personDao());
    }

    @Bean
    public PersonDao personDao() {
        PersonDao personDao = new PersonDao();
        personDao.setEndpoint("localhost");
        return personDao();
    }

}

It is a pretty-simple one-to-one conversion. Each <beans> document is translated into a Java class annotated with @Configuration. Each <bean> element is translated into a single method annotated with @Bean. The name of the method corresponds to the bean ID. To reference another bean in JavaConfig, you just call the method for the desired bean (see personService above).

One other observation I have had about Spring JavaConfig is that it is more natural to use constructor injection over setter injection – because it results in a one-line method. This is a plus to me, as I usually prefer constructor injection. If a class requires certain dependencies to function properly, I prefer to require them in the constructor. That way, I can ensure that the class is initialized into a valid state. I have noticed that, for whatever reason, constructor injection seems to be rarely used in XML configuration. Maybe constructor-arg is too hard to remember? I don’t know, but I like that JavaConfig seems to be bringing that back some.

Now, to bootstrap the JavaConfig Application Context, Spring has provided a new ApplicationContext implementation: AnnotationConfigApplicationContext. It is used the same as we are used to using ClassPathXmlApplicationContext:

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
PersonService personService = ctx.getBean(PersonService.class);

Or if you’re in a web application:

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.codetutr.config.AppConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Note, that a package name or class-name can be passed in for the contextConfigLocation param above.

Or finally, if you’re using Servlet 3′s Java configuration:

public class SpringWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        final AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
        springContext.register(AppConfig.class);

        final ServletRegistration.Dynamic springServlet = servletContext.addServlet("myServlet", new DispatcherServlet(springContext));
        springServlet.setLoadOnStartup(1);
        springServlet.addMapping("/*");
    }
}

This post just scratched the surface of JavaConfig. I will follow-up with posts on how to import beans from other @Configuration classes or XML files as well as how to use custom namespaces. Also check out my post on how to set up a Spring-MVC application using JavaConfig.
 

Reference: Spring Java Configuration from our JCG partner Steve Hanson at the CodeTutr blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Spring Java Configuration"

  1. Leigh Lin says:

    Excellent post. I am planning to use Java config for my next project.
    One correction: for line ApplicationContext ctx = new AnnotationConfigApplicationContext(PersonService.class);
    Should it be “ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);”

Leave a Reply


× 6 = six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close