java面试题

当前位置: 面试问题网 > java面试题 > Spring如何获取Bean

Spring如何获取Bean

通过xml配置文件
   bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext.
   第一种方式:FileSystemXmlApplicationContext
   通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:
   ApplicationContext ac = new FileSystemXmlApplicationContext(“applicationContext.xml”)
   ac.getBean(“beanName”);
   第二种方式:WebApplicationContextUtil
   在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如:
   ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
   ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
   其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();
   另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
   WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

【Spring如何获取Bean】相关文章

1. Spring如何获取Bean

2. Spring对多种ORM框架提供了很好的支持,简单描述在Spring中使用Hibernate的方法

3. Spring面试:什么是Spring, 它有什么特点

4. Spring如何实现事件处理

5. Spring如何实现资源管理

6. Spring如何配置一个bean来从JNDI得到DataSource

7. Spring如何创建一个数据连接池

8. 如何获取某个日期是当月的最后一天

9. Spring里面如何定义hibernate mapping

10. 如何在web应用里面配置spring

本文来源:https://www.mianshiwenti.com/a12645.html

点击展开全部

《Spring如何获取Bean》

将本文的Word文档下载到电脑,方便收藏和打印

推荐程度:

进入下载页面

﹝Spring如何获取Bean﹞相关内容

「Spring如何获取Bean」相关专题

其它栏目

也许您还喜欢