Junior — Middle
What methods can be used to configure the execution of a method after creating a Bean in Spring Framework?
sobes.tech AI
Answer from AI
In the Spring Framework, there are several ways to execute a method after creating and initializing a Bean:
- Implementing the
InitializingBeaninterface:
public class MyBean implements InitializingBean {
@Override
public void afterPropertiesSet() {
// code to execute after Bean creation
}
}
- Using the
@PostConstructannotation:
@Component
public class MyBean {
@PostConstruct
public void init() {
// initialization code
}
}
- Defining a method with the
init-methodattribute in XML configuration:
<bean id="myBean" class="com.example.MyBean" init-method="init"/>
- Using
BeanPostProcessorfor more complex logic after Bean creation.
These methods allow for additional initialization or configuration after Spring has created and configured the Bean.