`
fantaxy025025
  • 浏览: 1278863 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

获取spring的ApplicationContext几种方式

 
阅读更多

From:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html 

获取spring的ApplicationContext几种方式

Java类获取spring 容器的bean

 

常用的5种获取spring 中bean的方式总结:

 

方法一:在初始化时保存ApplicationContext对象

代码:

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");

ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

 

方法二:通过Spring提供的工具类获取ApplicationContext对象

代码:

import org.springframework.web.context.support.WebApplicationContextUtils;

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);

ac1.getBean("beanId");

ac2.getBean("beanId");

说明:

这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

 

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

 

方法三:继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。

Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

 

方法四:继承自抽象类WebApplicationObjectSupport

说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

 

方法五:实现接口ApplicationContextAware

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。

Spring初始化时,会通过该方法将ApplicationContext对象注入。

 

虽 然,spring提供了后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件application-context.xml文件中进行配置。否则获取的ApplicationContext对象将为null。

 

如下是我实现了ApplicationContextAware接口的例子

 

package quartz.util;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringConfigTool implements ApplicationContextAware{//extends ApplicationObjectSupport{
 
private static ApplicationContext context = null;
private static SpringConfigTool stools = null;
public synchronized static SpringConfigTool init(){
  if(stools == null){
   stools = new SpringConfigTool();
  }
  return stools;
}
 
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
  context = applicationContext;
}
 
public synchronized static Object getBean(String beanName) {
  return context.getBean(beanName);
}
 
}

  

XML文件中的配置信息

 

最后提供一种不依赖于servlet,不需要注入的方式

注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,如需细节可以观看源码org.springframework.web.context.ContextLoader

 

Title1 import org.springframework.web.context.ContextLoader; 

import org.springframework.web.context.WebApplicationContext; 

 

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 

wac.getBean(beanID); 

 

 

+

+

+

=

=

=

 

分享到:
评论

相关推荐

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

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

    获取spring容器的方法

    本文将深入探讨几种常见的获取Spring容器的方法,包括使用`ApplicationContext`、通过`ServletContext`、利用`ApplicationObjectSupport`、`WebApplicationObjectSupport`以及实现`ApplicationContextAware`接口等。...

    Java获取Bean的几种方式.pdf

    本文主要探讨了Java获取Bean的多种方式,尤其在Spring Boot和IOC(控制反转)环境下。这些方式可以帮助开发者便捷地从Bean容器中检索和使用所需的Bean。 1. **初始化时保存ApplicationContext对象** 当应用启动时...

    spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext

    总的来说,ApplicationContext 是 Spring 框架的核心组件,它通过不同的实现方式满足不同场景下的需求。理解并熟练掌握各种 ApplicationContext 的使用,能帮助开发者更有效地利用 Spring 框架构建和管理应用。在...

    获取Spring容器

    可以通过多种方式来初始化`ApplicationContext`,其中最常见的有以下几种: 1. **XML配置文件**:使用XML配置文件来定义Spring容器中Bean的配置信息。 2. **注解驱动**:使用注解如`@ComponentScan`、`@...

    Spring在代码中获取bean的几种方式.rar

    以下将详细介绍Spring在代码中获取bean的几种主要方法: 1. **`ApplicationContext` 接口** `ApplicationContext` 是Spring中最常用的接口之一,它提供了获取Bean的多种方法。例如,`getBean(String beanName)` ...

    几种spring获取bean的方法.txt

    根据提供的文件信息,我们可以总结出以下关于Spring框架中获取Bean的几种方法的相关知识点: ### Spring框架简介 Spring框架是一款开源的轻量级Java EE应用程序开发框架,它通过提供一系列强大的功能来简化Java...

    Spring在代码中获取bean的几种方式详解

    "Spring在代码中获取bean的几种方式详解" Spring框架是Java应用程序中最流行的框架之一,它提供了许多功能强大且灵活的功能之一就是Bean管理机制。Bean是Spring框架的核心组件,用于管理应用程序中的业务逻辑。在...

    SpringBoot 获取spring bean方式.rar

    本篇将详细介绍Spring Boot中获取Bean的几种常见方式。 首先,让我们理解什么是Spring Bean。在Spring框架中,Bean是一个由Spring容器管理的对象,通常代表应用中的业务组件或服务。Spring会负责Bean的创建、初始化...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件4

    然而,如果确实有需求,可以通过以下几种方式引入XML配置: 1. **启用XML配置** 在Spring Boot项目中,我们可以通过添加`@ImportResource`注解到主配置类(即带有@SpringBootApplication注解的类)上来导入XML配置...

    线程中获取spring 注解bean

    当需要在线程中获取Spring注解的bean时,有几种常见的方法: 1. **ThreadLocal**:Spring提供了一种名为`ThreadLocalTargetSource`的特殊`TargetSource`实现,可以将bean实例绑定到当前线程。这样,每个线程都有其...

    整合Spring与Struts的几种方法

    1. **继承Spring的ActionSupport类**:Action直接继承Spring的`ActionSupport`,并通过`ApplicationContext`获取服务。这种方式简单,但存在几个缺点:首先,Action与Spring紧密耦合,不利于更换其他IoC容器;其次,...

    第一章 Spring4 简介及获取Bean

    获取Bean主要有以下几种方式: 1. **通过Bean的ID**:使用`ApplicationContext`的`getBean()`方法,传入Bean的ID来获取实例。 2. **自动装配(Autowired)**:使用`@Autowired`注解,Spring会自动匹配类型匹配的...

    Spring4 HelloWorld

    "Spring4 HelloWorld"是入门Spring框架的基础教程,主要涉及以下几个关键知识点: 1. **Spring框架概述**:Spring是一个开源的Java平台,它提供了一个全面的编程和配置模型,用于现代Java应用。Spring的核心特性...

    在action以外的地方获取dao

    - 除了上述几种情况外,还可以在其他的非Action类中使用这种方式获取到所需的DAO对象。 #### 注意事项 1. **确保ApplicationContext的存在**: - 在使用`getRequiredWebApplicationContext`方法前,要确保...

    监听器获取Spring配置文件的方法

    本文将详细介绍如何通过监听器获取Spring配置文件中的bean,以及几种实现方式的优劣分析。 首先,我们需要创建一个监听器来执行这些初始化任务。这里创建了一个名为`InitDataListener`的类,它继承自`...

    struts+spring+hibernate三大框架整合

    1. **使用Spring的ActionSupport**:Action类直接继承自Spring的ActionSupport,通过`super.getWebApplicationContext()`获取Spring上下文,然后通过`ApplicationContext.getBean()`获取bean。这种方式简单,但...

    Spring在应用中获得Bean的方法

    获取Bean主要有以下几种方式: 1. **通过名称获取Bean** 使用`ApplicationContext`的`getBean(String name)`方法可以直接根据Bean的定义名称获取到对应的实例。例如: ```java ApplicationContext context = new...

    学习Spring的一点代码01:如何获取bean?

    Spring提供了多种方式来获取Bean,下面将详细介绍几种常用的方法。 1. **基于XML的配置** 在传统的Spring应用中,Bean定义通常存储在XML文件中。我们可以通过`ApplicationContext`接口的`getBean`方法来获取Bean。...

Global site tag (gtag.js) - Google Analytics