动态获取Spring IOC容器实例化对象
2016-11-06 00:00
429 查看
项目中,使用Spring管理Beans,如果有个类是new来实例化的,他需要依赖一个Spring管理的对象,那么他就应该可以动态得到Spring容器内的一个bean。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextUtils implements ApplicationContextAware { private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { ctx = appContext; } public static ApplicationContext getApplicationContext() { return ctx; } public static Object getBean(Class clazz){ return ctx.getBean(clazz); } }
相关文章推荐
- spring接口 BeanFactoryAware,动态获取IOC容器里面的对象(多例)
- Spring系列之-Spring IOC容器设计:Spring实例化对象
- spring非IOC容器中的对象获取IOC容器中对象的方法
- Spring--IoC--从applicationContext容器中获取对象
- Spring动态获取IoC容器中管理的Bean
- Spring动态获取IoC容器中管理的Bean
- Spring动态获取IoC容器中管理的Bean
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- 如何在Struts2中获取spring的实例化对象
- 【Spring】IOC容器创建对象
- Spring IOC容器中Bean的实例化过程
- spring,mavenwar类型获取容器对象
- spring中通过静态工厂方法获取bean实例化对象
- spring-ioc 放开那个对象,让我(容器)来
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- 获取Spring容器对象
- Spring IOC 容器源码分析 - 创建原始 bean 对象
- 从spring容器中获取已经创建好的对象
- spring容器获取DataSource对象,进行简单数据库应用
- Spring通过容器获取配置对象及事件注入(学习笔记二)