Saturday, 5 April 2025

 

🎯 10 Tricky Spring Boot Bean Creation Interview Questions (With Answers)

Knowing how Spring Boot manages beans is critical for backend interviews. Here's a curated list of tricky but real-world interview questions, each with a sharp, confident answer you can use in your blog or interviews.


✅ 1. How many instances of a @Component bean are created by default in Spring Boot?

✔️ Answer:

Only one instance is created per Spring container. This is due to the default singleton scope in Spring.

Bonus: You can verify this by comparing two injected references or printing the bean hashcode.


✅ 2. How do you create a new object of a bean every time it's needed?

✔️ Answer:

Use the @Scope("prototype") annotation:


@Component @Scope("prototype") public class MyService { ... }

Now Spring will create a new instance every time the bean is requested.


✅ 3. Can you use @Autowired with prototype-scoped beans?

✔️ Answer:

Yes, but be careful!

  • If injected directly via @Autowired, Spring injects only once (at startup).

  • To get a new object every time, use ObjectFactory or Provider:


@Autowired private ObjectFactory<MyPrototypeBean> factory; factory.getObject(); // gives new instance each time

✅ 4. How does Spring Boot know which beans to create?

✔️ Answer:

Spring Boot uses @ComponentScan (inside @SpringBootApplication) to automatically detect classes annotated with:

  • @Component

  • @Service

  • @Repository

  • @Controller


✅ 5. How to find out how many beans Spring has created at startup?

✔️ Answer:

In your main class:


ApplicationContext ctx = SpringApplication.run(MyApp.class, args); System.out.println("Bean count: " + ctx.getBeanDefinitionCount());

✅ 6. Can you have multiple beans of the same type? How does Spring know which one to inject?

✔️ Answer:

Yes, and you can resolve conflicts using:

  • @Primary – Marks a bean as default

  • @Qualifier("beanName") – Specifies the exact bean to inject


✅ 7. What happens if two beans implement the same interface and no qualifier is used?

✔️ Answer:

Spring throws a NoUniqueBeanDefinitionException, because it doesn't know which bean to inject.


✅ 8. What’s the difference between @Bean and @Component?

✔️ Answer:

@Component@Bean
Used on classesUsed on methods inside @Configuration
Automatically discoveredExplicitly declared
Can't customize easilyFine-grained control of object creation

✅ 9. How do you define a lazy-loaded bean?

✔️ Answer:

Use @Lazy:


@Component @Lazy public class MyBean { ... }

Now Spring creates the bean only when it’s first requested, not at startup.


✅ 10. Can Spring Boot manage third-party objects (e.g., not annotated with @Component)?

✔️ Answer:

Yes! Register them using @Bean:


@Configuration public class AppConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }

Spring will manage the lifecycle just like any other bean.


🧠 Bonus Tip: Must-Know Keywords for Interviews

"Spring Boot scans for beans using annotations like @Component. By default, it uses singleton scope, but we can switch to prototype or request. The container handles bean lifecycle and dependency resolution using IoC and DI principles."

No comments:

Post a Comment