A Spring Bean is an object that is managed by the Spring IoC container. It is created, configured, and managed by Spring during the application runtime.
Click to reveal answer
beginner
Name the main phases of the Spring Bean lifecycle.
The main phases are: Instantiation, Property Population (Dependency Injection), Initialization, Usage, and Destruction.
Click to reveal answer
intermediate
What happens during the Initialization phase of a Spring Bean?
During Initialization, Spring calls any custom initialization methods or lifecycle callbacks like @PostConstruct or afterPropertiesSet() to prepare the bean for use.
Click to reveal answer
intermediate
How can you customize the destruction of a Spring Bean?
You can customize destruction by implementing DisposableBean interface, using @PreDestroy annotation, or specifying a destroy-method in the bean configuration.
Click to reveal answer
beginner
What role does the Spring IoC container play in the Bean lifecycle?
The Spring IoC container controls the entire lifecycle of beans: creating, wiring dependencies, initializing, and destroying them when no longer needed.
Click to reveal answer
Which annotation is used to run code after a Spring Bean has been initialized?
A@PostConstruct
B@PreDestroy
C@Autowired
D@Bean
✗ Incorrect
@PostConstruct runs after the bean is fully initialized.
What is the first step in the Spring Bean lifecycle?
AInitialization
BDestruction
CInstantiation
DDependency Injection
✗ Incorrect
Instantiation is when the bean object is created.
Which interface can a Spring Bean implement to customize its destruction logic?
Confusing bean lifecycle with Java program lifecycle
Mixing bean lifecycle with object-oriented concepts
Thinking lifecycle only involves creation and destruction
2. Which annotation is used in Spring to run a method after a bean's properties have been set?
easy
A. @PostConstruct
B. @PreDestroy
C. @Autowired
D. @Bean
Solution
Step 1: Identify annotation for post-initialization
@PostConstruct is the annotation used to mark a method to run after bean properties are set.
Step 2: Eliminate other annotations
@PreDestroy runs before destruction, @Autowired injects dependencies, and @Bean defines a bean method.
Final Answer:
@PostConstruct -> Option A
Quick Check:
@PostConstruct runs after properties set [OK]
Hint: PostConstruct runs after init, PreDestroy before destroy [OK]
Common Mistakes:
Confusing @PreDestroy with @PostConstruct
Using @Autowired for lifecycle methods
Thinking @Bean runs after initialization
3. Given this Spring bean code snippet, what will be printed when the bean is initialized?
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
}
medium
A. Bean destroyed
B. No output
C. Bean initialized
D. Compilation error
Solution
Step 1: Understand @PostConstruct behavior
The method annotated with @PostConstruct runs after the bean is created and properties set, printing "Bean initialized".
Step 2: Check output possibilities
Since the method prints a message on init, output will be "Bean initialized". No errors or destruction messages occur here.
Final Answer:
Bean initialized -> Option C
Quick Check:
@PostConstruct prints "Bean initialized" [OK]
Hint: PostConstruct method runs once after bean creation [OK]
Common Mistakes:
Expecting output on bean destruction instead
Thinking no output occurs without explicit call
Confusing syntax causing compilation errors
4. What is wrong with this Spring bean code if the cleanup() method is never called?
@Component
public class MyBean {
public void cleanup() {
System.out.println("Cleaning up");
}
}
medium
A. cleanup method should be static
B. Missing @PreDestroy annotation on cleanup method
C. Missing @PostConstruct annotation on cleanup method
D. cleanup method must return a value
Solution
Step 1: Identify lifecycle annotation for destruction
The cleanup method should be annotated with @PreDestroy to run before bean destruction.
Step 2: Check other options for correctness
@PostConstruct is for initialization, static or return value are not required for lifecycle methods.
Final Answer:
Missing @PreDestroy annotation on cleanup method -> Option B
Quick Check:
@PreDestroy needed to run cleanup before destroy [OK]
Hint: Use @PreDestroy to run code before bean destruction [OK]
Common Mistakes:
Using @PostConstruct instead of @PreDestroy for cleanup
Making cleanup static unnecessarily
Expecting cleanup to run without annotation
5. You want to ensure a Spring bean runs a method both after initialization and before destruction. Which combination correctly achieves this?
hard
A. Implement InitializingBean interface only
B. Annotate the same method with both @PostConstruct and @PreDestroy
C. Use @Autowired on init method and @PreDestroy on destroy method
D. Annotate one method with @PostConstruct and another with @PreDestroy
Solution
Step 1: Understand lifecycle annotations usage
@PostConstruct runs after initialization, @PreDestroy runs before destruction; they must be on separate methods.
Step 2: Evaluate other options
Annotating the same method with both annotations is invalid. @Autowired is for injection, not lifecycle. Implementing InitializingBean interface only handles post-initialization but not destruction.
Final Answer:
Annotate one method with @PostConstruct and another with @PreDestroy -> Option D
Quick Check:
Separate methods with @PostConstruct and @PreDestroy [OK]
Hint: Use separate methods for init (@PostConstruct) and destroy (@PreDestroy) [OK]
Common Mistakes:
Trying to use one method for both init and destroy
Confusing @Autowired with lifecycle annotations
Using InitializingBean interface only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.