You are on page 1of 6

Bean Lifecycle Methods

Bean Lifecycle

Container Bean Dependencies Internal Spring Your Custom


Started Instantiated Injected Processing Init Method

Bean Is Ready For Use

Container Is Shutdown

Your Custom
Destroy Method

www.luv2code.com
Bean Lifecycle Methods / Hooks
• You can add custom code during bean initialization

• Calling custom business logic methods

• Setting up handles to resources (db, sockets, file etc)


• You can add custom code during bean destruction

• Calling custom business logic method

• Clean up handles to resources (db, sockets, files etc)

www.luv2code.com
Init: method configuration

<beans … >

<bean id="myCoach"
class="com.luv2code.springdemo.TrackCoach"
init-method="doMyStartupStuff">

</bean>

</beans>

www.luv2code.com
Destroy: method configuration
<beans … >

<bean id="myCoach"
class="com.luv2code.springdemo.TrackCoach"
init-method="doMyStartupStuff"
destroy-method="doMyCleanupStuff">

</bean>

</beans>

www.luv2code.com
Development Process

Step-
By-S
tep
1. Define your methods for init and destroy


2. Configure the method names in Spring config file


www.luv2code.com

You might also like