Spring在3.1版本后的bean获取方法的改变
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
@Test
public 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的方法如下
@Test
public 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... }
Spring在3.1版本后的bean获取方法的改变的更多相关文章
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- Spring入门学习(二)三种实例化bean的方法
前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...
- 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常
问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...
- 【Finchley】【升级变更】Spring Cloud 升级到Finchley版本后需要注意的地方
Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级. 升级前 ...
- Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!
点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...
- spring框架详解: IOC装配Bean
1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. 构造方法实例化:(默认无参数) 静态工厂实例化: 实例工厂实例化: 无参数构造方法的实例化: <!-- 默认情况下使用 ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- Spring源码分析(十一)bean的加载
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 经过前面的分析,我们终于结束了对XML配置文件的解析,接下来将会面临更大 ...
- Spring源码分析(七)bean标签的解析及注册
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在上一篇中提到过Spring中的标签包括默认标签和自定义标签两种,而两种 ...
随机推荐
- 第一阶段Sprint 对其他团队评价
咱们的team 针对对“小学生网页四则运算”这个产品的评审,本人提出建议:1.第一阶段的产品Sprint不够好,无任务看板.无燃尽图.希望完善该产品的的Sprint,第二阶段的Sprint要认真写好. ...
- 【Bzoj】1001狼抓兔子(平面图最小割转对偶图最短路)
YEAH 题目链接 终于做对这道题啦 建图的艰辛难以言表- - 顺便说一句我队列转STL啦 狼抓兔子的地图符合平面图定义,于是将该图转成对偶图并求出对偶图的最短路即可. 这篇博客给了我极大的帮助 ...
- 【Luogu】P1602Sramoc问题(堆)
题目链接 很巧妙的想法.一开始将1~k-1加入堆中,然后每次从堆里取出一个最小的,判断是不是答案,如果不是,那么就枚举新数的末一位加上. 代码如下 #include<cstdio> #in ...
- Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)
C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 刷题总结——过河(NOIP2015)
题目: 题目背景 NOIP2005提高组试题2. 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都 ...
- 刷题总结——路径(ssoi)
题目: 题目背景 CF 57D 题目描述 小美今天和她的好朋友在玩捉迷藏游戏.地图可以抽象成一张 n*m 的图,地图上有一些障碍.但这些障碍有一些性质:1:每个障碍周围 8 个格子是没有障碍的.2:每 ...
- BZOJ 2973 入门OJ4798 石头游戏
矩阵递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring ...
- 关于事件委托和时间冒泡(以及apply和call的事项)
搜索事件委托和事件冒泡,网上一大堆乱七八糟的解释,当然意思都对,没毛病. but,真的无聊. 事件冒泡:事件会从点击的元素开始依次向上流出,直到html,遇见事件监听则执行. 事件委托:原因——父元素 ...
- Scrapy学习-6-JSON数据处理
使用json模块处理JSON数据 class JsonwithEncodingPipeline(object): def __init__(self): self.file = codecs.open ...
- Redis数据结构之整数集合
整数集合是Redis用于保存整数值的集合抽象数据结构,它可以保存类型为int16_t .int32_t或者int64_t的整数值,并且保证集合中不会出现重复元素. 一.整数集合数据结构定义 参数说明: ...