Spring - 獲取ApplicationContext的幾種方式

襲冷發表於2018-08-08

一、在Spirng容器初始化時儲存ApplicationContext物件
    1、通過ClassPathXmlApplicationContext載入,預設獲取的是classes即原始碼路徑下的配置檔案

import org.springframework.context.support.ClassPathXmlApplicationContext;
    
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/context-*.xml");

    2、通過FileSystemXmlApplicationContext載入,預設獲取的是專案根路徑下的配置檔案

import org.springframework.context.support.FileSystemXmlApplicationContext;
        
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext*.xml");

    注:該方式適用於採用Spring框架的需要通過配置檔案手工初始化Spring的獨立應用程式
        
二、通過WebApplicationContextUtils獲取ApplicationContext物件
    1、servlet

import javax.servlet.http.HttpServlet;
      
ServletContext servletContext = httpServletRequest.getSession().getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    2、struts2

import org.apache.struts2.ServletActionContext;
    
ServletContext application = ServletActionContext.getRequest().getSession().getServletContext(); 
pplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);

        注、該方式適合於採用Spring框架的B/S系統,通過ServletContext物件獲取ApplicationContext物件
        
三、通過繼承*Support和實現*Aware獲取ApplicationContext物件   

    1、繼承自抽象類ApplicationObjectSupport

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;

public class SpringContextUtils extends ApplicationObjectSupport{
  
    public ApplicationContext getContext(){
        return super.getApplicationContext();
    }
}

    2、繼承自抽象類WebApplicationObjectSupport

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationObjectSupport;

public class SpringContextUtils extends WebApplicationObjectSupport{
    
    public ApplicationContext getContext(){
        return super.getWebApplicationContext();
    }
}

    3、實現介面ApplicationContextAware

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtils implements ApplicationContextAware{
    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

    注、以上繼承和實現方式,需要在Spring的配置檔案中進行配置

<bean id="SpringContextUtils" class="com.xl.utils.SpringContextUtils"/>

四、通過ContextLoader獲取ApplicationContext物件

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();

    注、該方式不依賴於servlet,不需要注入的方式;但在伺服器啟動Spring容器初始化時,不能使用該方法獲取Spring容器

 

 

相關文章