Example usage for org.springframework.beans.factory.config ConfigurableBeanFactory destroyBean

List of usage examples for org.springframework.beans.factory.config ConfigurableBeanFactory destroyBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableBeanFactory destroyBean.

Prototype

void destroyBean(String beanName, Object beanInstance);

Source Link

Document

Destroy the given bean instance (usually a prototype instance obtained from this factory) according to its bean definition.

Usage

From source file:com.kixeye.chassis.support.logging.LoggingConfiguration.java

@PostConstruct
public void initialize() {
    AbstractConfiguration config = ConfigurationManager.getConfigInstance();

    if (config.containsKey(LOGBACK_CONFIG_NAME)) {
        System.out.println("Loading logging config.");

        reloadLogging(config.getString(LOGBACK_CONFIG_NAME));
    }/*w w w  . j  ava 2 s . c o m*/

    config.addConfigurationListener(new ConfigurationListener() {
        @Override
        public synchronized void configurationChanged(ConfigurationEvent event) {
            if ((event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY
                    || event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY)
                    && StringUtils.equalsIgnoreCase(LOGBACK_CONFIG_NAME, event.getPropertyName())
                    && event.getPropertyValue() != null && !event.isBeforeUpdate()) {
                System.out.println("Reloading logging config.");

                reloadLogging((String) event.getPropertyValue());
            }
        }
    });

    ConfigurationListener flumeConfigListener = new ConfigurationListener() {
        private FlumeLoggerLoader loggerLoader = null;

        public synchronized void configurationChanged(ConfigurationEvent event) {
            if (!(event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY
                    || event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY
                    || event.getType() == AbstractConfiguration.EVENT_CLEAR_PROPERTY)) {
                return;
            }

            if (FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY.equals(event.getPropertyName())) {
                if ("true".equals(event.getPropertyValue())) {
                    if (loggerLoader == null) {
                        // construct the bean
                        loggerLoader = (FlumeLoggerLoader) applicationContext
                                .getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                    } // else we already have one so we're cool
                } else {
                    if (loggerLoader != null) {
                        // delete the bean
                        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext
                                .getParentBeanFactory();
                        beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                        loggerLoader = null;
                    } // else we don't have any so we're cool
                }
            } else if (FlumeLoggerLoader.RELOAD_PROPERTIES.contains(event.getPropertyValue())) {
                // only reload if we're already running - otherwise ignore
                if (loggerLoader != null) {
                    // delete the bean
                    ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext
                            .getParentBeanFactory();
                    beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                    loggerLoader = null;

                    // construct the bean
                    loggerLoader = (FlumeLoggerLoader) applicationContext
                            .getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                } // else we don't have any so we're cool
            }
        }
    };

    config.addConfigurationListener(flumeConfigListener);

    flumeConfigListener.configurationChanged(new ConfigurationEvent(this,
            AbstractConfiguration.EVENT_SET_PROPERTY, FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY,
            config.getProperty(FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY), false));
}