Spring中ApplicationContextAware接口的作用
来源:互联网 发布:土豆客户端淘宝登录 编辑:程序博客网 时间:2024/05/29 07:02
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,获得ApplicationContext对象。
前提必须在Spring配置文件中指定该类
*** ApplicationContextRegister
*
* @author yangyy
* @message Created by yangyy on 16/11/3.
*/
public class ApplicationContextRegister implements ApplicationContextAware {
private static final Logger _logger = LoggerFactory.getLogger(ApplicationContextRegister.class);
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
_logger.debug("ApplicationContext registed");
ApplicationContextUtil.getInstance().setApplicationContext(applicationContext);
}
}/**
* ApplicationContextUtil
*
* @author yangyy
* @message Created by yangyy on 16/11/3.
*/
public class ApplicationContextUtil {
private static final Logger _logger = LoggerFactory.getLogger(ApplicationContextUtil.class);
private static ApplicationContextUtil instance = null;
private ApplicationContext applicationContext = null;
private ApplicationContextUtil() {
}
/**
* Gets instance.
*
* @return the instance
*/
public static ApplicationContextUtil getInstance() {
if (instance== null) {
synchronized (ApplicationContextUtil.class) {
if (instance== null) {
instance= new ApplicationContextUtil();
}
}
}
return instance;
}
/**
* Sets application context.
*
* @param applicationContext the application context
*/
public void setApplicationContext(ApplicationContext applicationContext) {
synchronized (instance) {
_logger.debug("setApplicationContext, notifyAll");
instance.applicationContext = applicationContext;
instance.notifyAll();
}
}
/**
* Gets application context.
*
* @return the application context
*/
public ApplicationContext getApplicationContext() {
synchronized (instance) {
while (applicationContext == null) {
try {
_logger.debug("getApplicationContext, wait...");
instance.wait(20000);
if (applicationContext == null) {
_logger.error("Have been waiting for ApplicationContext to be set for 20s", new Exception());
}
} catch (InterruptedException ex) {
_logger.error("getApplicationContext, wait interrupted", ex);
}
}
return applicationContext;
}
}
/**
* Gets bean.
*
* @param name the name
* @return the bean
*/
public <T> T getBean(String name) {
return (T) getApplicationContext().getBean(name);
}
/**
* Get bean.
*
* @param <T> the type parameter
* @param clazz the clazz
* @return the t
*/
public <T> T getBean(Class<T> clazz) {
return (T) getApplicationContext().getBean(clazz);
}
配置文件:<bean class="com.yangbw.framework.core.support.ApplicationContextRegister" />
0 0
- Spring中ApplicationContextAware接口的作用
- Spring中ApplicationContextAware的作用
- ApplicationContextAware接口的作用
- ApplicationContextAware接口的作用
- ApplicationContextAware接口的作用
- spring中ApplicationContextAware接口
- Spring中ApplicationContextAware接口的使用
- Spring中ApplicationContextAware接口的用法
- Spring中ApplicationContextAware接口用法
- spring中ApplicationContextAware接口ServletContextAware接口
- Spring中ApplicationContextAware接口使用理解
- Spring中ApplicationContextAware的理解
- Spring中的ApplicationContextAware接口的使用
- Spring spring中InitializingBean接口和ApplicationContextAware接口
- spring ApplicationContextAware接口介绍
- Spring ApplicationContextAware接口
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean
- Spring中通过实现ApplicationContextAware接口获取bean
- java 中几种map的存储原理和内存占用情况
- Spell checker poj 1035
- 12、SSM框架-Spring 使用XML配置声明式事务(12)
- MySQL5.6.11安装步骤(Windows7 64位)
- cmos密码读取
- Spring中ApplicationContextAware接口的作用
- 致十年后的自己
- Object-C赋值的那点事
- 快速排序
- shell学习-12-for
- 13、SSM框架-Spring 使用注解方式配置声明式事务(13)
- 用c++实现基于vector的迪杰斯特拉算法
- eclipse配置文件导出,并给新工作空间使用
- 运用多线程实现两个线程+1,两个线程-1;线程安全+内部类