1. 程式人生 > >Spring動態管理IoC容器中的Bean

Spring動態管理IoC容器中的Bean

以下為工具類,此工具類需要在Spring中註冊。有動態獲取Bean的方法,也有把Bean動態交給IoC容器管理的方法。可直接copy複用:實現介面:

org.springframework.context.ApplicationContextAware

實現該介面的setApplicationContext(ApplicationContext context)方法,並儲存ApplicationContext 物件。Spring初始化時,會通過該方法將ApplicationContext 物件注入。

package com.xx.xx.util;

import java.util.Map;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextHolder implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext;
	
	/**
     * 實現ApplicationContextAware介面的context注入函式, 將其存入靜態變數.
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext =applicationContext;
    }
    
    /**
     * 取得儲存在靜態變數中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }
    
    /**
	 * 從靜態變數ApplicationContext中取得Bean, 自動轉型為所賦值物件的型別.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		checkApplicationContext();
		if (applicationContext.containsBean(name)) {
			return (T) applicationContext.getBean(name);
		}
		return null;
	}

    /**
     * 從靜態變數ApplicationContext中取得Bean, 自動轉型為所賦值物件的型別.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBeansOfType(clazz);
    }

    private static void checkApplicationContext() {
        if (applicationContext == null)
            throw new IllegalStateException("applicaitonContext未注入,請在applicationContext.xml中定義SpringContextUtil");
    }
    
    /**
	 * 同步方法註冊bean到ApplicationContext中
	 * 
	 * @param beanName
	 * @param clazz
	 * @param original bean的屬性值
	 */
	public static synchronized void setBean(String beanName, Class<?> clazz,Map<String,Object> original) {
		checkApplicationContext();
		DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
		if(beanFactory.containsBean(beanName)){
			return;
		}
		//BeanDefinition beanDefinition = new RootBeanDefinition(clazz);
		GenericBeanDefinition definition = new GenericBeanDefinition();
		//類class
		definition.setBeanClass(clazz);
		//屬性賦值
		definition.setPropertyValues(new MutablePropertyValues(original));
		//註冊到spring上下文
		beanFactory.registerBeanDefinition(beanName, definition);
	}
	
	/**
	 * 刪除spring中管理的bean
	 * @param beanName
	 */
	public static void removeBean(String beanName){
		ApplicationContext ctx = SpringContextHolder.getApplicationContext();
		DefaultListableBeanFactory acf = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
		if(acf.containsBean(beanName)) {
		    acf.removeBeanDefinition(beanName);
		}
	}
}