我所知道的在spring初始化bean,销毁bean之前的操作有三种方式:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口

直接上xml中配置文件:

<bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton"  init-method="init"  destroy-method="cleanUp">
  
   </bean>

@PostConstruct和 @PreDestroy注解在bean中方法名上即可在初始化或销毁bean之前执行。

实现InitializingBean和DisposableBean接口接口,举例如下:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.ecif.admin.app.cfg.KeyValueService;
import com.suning.ecif.admin.entity.KeyValue;

/**
 * 配置管理,增加后台对tb9005的jvm缓存配置
 *
 * @author djw
 * @version 1.0 2015-12-01
 */
@Service
public class DBConfigManager implements InitializingBean {
   
    @Autowired
    KeyValueService keyValueService;

private static Logger log = LoggerFactory.getLogger(DBConfigManager.class);

// Config file properties
    private Map<String, String> theProperties = new HashMap<String, String>();

/**
     * {@inheritDoc}
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        init();
       
    }

public void init() {

try {
            List<KeyValue> queryKeyValues = keyValueService.queryKeyValue(null);
            Map<String, String> map = new HashMap<String, String>();
            for (KeyValue keyValue : queryKeyValues) {
                String key = keyValue.getKey() == null ? null : keyValue.getKey().trim();
                String value = keyValue.getValue() == null ? null : keyValue.getValue().trim();
                map.put(key, value);
            }
            theProperties = map;
        } catch (Exception e) {
            log.error("DBConfigManager.init()", e);
        } finally {

}
    }

/**
     * get the Config Value
     *
     * @param key
     *            Config_key
     * @return Config_value
     */
    public String getConfigValue(String key) {
        return theProperties.get(key);
    }

/**
     * get the Config Value, if not exists then return the default value
     *
     * @param key
     *            Config_key
     * @param defaultValue
     *            default_Value
     * @return Config_value or default_Value
     */
    public String getConfigValue(String key, String defaultValue) {
        if ( theProperties.get(key) == null) {
            return defaultValue;
        }
        return theProperties.get(key);
    }

}

实现InitializingBean接口,实现afterPropertiesSet方法,该方法表明是在资源加载完以后,初始化bean之前执行的方法,同样DisposableBean就是在一个bean被销毁的时候,spring容器会帮你自动执行这个方法;

spring的初始化bean,销毁bean之前的操作详解的更多相关文章

  1. Spring实现初始化和销毁bean之前进行的操作,三种方式

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  2. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  3. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  4. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  5. spring实战三装配bean之Bean的作用域以及初始化和销毁Bean

    1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...

  6. Spring学习笔记--初始化和销毁Bean

    可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...

  7. 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式

    1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...

  8. 三种不同实现初始化和销毁bean之前进行的操作的比较

    Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...

  9. Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...

随机推荐

  1. 错误Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat

    今天算是见识了什么事顺序的重要性. 在使用mybatis时由于联合了其他的表,用到了resultMap,之后外加association这一项.可是在替换对应字段的位置上加上association总是报 ...

  2. Matlab的libsvm的安装

    最关键的是compilers的选择(对于把Microsoft visual stdio 2005或者其他的编译器安装在自定义目录下的这一步非常关键)  以下是步骤:>> mex -setu ...

  3. Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3706   Accepted: ...

  4. Uva 1625,颜色的长度

    类似于LCS的动态规划,指标函数的分解. 题目链接:https://uva.onlinejudge.org/external/16/1625.pdf 题目大意:两个颜色序列,将他们合并,合并的时候,每 ...

  5. Uva 12563,劲歌金曲,01背包

    题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...

  6. Poj(1511),SPFA

    题目链接:http://poj.org/problem?id=1511 嗯,最后一次写SPFA了,以后就套模板了. 题意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返 ...

  7. WPFの单例模式

    版本一: /// <summary>/// A simple singleton class implements./// </summary>public sealed cl ...

  8. IOS文字属性备注

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...

  9. Maven打jar发布包的常用配置

    1.修改pom.xml增加如下内容 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifac ...

  10. Codeoforces 558 B. Duff in Love

    //   B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standar ...