`

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);



下面几种方式没有用过,
getRequiredWebApplicationContext和 getWebApplicationContext的区别是,获取失败时候,getRequiredWebApplicationContext抛出异常,getWebApplicationContext抛出null


方法三:继承自抽象类ApplicationObjectSupport
   说明:

可以方便的获取到ApplicationContext。Spring初始化时,
会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

方法四:继承自抽象类WebApplicationObjectSupport
说明:
类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

方法五:实现接口ApplicationContextAware

说明: 实现该接口的setApplicationContext(ApplicationContext context)方法,


并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。


抽象类ApplicationObjectSupport提供getApplicationContext()方法,
分享到:
评论

相关推荐

    Spring框架中Bean的生命周期 Spring中Bean有几种作用域

    NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695

    Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier

    NULL 博文链接:https://m635674608.iteye.com/blog/2270455

    25个经典的Spring面试问答

    Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner ...

    java面试Spring.pdf

    1. Spring 介绍 1.1 Spring 的优点 ...说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?

    开源框架 Spring Gossip

    从代理机制初探 AOP 动态代理 <br>AOP 观念与术语 Spring AOP Advices Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。 Before ...

    Spring面试专题.pdf

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring面试题.zip

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring面试题含答案.pdf

    25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...

    Spring_Framework_ API_5.0.5 (CHM格式)

    Spring5 是一个重要的版本,距离Spring...随着 Java、JavaEE 和其他一些框架基准版本的增加,SpringFramework5 取消了对几个框架的支持。例如: Portlet Velocity JasperReports XMLBeans JDO Guava

    吴天雄--Spring笔记.doc

    Spring框架简介(EJB、JMX、Spring核心功能、Spring模块详解、Spring重要概念(容器)、Spring容器初始化的整个流程、Spring后处理器),IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含...

    Spring面试专题.md

    * Spring Context:提供框架式的Bean访问方式,以及企业级功能(JNDI、定时任务等); * Spring AOP:AOP服务; * Spring DAO:对JDBC的抽象,简化了数据访问异常的处理; * Spring ORM:对现有的ORM框架的支持; * ...

    ssh(structs,spring,hibernate)框架中的上传下载

    Spring针对几个著名的Web服务器的数据源提供了相应的JDBC抽取器:  •WebLogic:WebLogicNativeJdbcExtractor  •WebSphere:WebSphereNativeJdbcExtractor  •JBoss:JBossNativeJdbcExtractor  在定义了JDBC...

    Spring in Action(第2版)中文版

    3.5使用spring的特殊bean 3.5.1后处理bean 3.5.2bean工厂的后处理 3.5.3配置属性的外在化 3.5.4提取文本消息 3.5.5程序事件的解耦 3.5.6让bean了解容器 3.6脚本化的bean 3.6.1给椰子上lime 3.6.2脚本化bean...

    Spring in Action(第二版 中文高清版).part2

    3.5 使用Spring的特殊Bean 3.5.1 后处理Bean 3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2...

    Spring in Action(第二版 中文高清版).part1

    3.5 使用Spring的特殊Bean 3.5.1 后处理Bean 3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2...

    SpringMVC返回json数据的三种方式

    Spring MVC属于SpringFrameWork的后续产品,已经融合在...1、第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew。 如:<bean id=”defaultJsonView” class=”org.springframework.w

    springmybatis

    下面对这几个配置文件解释下: 1.Configuration.xml 是 mybatis 用来建立 sessionFactory 用的,里面主要包含了数据库连接相关东西,还有 java 类所对应的别名,比如 ...

    SSM 梳理 面试题整理

    14. Spring 支持的几种 bean 的作用域 15. 简单解释一下 spring 的 AOP 16. Mybatis 中#和$的区别? 17. Mybatis 的使用步骤是什么样的? 18. 使用 MyBatis 的 mapper 接口调用时有哪些要求 19. mybatis的缓存...

    2023年最新java面试大全

    【02期】你能说说Spring框架中Bean的生命周期吗? 【03期】如何决定使用 HashMap 还是 TreeMap? 【04期】分库分表之后,id 主键如何处理? 【05期】消息队列中,如何保证消息的顺序性? 【06期】单例模式有几种...

    Java常见面试题208道.docx

    96.spring 支持几种 bean 的作用域? 97.spring 自动装配 bean 有哪些方式? 98.spring 事务实现方式有哪些? 99.说一下 spring 的事务隔离? 100.说一下 spring mvc 运行流程? 101.spring mvc 有哪些组件? 102.@...

Global site tag (gtag.js) - Google Analytics