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> T getBean(String name) throws BeansException{
return (T)applicationContext.getBean(name);
}
}
主要是实现ApplicationContextAware接口
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
另外要注意Spring上下文和SpringMVC上下文的区别。
Spring上下文配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-application.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
SpringMVC上下文配置
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
注意:SpringMVC上下文和Spring上下文是分开独立,两者是父子关系。Spring 父------SpringMVC 子。但是SpringMVC上下文是可以取得Spring上下文。反之则不行。
在tomcat服务启动日志中可以看到,root WebApplicationContext就是Spring的。
再回头来看,我们的SpringContextUtil,它应该放在Spring里面来设置,才有效。放在SpringMVC里面是没法完成自动实例化的。
那么,我们能仿SpringContextUtil建立springMVC的工具类吗?答案是可以。
在做这个之前,我们要先理解springMVC的加载顺序.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="false">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-application.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>600000</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webPath</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
上面贴出的就是web.xml的部分配置,在这里我们首先讲解下web项目启动的加载顺序:
以Tomcat举例,启动Tomcat之后,首先会加载web.xml文件:
a)容器首先读取web.xml中的<context-param>的配置内容和<listener>标签中配置项;
b)紧接着实例化ServletContext对象,并将<context-param>配置的内容转化为键值传递给ServletContext;
c)创建<listener>配置的监听器的类实例,并且启动监听;
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上下文
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-application.xml
</param-value>
</context-param>
这里加载的才是springMVC上下文
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
这个时候我们看一下spring-mvc.xml这个文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.fwone.controller"></context:component-scan>
<mvc:default-servlet-handler />
<!--mvc:annotation-driven会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,
这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="serializerFeatures">
<list>
<value>QuoteFieldNames</value>
<value>WriteDateUseDateFormat</value>
<!-- 禁用fastjson循环引用检测 -->
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- FreeMarker视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="true"/>
<property name="suffix" value=".html"/>
<property name="prefix" value=""/>
<property name="requestContextAttribute" value="rc" />
<property name="order" value="0"/>
</bean>
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/page/"/>
</bean>
<!-- Kaptcha验证码生成器 -->
<bean name="producer" class="com.google.code.kaptcha.impl.DefaultKaptcha" scope="singleton">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">no</prop>
<prop key="kaptcha.textproducer.font.color">black</prop>
<prop key="kaptcha.textproducer.char.space">5</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
主要看这里
<context:component-scan base-package="com.fwone.controller"></context:component-scan>
我们知道,像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> T getBean(String name)throws BeansException{
return (T)applicationContext.getBean(name);
}
}