<value>Clarence Ho</value>
</property>
<property name="age">
<value>100</value>
</property>
</bean>
<bean id="simpleBean2"
class="com.apress.prospring3.ch5.lifecycle.SimpleBeanWithInterface">
<property name="age">
<value>100</value>
</property>
</bean>
<bean id="simpleBean3"
class="com.apress.prospring3.ch5.lifecycle.SimpleBeanWithInterface">
<property name="name">
<value>Clarence Ho</value>
</property>
</bean>
</beans>
Again, there's not much difference between the configuration code in Listing 5-4 and the
configuration code from Listing 5-2. The noticeable difference is the omission of the init-method
attribute. Because the SimpleBeanWithInterface class implements the InitializingBean interface,
Spring knows which method to call as the initialization callback, thus removing the need for any
additional configuration. The output from this example is shown here:
Initializing bean
Name: Clarence Ho
Age: 100
Initializing bean
Using default name
Name: Luke Skywalker
Age: 100
Initializing bean
An error occured in bean configuration: Error creating bean with name 'simpleBean3' defined in
class path resource [lifecycle/initInterface.xml]: Invocation of init method failed; nested
exception is java.lang.IllegalArgumentException: You must set the age property of any beans of
type class com.apress.prospring3.ch5.lifecycle.SimpleBean
As you can see, the output is the same as the method mechanism.
Using JSR-250 @PostConstruct Annotation
Another method that can achieve the same purpose is to use the JSR-250 life-cycle annotation,
@PostConstruct. Starting from Spring 2.5, Spring also supports JSR-250 annotations to specify the
method that Spring should call if the corresponding annotation relating to the bean's life cycle exists in
the class. Listing 5-5 shows the program with the @PostConstruct annotation applied.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home