Spring从容器中获取bean对象可以分别通过什么接口

发布时间:2019-08-06 23:25:43

Spring从容器中获取bean对象可以分别通过什么接口

推荐回答

:初始化保存ApplicationContext象new ClassPathXmlApplicationContext("applicationContext.xml").getBean("beanId"); 种式适用于采用Spring框架独立应用程序需要程序通配置文件手工初始化Spring配置信息 二:通Spring提供工具类获取ApplicationContext象 import org.springframework.web.context.support.WebApplicationContextUtils;ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) ac1.getBean("beanId"); ac2.getBean("beanId");种式适合于采用Spring框架B/S系统通ServletContext象获取ApplicationContext象通获取需要类实例面两工具式区别前者获取失败抛异者返null 三:继承自抽象类ApplicationObjectSupport 抽象类ApplicationObjectSupport提供getApplicationContext()便获取ApplicationContextSpring初始化通该抽象类setApplicationContext(ApplicationContext context)ApplicationContext 象注入四:继承自抽象类WebApplicationObjectSupport 类似面调用getWebApplicationContext()获取WebApplicationContext 五:实现接口ApplicationContextAware 实现该接口setApplicationContext(ApplicationContext context)并保存ApplicationContext 象Spring初始化通该ApplicationContext 象注入 总结:系统用述类实际于Spring框架紧密耦合起些类运行Spring框架系统应该尽量减少类应用使系统尽能独立于前运行环境看看篇文章挺详细:http://blog.csdn.net/wklken/article/details/6342977
以上问题属网友观点,不代表本站立场,仅供参考!