spring初始化之service沒有注入報錯問題

場景

  自己創建的類中(new方法創建的),引用了spring的類,但是運行時報錯,該類爲空。

  class dmm

  {

    @Autowired

    private ServiceABC serviceABC;

    使用時,直接serviceABC.XXX()報錯。

  }

原因

  自創建的類中,通過new的方式無法自動注入,只有交給spring託管,由spring創建纔可。

  解決方案:

    使用springcontextutil.getbean()獲取引用的對象,

    如:ServiceABC serviceABC=(ServiceABC)springcontextutil.getbean(ServiceABC.class);

    然後再在使用時serviceABC.XXX()就可以了

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

其他方法(獲取spring中bean的方式總結):

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

  

說明:這種方式適用於採用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。

方法二:通過Spring提供的工具類獲取ApplicationContext對象
    ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
    ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
    ac1.getBean("beanId");
    ac2.getBean("beanId");

  

說明:這種方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然後在通過它獲取需要的類實例。上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,後者返回null。

方法三:繼承自抽象類ApplicationObjectSupport

說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取ApplicationContext。

Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

方法四:繼承自抽象類WebApplicationObjectSupport

說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext

方法五:實現接口ApplicationContextAware

說明:實現該接口的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext對象注入。

以下是實現ApplicationContextAware接口方式的代碼,前面兩種方法類似:

  

    public class SpringContextUtil implements ApplicationContextAware {
    // Spring應用上下文環境
    private static ApplicationContext applicationContext;
    /**
    * 實現ApplicationContextAware接口的回調方法,設置上下文環境
    *
    * @param applicationContext
    */
    public void setApplicationContext(ApplicationContext applicationContext) {
      SpringContextUtil.applicationContext = applicationContext;
    }
    /**
    * @return ApplicationContext
    */
    public static ApplicationContext getApplicationContext() {
      return applicationContext;
    }
    /**
    * 獲取對象
    *
    * @param name
    * @return Object
    * @throws BeansException
    */
    public static Object getBean(String name) throws BeansException {
      return applicationContext.getBean(name);
    }
    }

  

雖然,spring提供的後三種方法可以實現在普通的類中繼承或實現相應的類或接口來獲取spring 的ApplicationContext對象,但是在使用是一定要注意實現了這些類或接口的普通java類一定要在Spring 的配置文件applicationContext.xml文件中進行配置。否則獲取的ApplicationContext對象將爲null。

方法六:通過Spring提供的ContextLoader
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    wac.getBean(beanID);

  

最後提供一種不依賴於servlet,不需要注入的方式。但是需要注意一點,在服務器啓動時,Spring容器初始化時,不能通過以下方法獲取Spring 容器,細節可以查看spring源碼org.springframework.web.context.ContextLoader。

 

 

參考:https://www.ktanx.com/blog/p/326

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章