`
zeng7960983
  • 浏览: 43955 次
  • 性别: Icon_minigender_1
  • 来自: 邵阳
社区版块
存档分类
最新评论

serlvet 获得spring的上下文

    博客分类:
  • java
阅读更多
在servlet或者filter或者Listener中使用spring的IOC容器的方法是:

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:

WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

事实上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代码实现的,建议使用上面上面的静态方法


注意:在使用webApplicationContext.getBean("ServiceName")的时候,前面强制转化要使用接口,如果使用实现类会报类型转换错误。如:
LUserService userService = (LUserService) webApplicationContext.getBean("userService");
分享到:
评论

相关推荐

    servlet与spring整合例子

    然后在Web应用的`web.xml`中,通过`ContextLoaderListener`配置Spring上下文,使得在应用启动时Spring容器被初始化。 2. **Servlet与Spring的交互** 传统的Servlet在接收到请求后,会手动创建或查找需要的服务对象...

    springmvc spring 两套上下文问题

    接下来,Spring MVC中的DispatcherServlet创建了自己的WebApplicationContext,这个上下文是特定于Servlet的。它继承自ApplicationContext,但主要处理与Web相关的Bean,如控制器(Controller)、视图解析器、本地化...

    Servlet JSP Spring MVC初学指南(alin书签).pdf

    3. **Spring MVC入门**:理解DispatcherServlet的作用,配置Spring MVC的上下文和组件扫描,以及配置视图解析器。 4. **Model-View-Controller**:了解MVC模式的工作原理,如何创建模型、控制器和视图。 5. **请求...

    Spring的注入在Servlet中使用

    Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文

    Spring 管理filter 和servlet

    在`web.xml`中,使用`ContextLoaderListener`来初始化Spring的WebApplicationContext,确保在Filter执行之前Spring的上下文已经被加载。配置如下: ```xml <param-name>contextConfigLocation <param-value>/...

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    这段代码指定了一个Listener类`ContextLoaderListener`,该类由Spring框架提供,用于在应用程序启动时加载Spring的上下文配置。 #### Filter Filter是在请求到达目标资源之前可以执行预处理操作的对象,并且在响应...

    spring+servlet 入门hello_world

    6. **启动配置**:在运行项目时,你需要确保Tomcat服务器配置正确,Spring的上下文加载正常,以及Servlet被正确地部署和初始化。 通过这个简单的"spring+servlet 入门hello_world"实例,你将学习到如何设置和运行一...

    Spring-Context:Spring上下文上下文

    在Spring-Context中,你可以理解为Spring上下文就是应用程序的运行环境,它负责管理和装配Bean,同时提供了事件发布、国际化等功能。让我们深入探讨一下Spring上下文的相关知识点。 1. **Bean的定义与管理** - **...

    Spring Security 3.1 +Spring +Servlet+JdbcTemplate

    执行这个SQL脚本将初始化数据库,为应用提供安全上下文。 `说明.txt`文件可能包含了项目的一些介绍、安装步骤或者配置说明,这对于理解和运行项目至关重要。建议仔细阅读此文件,以了解如何设置环境和启动应用程序...

    在Servlet直接获取Spring框架中的Bean.docx

    在Web应用中,Spring通常会创建一个WebApplicationContext,它与Servlet上下文(ServletContext)关联。 **方法一:使用WebApplicationContextUtils** 在Servlet中,我们可以利用`WebApplicationContextUtils`工具...

    spring+freemark整合工程

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <prop key="defaultEncoding">UTF-8 <prop key="number_format">0.### ...

    SSH(Servlet+Spring+Hibernate)整合所需jar包

    - `spring-context.jar`:提供上下文支持,包括AOP、事件、国际化等。 - `spring-web.jar` 和 `spring-webmvc.jar`:支持Web应用程序,特别是MVC模式。 - `spring-aop.jar`:支持面向切面编程。 - `spring-orm.jar`...

    sftp直接以url模式读取-----------包括servlet如何借用springproperties取文件

    在处理SFTP连接时,我们可以将服务器地址、端口、用户名和密码等信息写入到如`application.properties`或`application.yml`文件中,然后在Spring应用上下文中通过`@Value`注解或者`@ConfigurationProperties`来注入...

    Spring Boot实现异步请求(Servlet 3.0)

    使用原生 Servlet 方式时,需要在 Servlet 中添加 asyncSupported = true 属性,并使用 AsyncContext 对象来获取异步请求的上下文对象。然后,可以使用 setTimeout 方法来设定最大超时时间,并使用 response....

    java web整合开发王者归来光盘代码-第七章 servlet章节 (JSP+Servlet+Struts+Hibernate+Spring)

    监听器是Java Web中的特殊组件,可以监听特定的事件,如会话创建、销毁或上下文初始化等,从而实现自动化管理和监控。 综合以上技术,这个章节可能讲解了如何将这些组件集成在一起,创建一个完整的Java Web应用。...

    spring-boot-04-servlet.rar

    例如,可以设置服务器端口、上下文路径、错误页面等。 4. **注册Servlet和Filter**: 在Spring Boot中,可以使用`@WebServlet`和`@WebFilter`注解来注册Servlet和Filter。此外,还可以通过`...

    特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象

    1. **Servlet**: 可以在Servlet的init()方法中,通过ApplicationContextAware接口获取Spring上下文,然后手动从上下文中获取依赖的bean。 2. **Filter**: 同样,可以在Filter的init()方法中获取ApplicationContext...

    SpringMVC中的RootApplicationContext上下文和WebApplicationContext上下文,通过注解配置SpringMVC的完整解决方案

    注解配置SpringMVC原理简述1. 准备知识1.1 两个应用上下文1.2 ServletContext配置方法(Configuration Methods)1.3 运行时插拔1.4 SpringServletContainerInitializer1.4.1 AbstractContextLoaderInitializer1.4.2 ...

    Spring整合Struts

    这种方式允许你集中管理所有的Spring上下文文件,对于使用如StrutsTestCase这样的测试工具尤其有用。因为StrutsTestCase的MockStrutsTestCase不会在启动时初始化监听器,所以将所有上下文文件放置在插件中是一种变通...

    spring mvc jar包

    1. `org.springframework.context-3.0.2.RELEASE.jar`:这个库提供了上下文支持,包括应用上下文(ApplicationContext)和扩展上下文(ExtendedContext)。ApplicationContext 是 Spring 中的核心接口,它负责管理 ...

Global site tag (gtag.js) - Google Analytics