Java-Based Configuration in Spring 3.x and Beyond

Prior to the introduction of Spring 3.x, configuration in Spring primarily relied on XML files. With the release of Spring 3.x, JavaConfig was introduced as a method of configuration based on Java code. This approach allows developers to define Spring container configurations and manage Spring beans through Java code, eliminating the need for extensive XML configuration files.

JavaConfig provides a type-safe and more intuitive way of configuring applications, making the process more concise and easier to manage with version control and refactoring. The core of JavaConfig is the @Configuration annotation, which indicates that a class can be used as a configuration class, similar to an XML configuration file. Within a configuration clas, bean creation logic can be defined, and methods can be marked with the @Bean annotation, which returns a bean instance and automatically registers it with the Spring container.

Example

Consider a simple service MyService and its implementation MyServiceImpl, along with a consumer MyApp that depends on MyService. We will demonstrate how to configure these classes using JavaConfig.

First, we define the service interface and implementation:

public interface MyService {
    String serve();
}

public class MyServiceImpl implements MyService {
    @Override
    public String serve() {
        return "Service is serving!";
    }
}

Next, we define the consumer class:

public class MyApp {
    private MyService myService;

    @Autowired
    public MyApp(MyService myService) {
        this.myService = myService;
    }

    public void doSomething() {
        System.out.println(myService.serve());
    }
}

Then, we create a configuration class AppConfig:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public MyApp myApp() {
        return new MyApp(myService());
    }
}

In the AppConfig configuration class above, two methods are defined, both annotated with @Bean. By default, the method names (myService and myApp) become the bean IDs, and the return type determines the bean type. The Spring container calls these methods and manages the returned object instances.

Finally, we can start a Spring application context using JavaConfig in the main entry point of the application:

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        // Create a Spring application context based on JavaConfig
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        
        // Retrieve the MyApp bean and invoke its method
        MyApp myApp = context.getBean(MyApp.class);
        myApp.doSomething();
        
        // Close the context
        context.close();
    }
}

Using JavaConfig to configure a Spring application is clear and type-safe, making the configuration closer to actual coding practices and easier to maintain and refactor.

Tags: Spring JavaConfig Dependency Injection configuration

Posted on Fri, 08 May 2026 19:54:55 +0000 by EdN