📖 Scenario: You are building a simple Spring Boot application that manages a greeting message. You want to understand how Spring manages objects called beans to reuse them easily across your app.
🎯 Goal: Create a Spring Boot application that defines a bean for a greeting message, configures it, uses it in a service, and finally injects it into a controller to display the greeting.
📋 What You'll Learn
Create a configuration class with a bean method returning a greeting string
Add a configuration variable to customize the greeting message
Use the bean in a service class to provide the greeting
Inject the service into a controller and return the greeting from an endpoint
💡 Why This Matters
🌍 Real World
Spring beans are the foundation of managing objects in Spring applications. This project shows how to create and use beans to share data and logic easily.
💼 Career
Understanding Spring beans is essential for Java developers working with Spring Boot, as it enables clean, modular, and testable code.
Progress0 / 4 steps
1
Create a configuration class with a bean
Create a class called GreetingConfig annotated with @Configuration. Inside it, write a method called greetingMessage annotated with @Bean that returns the string "Hello, Spring!".
Spring Boot
Hint
Use @Configuration on the class and @Bean on the method that returns the greeting string.
2
Add a configuration variable for the greeting
Inside the GreetingConfig class, add a private final string variable called customGreeting initialized to "Welcome to Spring Boot!". Change the greetingMessage method to return customGreeting instead of the fixed string.
Spring Boot
Hint
Define a private final variable and return it from the bean method.
3
Create a service class that uses the greeting bean
Create a class called GreetingService annotated with @Service. Add a private final string field called message. Use constructor injection with @Autowired to receive the greetingMessage bean and assign it to message. Add a method getGreeting() that returns message.
Spring Boot
Hint
Use constructor injection with @Autowired to get the bean and store it in a field.
4
Inject the service into a controller and return the greeting
Create a class called GreetingController annotated with @RestController. Inject GreetingService using constructor injection. Add a @GetMapping("/greet") method called greet() that returns the result of getGreeting() from the service.
Spring Boot
Hint
Use constructor injection to get the service and return its greeting in the controller method.
Practice
(1/5)
1. What is a Bean in the Spring framework?
easy
A. A type of Java exception
B. A database connection
C. An object managed by the Spring container
D. A user interface component
Solution
Step 1: Understand Spring's role
Spring manages objects to help organize and connect parts of an application.
Step 2: Define what a Bean is
A Bean is any object that Spring creates and manages in its container.
Final Answer:
An object managed by the Spring container -> Option C
Quick Check:
Bean = Managed object [OK]
Hint: Beans are Spring-managed objects, not UI or exceptions [OK]
Common Mistakes:
Confusing beans with database connections
Thinking beans are UI elements
Assuming beans are exceptions
2. Which annotation is used to declare a class as a Spring Bean?
easy
A. @Qualifier
B. @Component
C. @Autowired
D. @RequestMapping
Solution
Step 1: Identify annotations for beans
@Component marks a class as a Spring-managed bean.
Step 2: Differentiate from other annotations
@Qualifier specifies which bean to inject, @Autowired injects beans, @RequestMapping maps web requests.
Final Answer:
@Component -> Option B
Quick Check:
@Component = Bean declaration [OK]
Hint: Use @Component to declare beans; others serve different roles [OK]
Common Mistakes:
Confusing @Autowired with bean declaration
Using @RequestMapping for beans
Thinking @Qualifier declares beans
3. Given the code below, what will be printed (assuming the @Autowired field and System.out.println are inside a Spring-managed class)?
@Component
public class MyBean {
public String greet() { return "Hello Spring"; }
}
@Autowired
MyBean myBean;
System.out.println(myBean.greet());
medium
A. Hello Spring
B. null
C. Compilation error
D. No output
Solution
Step 1: Understand @Component and @Autowired
@Component creates the bean, @Autowired injects it into the field.
Step 2: Check method call output
Calling greet() returns "Hello Spring", so it prints that string.