In the context of the study, Spring encountered the afterPropertiesSet() and destroy() methods of the InitializingBean and DisposableBean interfaces, respectively. I implemented two of these interfaces in my Class and implemented both of the above methods in it.

When creating a bean, the afterPropertiesSet() initialization method successfully executes. Tell me how to initiate a call to the destroy() method of the DisposableBean interface? I can not understand.

http://www.springframework.org/schema/beans/spring-beans.xsd "default-lazy-init =" true ">

 <bean id="t1000" class="ru.javabegin.training.spring.impls.robot.ModelT1000" scope="prototype"> <constructor-arg value="silver" index="0" type="String"/> <constructor-arg value="2006" index="1" type="int"/> <constructor-arg value="true" index="2" type="boolean"/> <property name="hand" ref="toshibaHand" /> <property name="leg" ref="toshibaLeg"/> <property name="head" ref="toshibaHead"/> </bean> <bean id="t1000Empty" class="ru.javabegin.training.spring.impls.robot.ModelT1000"> <constructor-arg ref="sonyHand"/> <constructor-arg ref="sonyLeg"/> <constructor-arg ref="toshibaHead"/> </bean> <bean id="t1000P" class="ru.javabegin.training.spring.impls.robot.ModelT1000"> <constructor-arg value="silver" index="0" type="String"/> <constructor-arg value="2006" index="1" type="int"/> <constructor-arg value="true" index="2" type="boolean"/> </bean> <bean id="sonyHand" class="ru.javabegin.training.spring.impls.sony.SonyHand" /> <bean id="sonyHead" class="ru.javabegin.training.spring.impls.sony.SonyHead"/> <bean id="sonyLeg" class="ru.javabegin.training.spring.impls.sony.SonyLeg"/> <bean id="toshibaHand" class="ru.javabegin.training.spring.impls.toshiba.ToshibaHand" scope="prototype"/> <bean id="toshibaHead" class="ru.javabegin.training.spring.impls.toshiba.ToshibaHead" /> <bean id="toshibaLeg" class="ru.javabegin.training.spring.impls.toshiba.ToshibaLeg" /> 

Below are the afterPropertiesSet () and destroy () methods in the bin, which implements the initializing Initial and DisposableBean int-sy:

 public void destroy() throws Exception { System.out.println(this + " method Destroy()"); } public void afterPropertiesSet() throws Exception { System.out.println(this + " method Init()"); } 
  • destroy() is called at the end of execution. and if it is necessary in the middle of a program, can it simply be called as a method? - Senior Pomidor
  • The fact of the matter is that it is defined, but not called upon completion of the program. I redefined these two methods — I inserted text into the console into them — the afterPropertiesSet () method initiates printing its text, but destroy () does not. As I understand it, it should also be automatically called when the program ends, as the afterPropertiesSet () method is automatically called when the bean is initialized. - Roman Kulagin
  • came spring.cfg.xml and method destroy () - Senior Pomidor
  • Here, right in the comments, throw text from a file and a method? - Roman Kulagin
  • one
    and how do you complete the program? - Vartlok

1 answer 1

Understood - you need, on the assurance of the program, to force Spring to close the context using the close () method - context.close (). Only in this case the destroy () method is called. But to do this, when creating the context, set the type of the context variable not through the common ApplicationContext interface, but use the concrete implementation of this ClassPathXmlApplicationContext context or ConfigurableApplicationContext context, for example. Only in specific implementations of this interface can the close () method be called.