Preface
Prev |  | Next

## 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:

- JavaConfig provides a truly object-oriented mechanism for dependency injection, meaning you can take full advantage of reuse, inheritance and polymorphism in your configuration code.
- You are given complete control over instantiation and dependency injection, meaning that even the most complex objects can be dealt with gracefully.
- Because only Java is required, you are left with fully refactorable configuration logic that requires no special tooling beyond your IDE.

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.

Prev |  | Next
Spring JavaConfig Reference Guide | Home | 1. Introduction