`

spring bean 获取 ServletContext

    博客分类:
  • Java
 
阅读更多
我的需求是获取spring开发的web项目在服务器上的绝对路径。

  进而引申到我需要知道servletContext,因为servletContext有一个servletContext.getRealPath("/");方法,这个方法就能获取项目的绝对路径。

  常规方式下我们如何获取servletContext呢?我们需要让我们的类继承HttpServlet类,然后获取servletConfig,通过这 个获取servletContext(servletConfig.getServletContext())。(至于如何获取 servletconfig对象,大家去google,百度找找吧)

  但是我需要在spring的bean中直接获取,这下可和我们常规的操作有些不同,因为spring的bean都是pojo的。根本见不着servletconfig和servletcontext的影子。

  这里我需要指出spring给我们提供了两个接口:org.springframework.web.context.ServletContextAware和

org.springframework.web.context.ServletConfigAware。我们可以让我们的bean实现上边的任何一个接口就能获取到servletContext了 .

代码如下:
    
public class DicBean implements ServletContextAware{ 
    private ServletContext servletContext;  
    public void setServletContext(ServletContext sc) { 
        this.servletContext=sc; 
        System.out.println("项目的绝对路径为:"+servletContext.getRealPath("/")); 
    } 
    }


这样,我们的bean就能够直接获取到servletContext了

如果你想要servletConfig,那方法一样,只是实现的接口不同了。

原理推想:应该是在创建spring的sessionFactory的时候,将应用服务器的相关属性一并加载,查看创建的bean是否实现相关接口,如果实现了,就将相关值赋予bean。

注意点:

  1、这东西是无法用junit进行单元测试的,因为他依赖于应用服务器
分享到:
评论

相关推荐

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...

    spring源代码解析

    简单的说,在web容器中,通过ServletContext为Spring的IOC容器提供宿主环境,对应的建立起一个IOC容器的体系。其中,首先需要建立的是根上下文,这个上下文持有的对象可以有业务对象,数据存取对象,资源,事物管理...

    Spring 2.0 开发参考手册

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    Spring中文帮助文档

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader接口 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    Spring API

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader接口 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    spring chm文档

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    Struts2+Spring3+MyBatis3完整实例

    - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher.xml] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning - JSR-330 'javax.inject....

    SPRING API 2.0.CHM

    BeanDefinition BeanDefinitionBuilder BeanDefinitionDecorator BeanDefinitionDocumentReader BeanDefinitionHolder BeanDefinitionParser BeanDefinitionParserDelegate BeanDefinitionParsingException ...

    在action以外的地方获取dao

    这是在action以外的地方拿ApplicationContext的方法,需要的参数是:ServletContext,在request.getServletContext里能拿到,所以只要有request就能拿到spring配置文件里的bean. 这种方法通常在写组件时用,比如写...

    spring-framework-reference-4.1.2

    Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................

    S2SH整合报错

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed;...

    SpringFilter:Springboot过滤器示例

    弹簧过滤器 ...它是Spring的Spring Context的功能,是一种Bean 因为它是一个Spring容器,所以它可以注入其他bean并具有良好的可用性。 因为可以使用其他bean,所以将实现证书,授权等。 如何使用过滤

    spring-framework-reference4.1.4

    Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    轻量级java web MVC框架

    一个非常简单的MVC框架,实现了类似Spring MVC的基本功能。 1、包括自动扫描绑定映射路径,只要在web.xml中指定扫描包,系统启动后会将请求url绑定到指定的处理方法上。如: 在web.xml中定义如下: <param-name>...

Global site tag (gtag.js) - Google Analytics