zoukankan      html  css  js  c++  java
  • Spring Bean 生命周期

    Bean 的生命周期

    为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和或 destroy-method 参数。

    init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。

    同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

    Bean的生命周期可以表达为:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

    在基于 XML 的配置元数据的情况下, 声明init 和 destroy 方法如下:

      <bean id="helloWorld" 
           class="hello.HelloWorld"
           init-method="init" destroy-method="destroy">
           <property name="message" value="Hello World!"/>
       </bean>
    

    一个示例

    先建一个Spring项目,参考在IDEA中使用Spring写一个HelloWorld

    这是 HelloWorld.java 的文件的内容

    package hello;
    
    public class HelloWorld {
        private String message;
        public void setMessage(String message){
            this.message  = message;
        }
        public void getMessage(){
            System.out.println("Your Message : " + message);
        }
        public void init(){
            System.out.println("Bean is going through init.");
        }
        public void destroy(){
            System.out.println("Bean will destroy now.");
        }
    }
    

    下面是 MainApp.java 文件的内容。

    在这里,你需要注册一个在 AbstractApplicationContext 类中声明的关闭 hook 的 registerShutdownHook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。

    package hello;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class MainApp {
        public static void main(String[] args) {
             AbstractApplicationContext context =
                    new ClassPathXmlApplicationContext("Beans.xml");
            HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
            obj.getMessage();
            context.registerShutdownHook();
        }
    }
    

    下面是 init 和 destroy 方法必需的配置文件 Beans.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="helloWorld" class="hello.HelloWorld" init-method="init" destroy-method="destroy">
            <property name="message" value="你好,Spring!"/>
        </bean>
    
    </beans>
    

    运行MainApp.java,结果如下:

    Bean is going through init.
    Your Message : 你好,Spring!
    Bean will destroy now.
    

    默认的初始化和销毁方法

    如果你有太多具有相同名称的初始化或者销毁方法的 Bean,那么你不需要在每一个 bean 上声明初始化方法和销毁方法。

    框架使用元素中的 default-init-methoddefault-destroy-method 属性提供了灵活地配置这种情况,如下所示:

    <?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"
           default-init-method="init"
           default-destroy-method="destroy">
    
        <bean id="helloWorld" class="hello.HelloWorld" >
            <property name="message" value="你好,Spring!"/>
        </bean>
    
    </beans>
    

    运行结果和前面相同。

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    【HDOJ】2267 How Many People Can Survive
    【HDOJ】2268 How To Use The Car
    【HDOJ】2266 How Many Equations Can You Find
    【POJ】2278 DNA Sequence
    【ZOJ】3430 Detect the Virus
    【HDOJ】2896 病毒侵袭
    求奇数的乘积
    平方和与立方和
    求数列的和
    水仙花数
  • 原文地址:https://www.cnblogs.com/youcoding/p/12742301.html
Copyright © 2011-2022 走看看