Chapter 2.5☕ 14 min read

Configuration and Bean

Can't modify third-party code? Use @Bean in a @Configuration class.

01The Concept: Factory Pattern

The Irani Chai Factory Analogy:

Hyderabad's Irani chai isn't sold in packets in the market. It has to be made fresh.

You set up a "Chai Factory" (@Configuration class). Inside is a machine (@Bean method) that takes raw materials and makes a cup of chai (Object), returning it to you.

You don't know the recipe, you just take the chai from the factory. Spring manages this factory. When you need chai, Spring calls this method and gives you the ready-made Bean.

02Technical Explanation
  1. @Configuration: Marks a class as a configuration class. It tells Spring: "Hey, this class contains methods that create objects. Please scan it."
  2. @Bean: Placed on a method inside a @Configuration class. It tells Spring: "Run this method and whatever object it returns, keep it in your Application Context as a Bean."

Use @Bean when you cannot modify the source code of a class (third-party libraries like AWS SDK, RestTemplate) or when complex logic is required to construct the object.

03Full Working Code: Third-Party Beans
package com.devinhyderabad;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private ExternalApiClient apiClient;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {
apiClient.fetchData();
}
}

@Configuration
class AppConfig {
@Bean
public ExternalApiClient externalApiClient() {
ExternalApiClient client = new ExternalApiClient("https://api.devinhyderabad.com");
client.setTimeout(5000);
return client;
}
}
04ExternalApiClient Setup
class ExternalApiClient {
private String url;
private int timeout;

public ExternalApiClient(String url) {
this.url = url;
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}

public void fetchData() {
System.out.println("Fetching data from " + url + " with timeout " + timeout + "ms");
}
}

This simulates configuring a real third-party client like AWS S3, RestTemplate, or ObjectMapper. The @Bean method allows complex setup before returning the object to Spring's context.

05Why It Matters / Interview Note

Interview Question: "What is the difference between @Bean and @Component?"

Answer: @Component marks our own classes as beans via component scanning. @Bean is used inside a @Configuration class to manually create and return an object. We use @Bean when we cannot modify the source code (third-party libraries) or when complex construction logic is required.

Enterprise Note: You will see @Configuration classes for things like SecurityFilterChain, PasswordEncoder, or ObjectMapper. We customize library behavior here and expose the customized object as a @Bean.

Key Takeaways

  • ✅ @Configuration marks a class as a source of bean definitions
  • ✅ @Bean on a method tells Spring to keep the return value in the context
  • ✅ Use @Bean for third-party classes you cannot annotate with @Component
  • ✅ @Bean methods allow complex object setup before returning