实现ApplicationContextAware接口时,获取ApplicationContext为null

将懒加载关闭,@Lazy(false),默认为true

 1 import org.springframework.beans.BeansException;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.ApplicationContextAware;
 4 import org.springframework.context.annotation.Lazy;
 5 import org.springframework.stereotype.Service;
 6
 7 @Service
 8 @Lazy(false)
 9 public class SpringContextUtil implements ApplicationContextAware {
10
11     private static ApplicationContext applicationContext = null; // Spring应用上下文环境
12
13     /*
14      *
15      * 实现了ApplicationContextAware 接口,必须实现该方法;
16      *
17      * 通过传递applicationContext参数初始化成员变量applicationContext
18      */
19
20     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
21         SpringContextUtil.applicationContext = applicationContext;
22     }
23
24     public static ApplicationContext getApplicationContext() {
25         return applicationContext;
26     }
27
28     @SuppressWarnings("unchecked")
29     public static <T> T getBean(String name) throws BeansException {
30         return (T) applicationContext.getBean(name);
31     }
32
33 }
时间: 2024-07-28 18:49:20

实现ApplicationContextAware接口时,获取ApplicationContext为null的相关文章

通过实现ApplicationContextAware接口动态获取bean

转载自:http://penghuaiyi.iteye.com/blog/2042296 场景: 在代码中需要动态获取spring管理的bean 代码: SpringContextUtils.java package com.winner.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springfr

ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题

想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContextListener,系统初始化时调用contextInitialized方法缓存数据: ApplicationContextAware,获取Spring的ApplicationContext对象,以获取spring容器管理的service对象. 系统初始化类如下: 1 package com.liz

如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象. 如何使用 ApplicationContextAware 接口 如何使用该接口?很简单. 1.定义一个工具类,实现 Applicati

Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContext方式 我自己常用的方法: 读取一个文件1 //创建Spring容器 2 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); 3 //获取chinese 实例 4 Person p = ctx.g

Spring初始化ApplicationContext为null

1. ApplicationContextAware初始化 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法. 我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean. 使用方法如下: 1.实现ApplicationContextAware接口: package com.bis.majian.practice.module.sp

获取applicationContext对象的方法

方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供的工具类获取ApplicationConte

spring中获取applicationContext

常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供

如何在Web启动时获取Spring 上下文环境

问题如题,本文给出一个解决方案,满足以下要求: (1)Web启动时能自动获取Spring 的上下文,从而用户可以随意的获取其中的Bean. (2)在单文件测试环境中,亦可以根据配置文件路径加载Spring上下文. 大致原理为: Spring 启动时,会给任何一个实现了ApplicationContextAware接口的Bean(也可以叫做类),注入一个构造函数参数:applicationContext. 有了这样的一个类,就可以在自己的代码里轻松的获取上下文了. 编写这样的一个类还有个好处,那就

ApplicationContextAware 接口的作用

接口说明:当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象. 在Web应用中,Spring容器通常采用声明式方式配置产生:开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器,MVC框架可以直接调用Spring容器中的Bean,无需访问Spring容器本身.在这种情况下,容器中的Bean处于容器管理下,无需