How do I configure Spring using Java configuration?

Spring Java configuration lets you configure your application using Java classes instead of XML.

The main annotations are:

  • @Configuration — marks a class as a Spring configuration class
  • @Bean — declares a Spring bean manually
  • @ComponentScan — tells Spring where to find annotated components
  • @PropertySource — loads external properties
  • @Enable... annotations — enable specific Spring features, such as MVC, transactions, JPA, etc.

1. Create a configuration class

import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
}

@Configuration tells Spring that this class contains bean definitions and application setup.


2. Define beans manually with @Bean

Use @Bean when you want Spring to manage an object that you create yourself.

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

@Configuration
public class AppConfig {

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

Spring will create and manage the MyService instance.

By default, the bean name is the method name: myService.


3. Use component scanning

Instead of defining every bean manually, you can let Spring discover classes annotated with:

  • @Component
  • @Service
  • @Repository
  • @Controller
  • @RestController

Example:

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

@Configuration
@ComponentScan("com.example.app")
public class AppConfig {
}

Then Spring can find beans like this:

import org.springframework.stereotype.Service;

@Service
public class MyService {

    public void doWork() {
        System.out.println("Working...");
    }
}

4. Inject dependencies through constructors

Java configuration works together with dependency injection.

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {

    public String findNameById(Long id) {
        return "Alice";
    }
}
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public String getUserName(Long id) {
        return userRepository.findNameById(id);
    }
}

If both classes are discovered by component scanning, Spring automatically injects UserRepository into UserService.


5. Bootstrapping Spring manually

For a non-Spring Boot application, you can start the Spring container like this:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);

        MyService myService = context.getBean(MyService.class);
        myService.doWork();
    }
}

6. Configure Spring MVC with Java configuration

For Spring MVC, use @EnableWebMvc and implement WebMvcConfigurer.

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan("com.example.app")
public class WebConfig implements WebMvcConfigurer {
}

Example controller:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring MVC";
    }
}

7. Load properties

You can load a properties file with @PropertySource.

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

@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
}

Then inject values with @Value:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppInfo {

    private final String appName;

    public AppInfo(@Value("${app.name}") String appName) {
        this.appName = appName;
    }
}

Example application.properties:

app.name=My Spring App

8. Enable transactions

If you use database transactions, enable them with @EnableTransactionManagement.

import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class PersistenceConfig {
}

Then use @Transactional on services:

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderService {

    @Transactional
    public void placeOrder() {
        // database operations
    }
}

Typical setup

A common Java configuration setup looks like this:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan("com.example.app")
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class AppConfig {
}

For Spring MVC:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan("com.example.app")
public class WebConfig implements WebMvcConfigurer {
}

Summary

To configure Spring using Java configuration:

  1. Create a class annotated with @Configuration.
  2. Add @ComponentScan to discover annotated classes.
  3. Use @Bean methods for manually created beans.
  4. Use constructor injection for dependencies.
  5. Add feature-specific annotations such as @EnableWebMvc, @EnableTransactionManagement, or JPA-related configuration as needed.

In most modern Spring applications, Java configuration plus component scanning replaces XML configuration almost entirely.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.