Monday, January 6, 2014

Spring Course: V11 Init and Destroy Methods

in general when a bean is created, the constructor will be called then the properties will be set. In spring, you can define an init-method which will be called after the bean constructor is called and the properties are set.

<bean id="person" class="Person" init-method="onCreate">

where onCreate is the method name (sure the name is not important).

in addition, you can define a destroy method, which will be called when the bean is destroyed

<bean id="person" class="Person" init-method="onCreate" destroy-method="onDestroy">

Note: destroy-method will not be called automatically if the bean's scope is prototype, the bean's scope should be singleton (which is the default value) in order to be called.

In addition, you can define the init method and the destroy method to be global for all beans by using default-init-method and default-destroy-method:

No comments:

Post a Comment