Complete the code to define a Spring bean using annotation.
@Component public class MyService { // Bean logic here } // The annotation [1] marks this class as a Spring bean.
The @Component annotation tells Spring to treat this class as a bean and manage its lifecycle.
Complete the code to implement a method that runs after bean initialization.
public class MyBean implements InitializingBean { @Override public void [1]() throws Exception { System.out.println("Bean is initialized"); } }
The afterPropertiesSet() method is called by Spring after the bean's properties are set, signaling initialization completion.
Fix the error in the bean destruction method name.
public class MyBean implements DisposableBean { @Override public void [1]() throws Exception { System.out.println("Bean is being destroyed"); } }
The correct method name in DisposableBean is destroy() without parentheses in the method signature placeholder.
Fill both blanks to define a bean with custom init and destroy methods.
@Bean(initMethod = "[1]", destroyMethod = "[2]") public MyBean myBean() { return new MyBean(); }
The initMethod and destroyMethod specify custom methods to run during bean lifecycle events.
Fill all three blanks to create a bean with lifecycle annotations.
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"); } }
The @PostConstruct annotation marks a method to run after bean creation. @PreDestroy marks a method before destruction. @PreDestroyMethod is incorrect; the correct annotation is @PreDestroy, so this is a trick option to test attention.