Java Tutorial/Spring/XML Bean Lifecycle

Материал из Java эксперт
Перейти к: навигация, поиск

Bean Lifecycle Disposable Bean

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="destructive" class="DestructiveBeanI">
        <property name="filePath" value="/tmp"/>
    </bean>
</beans>





Bean Liftcycle Initializing

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="simple1" class="SoutSimpleBean">
        <property name="person" value="A"/>
        <property name="value" value="my value"/>
    </bean>
</beans>





Custom Initialization Method

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="numberClass"
       class="CustomInitializationMethodNumberClass" 
       init-method="initialize"/>
</beans>





destroy method

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="numberClass"
        class="CustomDestroyMethodNumberClass"
        destroy-method="close"/>
</beans>





Implements Disposable Bean

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="numberClass"
      class="DisposableBeanNumberClass"/>
</beans>





Implements Initializing Bean

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="nameClass"
         class="InitializingBeanNameClass"/>
</beans>