zoukankan      html  css  js  c++  java
  • spring 初始化和销毁的三种方法

    • spring 中初始化和销毁有三种办法

    1、实现接口 DisposableBean ,initilzingBean
    2、使用注解 @PostConstructor, @PreDestory
    3、@Bean(initMethod = "",destroyMethod="")

    package com.shanjiancaofu.ad.service.impl;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AppConfig {
           // initMethod  指定类的初始化方法, destroyMethod 指定类的销毁方法
        @Bean(initMethod = "init", destroyMethod = "destroyMethod")
        User user() {
            return new User();
        }
    }

    User 类

    package com.shanjiancaofu.ad.service.impl;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    
    class User implements InitializingBean, DisposableBean {
        public String userName;
    
        public User() {
            System.out.println("User constructor");
        }
    
        public void init() {
            this.userName = "initName";
            System.out.println(" user init. ....");
        }
    
        public void destroyMethod() {
            this.userName = "destroyName";
            System.out.println(" user destroy. ....");
        }
    
        @PreDestroy
        public void preDestroy() {
            System.out.println("@PreDestroy ...");
        }
    
        @PostConstruct
        public void postConstruct() {
            System.out.println(" @PostConstruct. ....");
        }
    
        @Override
        public String toString() {
            return "user:" + this.userName;
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("InitializingBean afterPropertiesSet");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("DisposableBean destroy");
        }
    }

    启动spring 应用 控制台输出:

    User constructor
    @PostConstruct. ....
    InitializingBean afterPropertiesSet
    user init. ....

    关闭spring 应用 控制台输出:

    @PreDestroy ...
    DisposableBean destroy
    user destroy. ....
    •  获取spring 容器中的bean
    @Service
    @Lazy(false)
    public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
        private static ApplicationContext applicationContext = null;
    
        private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
    
        /**
         * 取得存储在静态变量中的ApplicationContext.
         */
        public static ApplicationContext getApplicationContext() {
            assertContextInjected();
            return applicationContext;
        }
    
        /**
         * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
         */
        @SuppressWarnings("unchecked")
        public static <T> T getBean(String name) {
            assertContextInjected();
            return (T) applicationContext.getBean(name);
        }
    
        /**
         * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
         */
        public static <T> T getBean(Class<T> requiredType) {
            assertContextInjected();
            return applicationContext.getBean(requiredType);
        }
    
        /**
         * 清除SpringContextHolder中的ApplicationContext为Null.
         */
        public static void clearHolder() {
            applicationContext = null;
        }
    
        /**
         * 实现ApplicationContextAware接口, 注入Context到静态变量中.
         */
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) {
            SpringContextHolder.applicationContext = applicationContext;
        }
    
        /**
         * 实现DisposableBean接口, 在Context关闭时清理静态变量.
         */
        @Override
        public void destroy() throws Exception {
            SpringContextHolder.clearHolder();
        }
    
        /**
         * 检查ApplicationContext不为空.
         */
        private static void assertContextInjected() {
            Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
        }
    }
    View Code
    故乡明
  • 相关阅读:
    添加arcgis server js API 在aptana环境中的代码帮助
    VS2010安装报错,提示“ 无法打开数据文件deffactory.dat”
    arctoolbox中出现错误:'Invalid Topology [INCOMPLETE_VOID_POLY]的解决办法
    Fiddler2(转)
    ArcGIS 10.X功能增减(转)
    ubuntu 修改root密码
    MongoDb 入门(1) windows 7安装Mongodb
    Pylons 安装
    ubuntu 配置 nginx+pylons [发布应用]
    win7 配置Python quixote Web Framework
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14148810.html
Copyright © 2011-2022 走看看