springmvc获取上下文ApplicationContext
来源:互联网 发布:淘宝买苹果手机可靠吗 编辑:程序博客网 时间:2024/06/06 20:33
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;@Componentpublic 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上下文配置,在Web.xml里
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>spring.xml里面配置的bean属于Spring上下文。
SpringMVC上下文配置,在Web.xml里
<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>spring-mvc.xml里面配置的bean属于SpringMVC上下文。
注意:SpringMVC上下文和Spring上下文是分开独立,两者是父子关系。Spring 父------SpringMVC 子。但是SpringMVC上下文是可以取得Spring上下文。反之则不行。
在tomcat服务启动日志中可以看到,root WebApplicationContext就是Spring的。
再回头来看,我们的SpringContextUtil,它应该放在Spring里面来设置,才有效。放在SpringMVC里面是没法完成自动实例化的。
阅读全文
0 0
- springmvc获取上下文ApplicationContext
- spring 获取上下文applicationContext
- Spring +quartz获取ApplicationContext上下文
- 获取Spring上下文即ApplicationContext
- springmvc 获取ApplicationContext对象
- 获取Spring上下文ApplicationContext的多种方法
- springMVC 使用WebApplicationContext获取ApplicationContext对象
- 获取sping上下文对象(ApplicationContext)的几种方式
- 获取Spring的上下文环境ApplicationContext的方式
- 在servlet容器启动时获取ApplicationContext上下文环境
- 获取Spring的上下文环境ApplicationContext的方式
- 获取Spring的上下文环境ApplicationContext的方式
- 获取Spring的上下文环境ApplicationContext的方式
- 获取Spring的上下文环境ApplicationContext的方式
- 获取Spring的上下文环境ApplicationContext的方式
- 获取Spring的上下文环境ApplicationContext请注意!!
- 十二种获取Spring的上下文环境ApplicationContext的方法
- 获取Spring的上下文环境ApplicationContext的方式
- OSI模型
- Linux I2C工具查看配置I2C设备
- 使用cordova将Ext JS 6.2的Modern应用程序打包为安卓APP
- 客户端/服务器 http加密方案----对称与非对称加密
- 前端开发面试题
- springmvc获取上下文ApplicationContext
- git初始化-git init使用
- 测试入门
- bzoj 2809: [Apio2012]dispatching
- 小白前端路--html和text的区别
- windows平台下gsoap生成C++客户端源码
- 使用libjpeg-turbo库中的TurboJPEG API实现图片解压,缩放及压缩功能
- hook入门简例
- Java过滤器与SpringMVC拦截器之间的关系与区别