Preface

Spring Java Configuration (JavaConfig) provides a pure-Java means of configuring the Spring IoC container. By taking advantage of Java 5.0 language features such as annotations and generics, JavaConfig allows users to express configuration logic and metadata directly in code, alleviating any need for XML.

By relying only on basic Java syntax and language features, JavaConfig offers several distinct advantages:

To get a sense of how to use JavaConfig, let's configure an application consisting of two beans:

@Configuration
public class AppConfig {
    @Bean
    public Service service() {
        return new ServiceImpl(repository());
    }

    @Bean
    public Repository repository() {
        return new JdbcRepository();
    }
}

Bootstrapping this application would then be as simple as the following:

public class AppBootstrap {
    public static void main(String[] args) {
        JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(AppConfig.class);
        Service service = ctx.getBean(Service.class);
        service.doSomething();
    }
}

While this example is a trivial one, JavaConfig can flex to meet the needs of the most complex and sophisticated enterprise applications. This guide will show you how.