0
0
Spring Bootframework~10 mins

Bean lifecycle overview in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Spring bean using annotation.

Spring Boot
@Component
public class MyService {
    // Bean logic here
}

// The annotation [1] marks this class as a Spring bean.
Drag options to blanks, or click blank then click option'
A@Repository
B@Service
C@Component
D@Controller
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Repository when the question asks for a generic bean annotation.
Forgetting to add any annotation.
2fill in blank
medium

Complete the code to implement a method that runs after bean initialization.

Spring Boot
public class MyBean implements InitializingBean {
    @Override
    public void [1]() throws Exception {
        System.out.println("Bean is initialized");
    }
}
Drag options to blanks, or click blank then click option'
AafterPropertiesSet
Bstart
Cinit
DpostConstruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PostConstruct annotation method name instead of interface method.
Using a method name not defined in InitializingBean.
3fill in blank
hard

Fix the error in the bean destruction method name.

Spring Boot
public class MyBean implements DisposableBean {
    @Override
    public void [1]() throws Exception {
        System.out.println("Bean is being destroyed");
    }
}
Drag options to blanks, or click blank then click option'
Adestroy
Bdestroyed
Cdestroy()
DdestroyBean
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses in the method name placeholder.
Using incorrect method names like destroyBean or destroyed.
4fill in blank
hard

Fill both blanks to define a bean with custom init and destroy methods.

Spring Boot
@Bean(initMethod = "[1]", destroyMethod = "[2]")
public MyBean myBean() {
    return new MyBean();
}
Drag options to blanks, or click blank then click option'
AcustomInit
BcustomDestroy
Cinit
Ddestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using default method names instead of custom ones.
Mixing up init and destroy method names.
5fill in blank
hard

Fill all three blanks to create a bean with lifecycle annotations.

Spring Boot
public class MyBean {

    @[1]
    public void start() {
        System.out.println("Bean started");
    }

    @[2]
    public void stop() {
        System.out.println("Bean stopped");
    }

    @[3]
    public void cleanup() {
        System.out.println("Bean cleaned up");
    }
}
Drag options to blanks, or click blank then click option'
APostConstruct
BPreDestroy
CPreDestroyMethod
DPostDestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent annotations like @PreDestroyMethod or @PostDestroy.
Mixing up PostConstruct and PreDestroy.