实现ApplicationContextAware接口时,获取ApplicationContext为null
将懒加载关闭,@Lazy(false),默认为true
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; @Service
@Lazy(false)
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; // Spring应用上下文环境 /*
*
* 实现了ApplicationContextAware 接口,必须实现该方法;
*
* 通过传递applicationContext参数初始化成员变量applicationContext
*/ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext() {
return applicationContext;
} @SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
} }
实现ApplicationContextAware接口时,获取ApplicationContext为null的更多相关文章
- Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?
1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...
- @SpringContext通过实现ApplicationContextAware接口动态获取bean
场景: 在代码中需要动态获取spring管理的bean 目前遇到的主要有两种场景:1.在工具类中需要调用某一个Service完成某一个功能,如DictUtils2.在实现了Runnable接口的任务类 ...
- 【ApplicationContext】通过实现ApplicationContextAware接口获取bean
SpringApplicationUtils.java import org.springframework.beans.BeansException; import org.springframew ...
- Spring - 运行时获取bean(ApplicationContextAware接口)
默认情况下,我们的bean都是单例模式(即从容器初始化到销毁只保持一个实例).当一个bean需要引用另外一个bean,我们往往会通过bean属性的方式通过依赖注入来引用另外一个bean.那么问题就来了 ...
- ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题
想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...
- spring获取ApplicationContext对象的方法——ApplicationContextAware
一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之 ...
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236 Spring获取ApplicationContex ...
- Spring初始化ApplicationContext为null
1. ApplicationContextAware初始化 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationConte ...
随机推荐
- java多线程基础
多线程基础 读书练习照猫画虎 package Threadtest; import java.util.Date; import java.util.concurrent.ArrayBlockingQ ...
- PHP比较操作符详解(转自hack58)
php的比较操作符有==(等于)松散比较,===(完全等于)严格比较,这里面就会引入很多有意思的问题. 在松散比较的时候,php会将他们的类型统一,比如说字符到数字,非bool类型转换成bool类型, ...
- fdisk,mount.label
########fdisk ll /dev/sda* df -TH fdisk -cul fdisk -cu ~~n~~p~~+1G~~ partx -a /dev/sda ########EXT4 ...
- 跟我学Windows Azure 二 使用SQL Azure创建数据库服务器,创建数据库,创建表
登陆Windows Azure门户 输入我们上一节课所注册的帐号及密码,点击登陆. 选择SQL 数据库,选择服务器 选择创建数据库服务器 设置访问数据库服务器的登陆帐号及密码 点击确定完成数据库服务器 ...
- Python>>>使用Python和Pygame创建画板
下面是画板截图 # -*- coding: utf-8 -*- import pygame from pygame.locals import * import math class Brush: d ...
- 使用keytool 生成证书
keytool 工具介绍 keytool 是java 用于管理密钥和证书的工具,其功能包括: 1 创建并管理密钥 2 创建并管理证书 3 作为CA 为证书授权 4 导入导出证书 keytool 采用k ...
- 【转】libevent和基于libevent的网络编程
转自: http://www.cnblogs.com/nearmeng/p/4043548.html 1 libevent介绍和安装 介绍 libevent是一个轻量级的基于事件驱动的高性能的开源网络 ...
- springmvc+spring-security+mybatis +redis +solar框架抽取
参考文章:Spring MVC 3 深入总结: 第二章 Spring MVC入门 —— 跟开涛学SpringMVC 参考博客:http://www.cnblogs.com/liukemng/categ ...
- open office操作word文档
前段时间工作需要使用open office往word中写文件,写图片,以及向footer也就是页尾中插入图片,已经封装成了类,直接调用即可,代码如下: package com.test.common. ...
- Shiro启用注解方式
shiro验证权限方式一种是基于url配置文件: 例如: <bean id="shiroFilter" class="org.apache.shiro.spring ...