`
uule
  • 浏览: 6337745 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

通过ApplicationContextAware获取bean

阅读更多

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,获得ApplicationContext 对象。

前提必须在Spring配置文件中指定该类。

 

public class SpringContextHolder implements ApplicationContextAware {

     /**
      * 以静态变量保存ApplicationContext,可在任意代码中取出ApplicaitonContext.
      */
	private static ApplicationContext context;

	/**
	 * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
	 */
	public void setApplicationContext(ApplicationContext context) {
		SpringContextHolder.context =context;
	}	
	public static ApplicationContext getApplicationContext() {
		return context;
	}
	/**
	 * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(String name) {
		return (T) context.getBean(name);
	}
}

 bean中:

<bean id="springContextHolder" class="com.enation.framework.context.spring.SpringContextHolder" />

 

调用:

ICartManager cartManager = SpringContextHolder.getBean("cartManager");

 

正常情况:

 

public class AppManager extends ContextLoaderListener implements  ServletContextListener {

	private ServletContext context;
	private WebApplicationContext webApplicationContext;

	public void contextInitialized(ServletContextEvent sce) {
		this.context = sce.getServletContext();
		this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		this.context.setAttribute("WEBAPPLICATIONCONTEXT", webApplicationContext);

	}

 

HttpSession session = request.getSession();
WebApplicationContext webApplicationContext = (WebApplicationContext)session.getServletContext().getAttribute("WEBAPPLICATIONCONTEXT");
UnsubscribeEmailFacade unsubscribeEmailFacade = (UnsubscribeEmailFacade)webApplicationContext.getBean("unsubscribeEmailFacade");

 法2:

 

org.springframework.web.context.ContextLoader
// 获取spring bean容器
	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
	ISysConfigDao sysconfigdao = (ISysConfigDao) wac.getBean("iSysConfigDao");

 

 

 

 

 

分享到:
评论
2 楼 uule 2010-12-03  
context 是空的?你bean里面注入了吗?
1 楼 yixibo 2010-12-02  
收到,问题 是我在使用SpringContextHolder.getBean("cartManager");  的时候,总是报空指针错误,因为context 是空的,但是在项目启动时,context这个已被 注入 了,不明白原因。不知道你有什么办法,谢谢!

相关推荐

    Java获取Bean的几种方式.pdf

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

    线程中获取spring 注解bean

    在提交任务时,可以将bean作为参数传递,或者在任务内部使用`ApplicationContextAware`接口获取应用上下文,从而获取bean。 4. **ApplicationContextAware**:让线程处理类实现`ApplicationContextAware`接口,...

    springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源

    springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源

    Spring通过ApplicationContext主动获取bean的方法讲解

    Spring通过ApplicationContext主动获取bean的方法讲解 今天,我们来讨论Spring框架中如何通过ApplicationContext主动获取bean的方法。这个问题在实际开发中非常常见,特别是在异步线程或某些特殊情况下无法使用...

    Spring在代码中获取bean的方法小结

    在非Spring管理的类中,如果该类由Spring初始化,我们可以创建一个持有ApplicationContext的成员变量,并使用`@Autowired`注解进行注入,再通过ApplicationContext获取Bean: ```java @Autowired private ...

    Web项目中获取SpringBean与在非Spring组件中获取SpringBean.pdf

    `@Autowired`注解会自动注入BeanFactory,由于它是静态的,所以在任何地方都可以直接通过`SpringWiredBean.beanFactory`获取Bean。 这种方法避免了在XML配置文件中添加额外的Bean声明,更加简洁。但是需要注意,...

    Spring在应用中获得Bean的方法

    实现`ApplicationContextAware`接口,可以获取到`ApplicationContext`的引用,然后通过它获取Bean。 ```java public class MyClass implements ApplicationContextAware { private ApplicationContext context; ...

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

    在独立应用程序中,我们可以通过读取XML文件生成ApplicationContext对象,然后获取Bean实例。例如: ``` ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean(...

    关于SpringBoot获取IOC容器中注入的Bean(推荐)

    这个类实现了`ApplicationContextAware`接口,使用`setApplicationContext`方法来设置`ApplicationContext`对象,然后提供了多个获取Bean对象的方法,例如`getBean(String name)`、`getBean(Class&lt;T&gt; clazz)`等。...

    Spring实现Aware接口自定义获取bean的两种方式

    ApplicationContextAware接口是Spring框架中的一种 Aware接口,通过实现该接口,可以获取ApplicationContext对象,从而获取bean对象。代码如下: @Service public class ApplicationContextHelper implements ...

    Spring启动后获取所有拥有特定注解的Bean实例代码

    在Bean的生命周期中,Spring提供了多种方式来获取Bean的实例,例如通过getBean方法、通过BeanFactory获取Bean实例等。 在获取所有拥有特定注解的Bean实例代码时,需要注意的是,ApplicationContextAware接口不能...

    17. Spring Boot普通类调用bean【从零开始学Spring Boot】

    这是Spring提供的接口,用于获取bean和管理bean的生命周期。你可以通过实现ApplicationContextAware接口,或者直接在代码中创建ApplicationContext实例来访问bean。 5. **非Spring管理类调用bean**: 在非Spring...

    获取Spring容器

    1. **类型转换**:当通过`ApplicationContextUtil.getBean`方法获取Bean时,需要进行类型转换,确保返回的Bean与预期的类型一致。 2. **线程安全性**:如果在多线程环境中使用`ApplicationContextUtil.getBean`方法...

    Spring中关于Bean的管理的课件

    4. **Spring的应用上下文(ApplicationContext)**:ApplicationContext是Spring的主要接口之一,它提供了获取Bean、处理消息和事件等功能,是Spring应用中的主要入口点。 5. **构造注入(constructor injection)*...

    spring的bean作用域

    自定义作用域需要实现ApplicationContextAware接口来获取ApplicationContext,并通过实现Scope接口来定义作用域的行为。 理解并正确使用Bean的作用域是优化Spring应用程序性能和资源管理的关键。选择合适的作用域...

    基于java的企业级应用开发:Bean的生命周期.ppt

    - `ApplicationContextAware`的`setApplicationContext()`方法允许Bean获取到ApplicationContext,这样它可以访问其他Bean或者服务。 4. **BeanPostProcessor**: - BeanPostProcessor接口的`...

    Spring Bean生命周期&BeanDefinitions1

    3. **Bean接口回调**:如果Bean实现了特定的接口,如`BeanNameAware`、`BeanFactoryAware`或`ApplicationContextAware`,Spring会在适当的时候调用对应的回调方法。这些接口允许Bean获取其ID、BeanFactory引用或...

    spring-aware接口实现与bean作用域(spring多容器层面)

    在多个项目整合且跨越Spring容器的情况下,获取Bean的实现方式可能更复杂。可以通过Spring的`ConfigurableApplicationContext`接口的`getBeanFactory()`方法获取`BeanFactory`,然后使用`BeanFactory`的`...

    Spring实战之让Bean获取Spring容器操作示例

    通过以上的知识点讲解,我们了解了在Spring框架中,如何通过`ApplicationContextAware`接口让Bean获取Spring容器的引用,并通过实例演示了如何操作和配置。同时,我们也注意到了在实际开发过程中需要注意的一些事项...

Global site tag (gtag.js) - Google Analytics