xml配置不变,如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="test3" class="org.springframework.tests.sample.beans.TestBean" scope="prototype"><property name="name"><value>custom</value></property><property name="age"><value>25</value></property></bean></beans>

  

3.0版之前

获取bean的方式是用   XmlBeanFactory

  @Testpublic void beanTest(){Resource rs2 = new ClassPathResource("test.xml",AATest.class);BeanFactory bf = new XmlBeanFactory(rs2);TestBean tb2 = (TestBean)bf.getBean("test3");assertEquals("custom",tb2.getName());}

  

3.1版本之后,因为某些原因,XmlBeanFactory被抛弃了,如果再用的化,会显示

The type XmlBeanFactory is deprecated

源码里的测试方法里获取bean的方法如下

@Testpublic void beanTest2(){Resource rs = new ClassPathResource("test.xml",AATest.class);;DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(rs);TestBean tb = (TestBean) grandParent.getBean("test3");assertEquals("custom",tb.getName());}

当然,也可以像下面这样调用

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/qst/chapter08/bean.xml");Book book = (Book) context.getBean("book");

  

至于被抛弃的原因,如下

/*** Convenience extension of {@link DefaultListableBeanFactory} that reads bean definitions* from an XML document. Delegates to {@link XmlBeanDefinitionReader} underneath; effectively* equivalent to using an XmlBeanDefinitionReader with a DefaultListableBeanFactory.** <p>The structure, element and attribute names of the required XML document* are hard-coded in this class. (Of course a transform could be run if necessary* to produce this format). "beans" doesn't need to be the root element of the XML* document: This class will parse all bean definition elements in the XML file.** <p>This class registers each bean definition with the {@link DefaultListableBeanFactory}* superclass, and relies on the latter's implementation of the {@link BeanFactory} interface.* It supports singletons, prototypes, and references to either of these kinds of bean.* See {@code "spring-beans-3.x.xsd"} (or historically, {@code "spring-beans-2.0.dtd"}) for* details on options and configuration style.** <p><b>For advanced needs, consider using a {@link DefaultListableBeanFactory} with* an {@link XmlBeanDefinitionReader}.</b> The latter allows for reading from multiple XML* resources and is highly configurable in its actual XML parsing behavior.** @author Rod Johnson* @author Juergen Hoeller* @author Chris Beams* @since 15 April 2001* @see org.springframework.beans.factory.support.DefaultListableBeanFactory* @see XmlBeanDefinitionReader* @deprecated as of Spring 3.1 in favor of {@link DefaultListableBeanFactory} and* {@link XmlBeanDefinitionReader}*/
@Deprecated
@SuppressWarnings({"serial", "all"})
public class XmlBeanFactory extends DefaultListableBeanFactory {
//code...}

  

 

转载于:https://www.cnblogs.com/lakeslove/p/7134191.html

Spring在3.1版本后的bean获取方法的改变相关推荐

  1. Spring @Autowired 调用别的包下的Bean 解决方法

    Spring @Autowired 调用别的包下的Bean 解决方法 参考文章: (1)Spring @Autowired 调用别的包下的Bean 解决方法 (2)https://www.cnblog ...

  2. 惊呆了,Spring中竟然有12种定义bean的方法

    前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...

  3. ipython版本_使用pyenv切换版本后ipython无法使用解决方法

    请务必认真阅读和理解本<免责声明及用户隐私协议>(以下简称<协议>)中规定的所有权利和限制.除非您接受本<协议>条款,否则您无权下载.安装或使用本"软件& ...

  4. Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!

    点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...

  5. Spring Cloud Alibaba 发布毕业后的首个版本

    相信大家对上周的 <来自 Spring Cloud 官方的消息,Spring Cloud Alibaba 即将毕业>文章记忆犹新.本周,Spring Cloud Alibaba 正式毕业, ...

  6. Spring Boot 2.3 版本变化[翻译]

    大家好,我是烤鸭: ​ 最近在把低版本的springboot项目升级,正好翻译了下springboot 2.1-2.3 版本的更新日志. ​ Github 原文:https://github.com/ ...

  7. 30个类仿真手写spring框架V2.0版本

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  8. Spring使用与两大核心(Bean,AOP)

    Spring框架 哈哈哈Spring官网的标题直接说明了我们为什么要使用spring.接下来我们看看一下Spring的使用步骤. Spring入门使用 1.下载Spring依赖 先进入官网 点击进入g ...

  9. Spring Framework与JDK版本对应表

    最近在实践Spring项目时,发现无法通过注解的方式实现Bean容器管理, 控制器报错信息为:Failed to read candidate component class,也就是注解扫描不了, 在 ...

最新文章

  1. Android5.0如何正确启用isLoggable(二) 理分析
  2. MySQL 深入浅出索引(上)
  3. 【editor】Source Insight定制之代码风格自动校准功能(AStyle的使用)
  4. IDEA 配置Tomcat
  5. 【Delphi】从内存读取或解压压缩文件(RAR、ZIP、TAR、GZIP等)(二)
  6. 扩展中断控制器8259实验_「正点原子FPGA连载」第十三章双核AMP实验
  7. 二级java编写用户界面例题,单选题11—20:2012年计算机二级VB用户界面设计练习题及答案-计算机二级-233网校...
  8. iOS底层探索之多线程(十五)—@synchronized源码分析
  9. oracle显示工资计算税款,工资税收如何计算公式
  10. 智能音箱---TAS5754M 音频DSP 到Android
  11. linux 动态ip解析,Linux DDNS 动态IP地址解析
  12. pdf转图片在线转换免费
  13. 链接mysql 504_总结403到504的常用问题
  14. net_device_ops的ndo_open和ndo_start_xmit函数
  15. Android 中Fragment之间传递数据
  16. 驻点、极值点、拐点间的区别和联系
  17. Java并发编程73道面试题及答案 —— 面试稳了 侵立删
  18. 内插和数字上变频技术
  19. java 生成随机数字_java如何产生随机数
  20. 1:Web开发入门-Java Web

热门文章

  1. android 揭示动画_遗传编程揭示具有相互作用的多元线性回归
  2. 因为犯罪被判三年刑,期间没办法还信用卡,银行会怎么做?
  3. 银行产生的烂账是如何处理的?
  4. 同盟与对抗:谈《少女杜拉的故事》中的治疗关系(转)
  5. Python Django框架根据模型不能新建表
  6. python 公众号爬虫_python_爬虫_微信公众号抓取
  7. java设计按月每天签到_java实现app签到功能
  8. python递归函数的思想_Python递归函数实例讲解
  9. win10c语言错误,win10打开程序提示错误状态0xc0000020的原因和解决方法
  10. charat越界返回什么_Java基本语法方法修饰符返回类型参数列表异常列表