The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

klancaster (1) [Avatar] Offline
#1
Hi,

I am learning spring and picked up the MEAP Spring in Action 3ed. THANK YOU for an easy to understand book. I have a question regarding the destroy-method not being called in chapter 2 example.

I pieced together the following implementation based on your narrative in the book (Snippets for brevity)

-------------------------------------
xmlConfig - applicationContext-IdolCompetition.xml
-------------------------------------
...

<bean id="saxophone" class="com.springinaction.chapter02.springidol.Saxophone" scope="prototype"/>

<bean id="kenny" >
class="com.springinaction.chapter02.springidol.Instrumentalist"
init-method="tuneInstrument" destroy-method="cleanInstrument">
<property name="name" value="KennyG" />
<property name="song" value="Jingle Bells" />
<property name="instrument" ref="saxophone" />
</bean>

-------------------------------------
Main
-------------------------------------
.....
String configFile = "com/springinaction/chapter02/springidol/applicationContext-IdolCompetition.xml";

ApplicationContext ctx = new ClassPathXmlApplicationContext(configFile);

Performer performer = (Performer) ctx.getBean("kenny");
try {
performer.perform();
}
catch (PerformanceException e) {
e.printStackTrace();
}
....

-------------------------------------
Instrumentalist
-------------------------------------
.....
public void tuneInstrument() {
instrument.tune();
}

public void cleanInstrument() {
instrument.clean();
}

.....


-------------------------------------
Saxophone (implements Instruments)
-------------------------------------
@Override
public void clean() {
System.out.println("sax has been cleaned");
}

@Override
public void tune() {
System.out.println("sax has been tuned");
}

// NOTE the clean and tune methods ARE declared in the Instrument interface.


When i run main the Instrumentalist.tuneInstrument() method does in fact fire and I see the corresponding output. However the Instrumentatlist.cleanInstrument() never seems to fire as I do not see the expected output "sax has been tuned".

Could you elaborate on why this is. I read in some old spring references that spring cannot call lifecycle methods beyond init on prototype scoped beans. But the kenny bean is not prototype. The contained saxophone is of course prototype. but I feel that should not matter.
kurinosuke (156) [Avatar] Offline
#2
Re: destroy-method not being called in chapter 2 example
I think that it won't work because the application ends without the ApplicationContext knowing it. To let the ApplicationContext know that the application is shutting down, you have to register a shutdown hook on it. Check the sample code from the reference documentation, "3.6.1.6 Shutting down the Spring IoC container gracefully in non-web applications" : http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-shutdown