zoukankan      html  css  js  c++  java
  • Spring bean 实现生命周期的三种解决方案

    解决方案一:通过XML配置文件实现:(标签bean的属性init-method和destroy-method)

    beans.xml:

    <beans>
    	<bean id="bean" class="org.spring.tutorial.SimpleBean" init-method="init" destroy-method="destroy" />
    </beans>

    SimpleBean.java:

    package org.spring.tutorial;
    
    public class SimpleBean {
    
    	public SimpleBean() {
    		System.out.println("SimpleBean called");
    	}
    	
    	public void init() {
    		System.out.println("init called");
    	}
    	
    	public void destroy() {
    		System.out.println("destroy called");
    	}
    	
    }
    


    MainApp.java:

    package org.spring.tutorial;
    
    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"); // 加载指定XML文件的定义
    		context.registerShutdownHook();	// 注册关闭钩子
    	}
    }


    运行之:

    SimpleBean called
    init called
    五月 19, 2013 5:59:18 下午 org.springframework.context.support.AbstractApplicationContext doClose
    INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1286c71: startup date [Sun May 19 17:59:18 CST 2013]; root of context hierarchy
    destroy called


    解决方案二:实现Spring的InitializingBean和DisposableBean。

    SimpleBean.java:

    package org.spring.tutorial;
    
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    
    public class SimpleBean implements InitializingBean,DisposableBean {
    
    
    	public SimpleBean() {
    		System.out.println("SimpleBean called");
    	}
    
    
    	@Override
    	public void destroy() throws Exception {
    		System.out.println("destroy called");
    	}
    
    
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		System.out.println("afterPropertiesSet called");
    	}
    	
    
    
    	
    }


    beans.xml:

    <beans>
    
    	<bean id="bean" class="org.spring.tutorial.SimpleBean" />
    	
    </beans>


    运行结果:

    SimpleBean called
    afterPropertiesSet called
    五月 19, 2013 6:03:44 下午 org.springframework.context.support.AbstractApplicationContext doClose
    INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e77ca4: startup date [Sun May 19 18:03:44 CST 2013]; root of context hierarchy
    destroy called


    解决方案三:利用javax.annotation包的注解PostConstruct和PreDestroy。

    SimpleBean.java:

    package org.spring.tutorial;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    public class SimpleBean {
    
    	public SimpleBean() {
    		System.out.println("SimpleBean called");
    	}
    
    	@PreDestroy
    	public void destroy() throws Exception {
    		System.out.println("destroy called");
    	}
    
    	@PostConstruct
    	public void afterPropertiesSet() throws Exception {
    		System.out.println("afterPropertiesSet called");
    	}
    	
    
    	
    }
    


    beans.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:annotation-config />
    	<bean id="bean" class="org.spring.tutorial.SimpleBean" />
    	
    </beans>


    运行结果:

    SimpleBean called
    五月 19, 2013 6:06:39 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@431425: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,bean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
    afterPropertiesSet called
    destroy called


    总结:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的属性init-method,destroy-method)来实现bean的生命周期。可以根据个人的喜好选择不同的方案。




  • 相关阅读:
    jQuery操作字符串
    jQuery判断复选框是否被选中的3种方式
    19使用推模式和拉模式实现电梯超重报警
    18不使用委托实现能自动侦测车距的智能汽车
    动态创建html元素的几种方法
    17委托异步调用方法
    MVC文件上传09-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹,并在其中创建分类子文件夹
    MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹
    MVC文件上传07-使用客户端jQuery-File-Upload插件和服务端Backload组件裁剪上传图片
    MVC文件上传06-使用客户端jQuery-File-Upload插件和服务端Backload组件自定义控制器上传多个文件
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3087394.html
Copyright © 2011-2022 走看看