1、可通过下面工具类获取
package org.mvc.demo.utlis;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;//spring上下文
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static T getBean(String name) throws BeansException{
return (T)applicationContext.getBean(name);
}
}
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
另外要注意Spring上下文和SpringMVC上下文的区别。
Spring上下文配置
contextConfigLocation
classpath:spring-application.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC上下文配置
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
注意:SpringMVC上下文和Spring上下文是分开独立,两者是父子关系。Spring 父------SpringMVC 子。但是SpringMVC上下文是可以取得Spring上下文。反之则不行。
在tomcat服务启动日志中可以看到,root WebApplicationContext就是Spring的。
再回头来看,我们的SpringContextUtil,它应该放在Spring里面来设置,才有效。放在SpringMVC里面是没法完成自动实例化的。
那么,我们能仿SpringContextUtil建立springMVC的工具类吗?答案是可以。
在做这个之前,我们要先理解springMVC的加载顺序.
contextConfigLocation
classpath*:spring-application.xml
log4jRefreshInterval
600000
webAppRootKey
webPath
org.springframework.web.util.WebAppRootListener
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
dispatcher
/
上面贴出的就是web.xml的部分配置,在这里我们首先讲解下web项目启动的加载顺序:
以Tomcat举例,启动Tomcat之后,首先会加载web.xml文件:
a)容器首先读取web.xml中的
b)紧接着实例化ServletContext对象,并将
c)创建
d)调用listener的contextInitialized(ServletContextEvent args)方法,ServletContext= ServletContextEvent.getServletContext();
此时你可以通过ServletContext获取context-param配置的内容并可以加以修改,此时Tomcat还没完全启动完成。
e)后续加载配置的各类filter;
f)最后加载servlet;
最后的结论是:web.xml中配置项的加载顺序是context-param=>listener=>filter=>servlet,配置项的顺序并不会改变加载顺序,但是同类型的配置项会应该加载顺序,servlet中也可以通过load-on-startup来指定加载顺序。
明白加载顺序之后,我们就可以写一个springMVC上下文的工具类了。
前面说过:这里加载的是spring上下文
contextConfigLocation
classpath*:spring-application.xml
这里加载的才是springMVC上下文
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
这个时候我们看一下spring-mvc.xml这个文件。
text/html;charset=UTF-8
application/json
QuoteFieldNames
WriteDateUseDateFormat
DisableCircularReferenceDetect
no
black
5
主要看这里
我们知道,像Controller都是单独扫描并实例化各个Controller,所以我们要在
com.fwone.controller这个包下写我们的springMVC上下文工具类
到了这里,基本都理解清楚了。直接复制之前的spring上下文工具类
package com.fwone.controller;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
@Controller
public class SpringMvcContext implements ApplicationContextAware {
private static ApplicationContext applicationContext;//springMVC上下文定义
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringMvcContext.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext(){
return SpringMvcContext.applicationContext;
}
public static T getBean(String name)throws BeansException{
return (T)applicationContext.getBean(name);
}
}