spring getbean 方法分析

在最近的項目中,有個地方咱們不得不實用getBean的方法,本身從Spring context中獲取bean進行數據庫操做。java

方法一(效率低,極易出現bug,不推薦使用):spring

剛剛開始的時候,咱們使用這中方式,可是在應用過程當中發現此方式效率低下,並且極易出現bug。 在咱們系統中會生成ehcache_auto_created_時間戳文件夾,數據庫

<!-- lang: java -->
String[] xmlCfg = new String[] {"classpath:/spring/applicationContext-service.xml",
			"classpath:/spring/applicationContext-util.xml",
			"classpath:/spring/applicationContext.xml"}; 
ApplicationContext context  = new FileSystemXmlApplicationContext(xmlCfg);
// 獲取inspectionUtil bean
inspectionUtil = (InspectionUtil) context.getBean("inspectionUtil");

因此我google了一下,改用其餘方法。app

方法二(效率高,靈活性高,可複用,推薦使用): 建立一個工具類SpringContextsUtil ,經過實現Spring中的ApplicationContextAware接口,在applicationContext.xml中注入bean後Spring會自動調用setApplicationContext方法。此時咱們就能夠獲取到Spring context。工具

<!-- lang: java -->
public class SpringContextsUtil implements ApplicationContextAware{

private static ApplicationContext applicationContext;    //Spring應用上下文環境   
  /**
  * 實現ApplicationContextAware接口的回調方法,設置上下文環境  
  * @param applicationContext
  * @throws BeansException
  */
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContextsUtil.applicationContext = applicationContext;
  }
 
  /**
  * @return ApplicationContext
  */
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
 
  /**
  * 獲取對象  
  * @param name
  * @return Object 一個以所給名字註冊的bean的實例
  * @throws BeansException
  */
  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }
 
  /**
  * 獲取類型爲requiredType的對象
  * 若是bean不能被類型轉換,相應的異常將會被拋出(BeanNotOfRequiredTypeException)
  * @param name       bean註冊名
  * @param requiredType 返回對象類型
  * @return Object 返回requiredType類型對象
  * @throws BeansException
  */
  public static Object getBean(String name, Class requiredType) throws BeansException {
    return applicationContext.getBean(name, requiredType);
  }
 
  /**
  * 若是BeanFactory包含一個與所給名稱匹配的bean定義,則返回true
  * @param name
  * @return boolean
  */
  public static boolean containsBean(String name) {
    return applicationContext.containsBean(name);
  }
 
  /**
  * 判斷以給定名字註冊的bean定義是一個singleton仍是一個prototype。
  * 若是與給定名字相應的bean定義沒有被找到,將會拋出一個異常(NoSuchBeanDefinitionException)  
  * @param name
  * @return boolean
  * @throws NoSuchBeanDefinitionException
  */
  public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(name);
  }
 
  /**
  * @param name
  * @return Class 註冊對象的類型
  * @throws NoSuchBeanDefinitionException
  */
  public static Class getType(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getType(name);
  }
 
  /**
  * 若是給定的bean名字在bean定義中有別名,則返回這些別名  
  * @param name
  * @return
  * @throws NoSuchBeanDefinitionException
  */
  public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getAliases(name);
  }
}

調用方法:ui

<!-- lang: java -->
// 獲取inspectionUtil bean
inspectionUtil = (InspectionUtil) SpringContextUtil.getBean("inspectionUtil");

注:google

一、使用時會出現沒法獲取applicationContext,並拋出NullPointerException。 緣由:使用此方法必須在spring applicationContext.xml中注入bean。不然spring沒法自動調用setApplicationContext。以下spa

<!-- lang: xml -->
<bean id="springContextsUtil" class="com.sinosoft.sepmis.util.SpringContextsUtil" ></bean>

二、若是注入後仍然出現這個問題。 則修改<beans default-autowire="byName" default-lazy-init="true">中的default-lazy-init="false"。 或者是修改bean注入屬性prototype

<!-- lang: xml -->
<bean id="springContextsUtil" class="com.sinosoft.sepmis.util.SpringContextsUtil" lazy-init="false"></bean>
相關文章
相關標籤/搜索