獲取ApplicationContext對象的實現 在static代碼塊中調用

在作一個須要從static代碼塊中操做數據庫的方式。若是從新創建一個數據庫鏈接有點low,因此從spring中進行獲取。網上搜索到幾種方式。須要new幾個對象。由於已經有一個了。再new一個。不合理。因此本人使用了以下的方式進行獲取bean。java

因此使用了實現ApplicationContextAware來實現web

@Service
public class SpringContextHolder implements ApplicationContextAware {
//    ServletContextListener
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static <T>T getBean(String beanName , Class<T>clazz) {
        return applicationContext.getBean(beanName , clazz);
    }

}


本人使用的是註解方式。只須要在類上面加上一個@Service 就能夠。不用在xml中進行配置。spring

這樣調用getBean方法就能夠獲取bean對象了。數據庫

具體的在static代碼塊中進行查詢數據。以下。app

有一箇中間類。下面分爲兩段代碼。ide

/**
 * 使用static方法獲取 spring 中bean對象
 *
 * Created by abing on 2015/11/6.
 */
public class CommonBeanUtils {

    //
    public static Object getBean(String name){

        Object object = SpringContextHolder.getBean(name);

        return object;
    }

}
static {
    PlatformLogger.message("============================================");
    TUserService tUserService = (TUserService) CommonBeanUtils.getBean("userService");
    List<TUser> list = tUserService.getUserById("1");
    PlatformLogger.message("============================================");
    for (TUser tUser : list) {
        PlatformLogger.message(tUser.getId() + "  " + tUser.getName() + "  " + tUser.getPassword());
    }
    PlatformLogger.message("============================================");
}


這樣就能夠在static代碼塊中查詢數據庫了。工具


經過在網上搜索,發現還有幾種方式能獲取bean對象。下面列出幾種方式來。測試

方法一:在初始化時保存ApplicationContext對象 
代碼: 
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId"); ui

方法二:經過Spring提供的工具類獲取ApplicationContext對象
代碼:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId")
spa


方法三:繼承自抽象類ApplicationObjectSupport
說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,能夠方便的獲取到ApplicationContext。
Spring初始化時,會經過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

方法四:繼承自抽象類WebApplicationObjectSupport
說明:相似上面方法,調用getWebApplicationContext()獲取WebApplicationContext


方法四:繼承自抽象類WebApplicationObjectSupport
說明:相似上面方法,調用getWebApplicationContext()獲取WebApplicationContext




還有一種方式是beanFactoryAware 

本身測試了一下確實可行。下面貼出代碼。

@Service
public class HolderBeanFactoryAware implements BeanFactoryAware {

    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        HolderBeanFactoryAware.beanFactory = beanFactory;
    }

    public static Object getBean(String beanName) {
        return beanFactory.getBean(beanName);
    }

    public static <T>T getBean(String beanName , Class<T>clazz) {
        return beanFactory.getBean(beanName , clazz);
    }

}

本身操做數據庫的地方

/**
 * 測試BeanFactoryAware來獲取bean對象的例子
 */
public static void testBeanFactoryAware(){
    TUserService tUserService = (TUserService) HolderBeanFactoryAware.getBean("userService");
    List<TUser> list = tUserService.getUserById("1");
    PlatformLogger.message("============================================");
    for (TUser tUser : list) {
        PlatformLogger.message(tUser.getId() + "  " + tUser.getName() + "  " + tUser.getPassword());
    }
    PlatformLogger.message("============================================");
}

 

獲取bean的方式 常見的方式爲上面的五種方式。還有下面一種實現beanFactoryAware的實現方式。

相關文章
相關標籤/搜索