`

spring 获取Context

阅读更多
  Spring获取ApplicationContext的正确方式
首先在web.xml里配置ApplicationContext     
   <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
     </context-param>     
     <listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

java程序中:
  ServletContext servletContext = request.getSession.getServletContext();
        WebApplicationContext wac =    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

前提是要获取request或者Servlet
一些遗留系统中我们要获取Bean采用如下方式:
建一个类实现ApplicationContextAware接口,有一个引用ApplicationContext的静态成员,然后,遗留系统需要引用spring管理的bean的地方,使用这个类。

public class SpringContext implements ApplicationContextAware {

    protected static ApplicationContext context;
    
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static Object getBean(String name){
        return getContext.getBean(name);
    }

}

在ApplicationContext中添加:
<bean id="springContext" class="net.blogjava.chenlb.SpringContext"></bean>

然后在类中直接调用

SpringContext.getBean("name");
分享到:
评论

相关推荐

    spring-context-4.2.xsd.zip

    《Spring框架中的Context模块与XSD配置详解》 在Java企业级开发中,Spring框架扮演着至关重要的角色,尤其在Spring的Context模块中,通过XML Schema(XSD)进行配置,使得应用程序的组件管理和依赖注入变得简洁高效...

    spring-context源码

    《深入剖析Spring-Context源码》 Spring框架是Java企业级应用开发的基石,而`spring-context`模块则是Spring框架的核心部分,它提供了上下文(ApplicationContext)接口,为bean的创建、配置、管理以及与其他服务的...

    Spring获取ApplicationContext对象工具类的实现方法

    在实际开发过程中,经常需要从各个角落获取到这个ApplicationContext对象,以便于能够使用Spring提供的各种服务。本文将详细介绍Spring中获取ApplicationContext对象的工具类实现方法。 在Spring中,典型的获取...

    spring-context-3.0.2.release.jar.zip

    《Spring框架核心模块:Spring-Context 3.0.2 RELEASE深度解析》 Spring框架作为Java领域中的一个重量级框架,其核心模块之一便是Spring-Context,它为开发者提供了全面的上下文支持,使得应用程序组件的配置和生命...

    spring-context-support.jar,quartz-all-1.6.0.jar

    `spring-context`是Spring框架的核心模块之一,它提供了上下文(ApplicationContext)的概念,使开发者可以获取到bean工厂,从而管理和控制应用的全部对象。`spring-context-support.jar`是Spring的一个扩展模块,它...

    利用Spring Context上下文创建自定义对象

    在Spring框架中,Spring Context上下文是核心组件之一,它为开发者提供了强大的依赖注入(Dependency Injection,简称DI)和控制反转(Inversion of Control,简称IoC)功能。本篇文章将深入探讨如何利用Spring ...

    spring-context-3.0.0.RELEASE.jar.zip

    3. **服务定位**:通过Spring Context,可以方便地获取到容器中的任何Bean,实现服务的查找和调用。 4. **事件广播**:Spring Context支持发布和监听自定义事件,使得不同组件之间能进行异步通信。 5. **国际化支持*...

    Spring配置文件spring-context.zip

    "Spring配置文件spring-context.zip"包含了Spring框架中的上下文配置,这是Spring管理对象及服务的核心。 `applicationContext.xml`是Spring应用上下文的主配置文件,它定义了bean的声明、bean之间的依赖关系以及...

    解析web.xml中在Servlet中获取context-param和init-param内的参数

    要从Servlet中获取`context-param`,可以通过以下代码实现: ```java String contextParamValue = getServletContext().getInitParameter("context/param"); ``` `getServletContext().getInitParameter()`方法...

    Spring中的Context你真的懂了吗

    在这个代码中,我们首先创建了一个 AnnotationConfigApplicationContext 实例,然后获取了一个 bean,最后关闭了 Context。这个过程就对应了 Spring Context 的生命周期。 在初始化和启动阶段,Spring 会完成 ...

    Java中Spring获取bean方法小结

    这里我们将详细探讨如何在Java中通过Spring获取配置的bean。 1. **BeanFactory与ApplicationContext** - **BeanFactory** 是Spring中最基础的IoC容器,负责管理和实例化Bean。它允许开发者定义Bean的生命周期和...

    spring-framework-4.2.5.core_beans_context_aop.zip

    Spring Context 还充当了一个全局的bean工厂,允许任何地方的bean获取到其他bean的引用,增强了组件间的交互。此外,它支持消息源和AOP方面的配置,使得应用程序能够更好地适应不同的运行环境。 4. **Spring AOP**...

    获取Spring容器

    ### 获取Spring容器的相关知识点 #### 一、Spring容器简介 Spring框架的核心是其IoC容器,也称为Spring容器。Spring容器负责管理应用对象的生命周期、配置以及装配过程。Spring提供了两种主要类型的容器: 1. **...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    Spring 获取 WebApplicationContext、ApplicationContext 几种方法详解 在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的...

    获取spring容器的方法

    ### 获取Spring容器的方法详解 在Java开发中,Spring框架因其强大的依赖注入(Dependency Injection, DI)和面向切面编程(Aspect Oriented Programming, AOP)功能而被广泛使用。掌握如何获取Spring容器对于管理和...

    几种spring获取bean的方法.txt

    ### 几种Spring获取Bean的方法 #### 1. 通过`WebApplicationContext`获取Bean 在Web环境下,Spring提供了`WebApplicationContext`接口,它是`ApplicationContext`的子接口,专门用于Web应用。可以通过`...

    【新手向】spring系列jar包整理

    3. **spring-context-5.2.5.RELEASE.jar**: 扩展了`spring-core`,提供了上下文支持,包括Bean的定义、配置和生命周期管理,以及消息源、国际化等功能。它也包含了事件传播和应用层服务,如电子邮件、任务调度等。 ...

    spring-framework-5.1.7.RELEASE-dist.zip

    《Spring框架5.1.7.RELEASE:深度解析与应用》 Spring框架是Java开发中的一个核心组件,...通过这个压缩包,开发者可以获取到所有必要的Spring库,快速搭建开发环境,从而专注于业务逻辑的实现,而非基础设施的搭建。

    spring配置中<context-param> 和<init-param>的 区别

    在Spring框架的XML配置中,`&lt;context-param&gt;`和`&lt;init-param&gt;`是两种不同的参数配置方式,它们各自有特定的用途和作用范围。这里我们将深入探讨这两种参数的区别,并通过实际应用来理解它们。 首先,`&lt;context-...

    SpringCloud.pdf

    1. **分布式/版本化配置**:Spring Cloud Config 提供了一个集中式的配置服务器,允许服务动态地获取和更新配置。这使得在分布式环境中管理和维护配置变得更加容易,支持配置的版本控制。 2. **服务注册与发现**:...

Global site tag (gtag.js) - Google Analytics