`
TimerBin
  • 浏览: 358654 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

获取spring ApplicationContext常用方法

阅读更多

背景说明:在开发过程中难免会用到ApplicationContext,在这里记录下笔记。

 

第一种方式:根据配置文件获取长用在工具测试类

 

 

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("spring.xml");
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 在junit中配置applicationContext  获取bean

 

 

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml" })
public class TimerBinTest {

	@Autowired
	TimerBin timerBin;

	@Test
	public void init() {

	}
}

 

 

第二种方式:通过ServletContext来获取applicationContext 在serverlet 中比较常用

 

 

import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
ServletContext serverContext = null;
//当没获得时会报错,内部还是调用的getWebApplicationContext方法
ApplicationContext applicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(serverContext);
//当没获得时不会报错
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(serverContext)
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 当在spring MVC的Controller方法中还可以通过以下两种方式获取配置信息

 

1、

 

import javax.servlet.http.HttpServletRequest;
HttpServletRequest request
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 2、

 

 

import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.HttpServletRequest;
HttpServletRequest request
WebApplicationContext webApplicationContext = (WebApplicationContext) request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
webApplicationContext.getBean(TimerBin.class);
webApplicationContext.getBean("timerBin");

 

 

第三种方式:继承ApplicationObjectSupport(抽象类)获取

import org.springframework.context.support.ApplicationObjectSupport;
public class ApplicationContextUtil  extends ApplicationObjectSupport{
	public Object getBean(String name){
		return this.getApplicationContext().getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return this.getApplicationContext().getBean(className);
	}
}

该类是在没有用SpringMVC时使用,不能获取到ServletContext。 

 

第四种方法:继承WebApplicationObjectSupport(抽象类)获取

import org.springframework.web.context.support.WebApplicationObjectSupport;
public class ApplicationContextUtil2  extends WebApplicationObjectSupport{
public class ApplicationContextUtil2  extends WebApplicationObjectSupport{
	public Object getBean(String name){
		return this.getApplicationContext().getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return this.getApplicationContext().getBean(className);
	}
	public WebApplicationContext getWebApplicationContexts(){
		return this.getWebApplicationContext();
	}
}

 可以获得WebApplicationContext信息其中包含了ServletContext配置

 

第五种方法:实现ApplicationContextAware接口(最常用,最推荐方式)

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextUtil implements ApplicationContextAware {

	private static ApplicationContext context;
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context=context;
	}
 
	public static ApplicationContext getApplicationContext(){
		return context;
	}
	public Object getBean(String name){
		return context.getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return context.getBean(className);
	}
}

但是有一点需要在spring的配置文件中对ApplicationContextUtil进行定义

<bean class="cn.timerbin.util.ApplicationContextUtil"></bean>  

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

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

    至于标签中的"源码",这意味着深入理解Spring框架的实现细节,包括ApplicationContext的创建过程、bean的加载和依赖注入等,可以通过阅读Spring的源代码来获取更深入的知识。"工具"可能指的是使用一些辅助工具,如...

    JSP Spring ApplicationContext的国际化支持

    在应用程序中使用国际化支持时,可以在需要显示本地化消息的地方通过ApplicationContext获取MessageSource实例,并调用getMessage方法。在调用时,通常需要提供消息代码(对应资源文件中的key),可选的消息参数...

    Spring3.0常用开发jar包

    由于Spring3.0版本相对较旧,开发者在实际项目中可能需要考虑升级到更现代的版本,以获取最新的功能和安全更新。同时,了解并熟练掌握Spring的这些核心概念和技术,对于成为一名专业的Java开发者至关重要。

    基于java的企业级应用开发:Spring JdbcTemplate 的常用方法.ppt

    在本教程中,我们将深入探讨Spring JdbcTemplate的常用方法,并介绍如何结合JUnit进行单元测试。 首先,`execute()`方法是JdbcTemplate的一个关键功能,它允许执行任意的SQL语句,包括DDL(数据定义语言)如创建表...

    Spring如何获取Bean

    我们可以在 XML 配置文件中定义 Bean,然后使用 FileSystemXmlApplicationContext 或 WebApplicationContextUtil 来获取 ApplicationContext,最后使用 getBean() 方法来获取我们要的 Bean。 例如,我们可以在 XML ...

    spring学习方法

    - 在Java代码中,可以使用`ApplicationContext`接口来获取Spring容器中的Bean。以下是一个示例代码: ```java package day01; import java.util.Calendar; import org.springframework.context.ApplicationContext...

    spring一些常用知识点

    你可以通过`ApplicationContext`接口来获取Bean并进行操作。 3. **AOP(Aspect-Oriented Programming, 面向切面编程)**:Spring提供了AOP支持,允许开发者定义“切面”来封装系统中的横切关注点,如日志、事务管理...

    Spring 应用上下文获取 Bean 的常用姿势实例总结

    本文将总结 Spring 应用上下文获取 Bean 的常用姿势实例,并对其实现方法和操作注意事项进行详细的分析和讲解。 一、从应用程序上下文中获取 Bean 在 Spring 框架中,有多种方式可以获取 Bean 对象。下面我们将...

    spring 中常用的设计模式.docx

    ### Spring中常用的设计模式解析 #### 一、引言 Spring 框架作为 Java 开发领域中的一个重要组成部分,不仅提供了强大的依赖注入和面向切面编程能力,还广泛运用了各种设计模式,使得整个框架更加灵活、易于扩展与...

    JSP 获取Spring 注入对象示例

    3. 获取Spring的ApplicationContext:Spring的`WebApplicationContextUtils`工具类提供了静态方法`getWebApplicationContext()`,可以利用ServletContext来查找Spring的ApplicationContext。 ```jsp ...

    Spring全家桶知识笔记.pdf

    Spring中BeanFactory顶层接口提供了通过容器获取特定名称bean的方法,而ListableBeanFactory接口定义了访问容器中bean基本信息的方法。HierarchicalBeanFactory允许父子级联,这意味着可以创建父子层级关联的容器...

    Spring常用注解

    以下是对Spring常用注解的详细说明: 1. `@Component`:这是Spring的基础组件注解,用于标记一个类作为Spring管理的Bean。你可以将其视为传统XML配置中的`&lt;bean&gt;`标签。Spring会自动扫描标记了此注解的类,并将它们...

    struts+spring+hibernate三大框架整合

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

    spring3.2与Ibatis整合

    4. **在Servlet中获取Bean**:在Servlet中获取Spring管理的Bean,通常通过以下方式: - 使用`WebApplicationContextUtils`从ServletContext中获取ApplicationContext,然后通过ApplicationContext获取Bean。 - ...

    springspring详细教程下载

    4. **读取并使用配置**:在主程序中,使用`ClassPathXmlApplicationContext`类加载配置文件,并通过`getBean`方法获取bean实例,调用其方法。 ### Spring框架的应用上下文(ApplicationContext) `...

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

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

    Spring IOC源码解读

    WebApplicationContext是专门为Web应用设计的ApplicationContext子接口,它提供与Servlet环境集成的方法,如获取ServletContext。ConfigurableApplicationContext接口则提供了更多配置选项,如添加应用监听器,用于...

    Spring Boot读取配置文件常用方式

    以下将详细介绍Spring Boot读取配置文件的常用方法。 1. **属性文件** Spring Boot默认支持两种主要的属性文件格式:`application.properties`和`application.yml`。前者使用键值对形式,后者使用YAML(YAML Ain't...

    第一次搭建Spring框架

    Spring框架是Java开发中最常用的轻量级框架之一,它的出现极大地简化了企业级应用的开发。本教程将详细讲解如何进行第一次Spring框架的搭建,帮助初学者快速入门。 首先,理解Spring的核心特性至关重要。Spring主要...

Global site tag (gtag.js) - Google Analytics