|
這篇文章是過于理論的東西,這里有一份能夠直接使用的封裝好的源碼:Spring MVC3.2 通用獲取bean封裝源碼 Bean工廠(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高級IoC的配置機制。BeanFactory使管理不同類型的Java對象成為可能,應用上下文(com.springframework.context.ApplicationContext)建立在BeanFactory基礎之上,提供了更多面向應用的功能,它提供了國際化支持和框架事件體系,更易于創(chuàng)建實際應用。我們一般稱BeanFactory為IoC容器,而稱ApplicationContext為應用上下文。但有時為了行文方便,我們也將ApplicationContext稱為Spring容器。 本文不涉及通過@Resource、@Autowired 自動注入,僅僅通過ApplicationContext獲取Sping配置文件中的Bean。 要獲取XML中配置的Bean,最關鍵的是獲取org.springframework.context.ApplicationContext 第一種獲取ApplicationContext的方法: import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml"); 或者 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 這種方式實例化applicationContext是非常耗時的,僅僅推薦使用在程序需要通過配置文件手工初始化Spring的情況。 第二種獲取ApplicationContext的方法: Spring提供了WebApplicationContextUtils.getWebApplicationContext方法來獲取ApplicationContext對象。 import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext; ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletContext servletContext); //ServletContext servletcontext = request.getSession().getServletContext(); 通過javax.servlet.ServletContext 獲取到ApplicationContext實例對象,這意味著,必須使用到request、session等等。 注意:當使用WebApplicationContextUtils獲取ApplicationContext實例時,需要在web.xml配置文件中添加org.springframework.web.context.ContextLoaderListener監(jiān)聽器,否則獲取不到ApplicationContext對象,返回Null。 配置文件:web.xml <!--ContextLoaderListener自動注入 applicationContext,通過 WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext())獲取 --> <!--Spring配置文件加載位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appContext.xml,/WEB-INF/spring/appInterceptor.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 第三種獲取ApplicationContext的方法: 通過繼承org.springframework.context.support.ApplicationObjectSupport使用getApplicationContext()獲取ApplicationContext實例 第四種獲取WebApplicationContext的方法: 通過繼承org.springframework.web.context.support.WebApplicationObjectSupport使用getWebApplicationContext() 獲取到org.springframework.web.context.WebApplicationContext WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 第五種獲取ApplicationContext的方法: 通過實現(xiàn)org.springframework.context.ApplicationContextAware接口,實現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對象。 Spring初始化時,會通過該方法將ApplicationContext對象注入。 第三、四、五種方法都需要將類配置在Spring配置文件中: <!--假定ApplicationContextTool為繼承或者實現(xiàn)了第三、四、五種方法的具體實現(xiàn)類--> <bean class="com.twovv.utils.ApplicationContextTool"></bean> 否則將獲取不到ApplicationContext,返回Null。 |
|
|