通过实现ApplicationContextAware接口获取bean
2011-07-23 15:16
429 查看
场景:
在某个bean中需要动态获取其它bean
实例代码:packageorg.company.xxx;
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
public class Demo implements ApplicationContextAware {
// Spring应用上下文环境
private ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
publicvoid setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.applicationContext= applicationContext;
}
public Object getBean(String beanId)throws BeansException {
return applicationContext.getBean(beanId);
}
}注:实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法!
在某个bean中需要动态获取其它bean
实例代码:packageorg.company.xxx;
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
public class Demo implements ApplicationContextAware {
// Spring应用上下文环境
private ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
publicvoid setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.applicationContext= applicationContext;
}
public Object getBean(String beanId)throws BeansException {
return applicationContext.getBean(beanId);
}
}注:实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法!
相关文章推荐
- 通过实现ApplicationContextAware接口获取bean
- 通过实现ApplicationContextAware接口获取bean
- @SpringContext通过实现ApplicationContextAware接口动态获取bean
- 通过实现ApplicationContextAware接口获取bean
- Spring中通过实现ApplicationContextAware接口获取bean
- 通过实现ApplicationContextAware接口动态获取bean
- 【ApplicationContext】通过实现ApplicationContextAware接口获取bean
- Spring中通过实现FactoryBean接口获取bean实例
- Spring中ApplicationContextAware实现获取bean的工具类,在servlet中通过servletContext获取application
- 通过ApplicationContextAware接口获取Spring配置文件中的所有Bean
- Spring实现Aware接口自定义获取bean的两种方式
- 多线程中实现ApplicationContextAware接口获取需要的bean,applicationContext.getBea未返回也未报错
- (07)通过 Aware 接口获取 WEB 资源
- 获取同一接口多个实现类的bean
- 通过接口获取所有实现类——续
- 通过ApplicationContextAware获取bean
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
- 通过实现TextWatcher接口来获取RecyclerView item的EditText的值
- 通过字符创调用接口中实现类的方法,SpringBean自动注入,
- 通过ApplicationContextAware获取bean