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中的标签包括默认标签和自定义标签两种,而两种 ...
随机推荐
- 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 ...
- [POJ1143]Number Game
[POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...
- BZOJ 2005 [Noi2010]能量采集 ——Dirichlet积
[题目分析] 卷积一卷. 然后分块去一段一段的求. O(n)即可. [代码] #include <cstdio> #include <cstring> #include < ...
- Redis的数据类型及相关操作命令
redis 基础内容 —— redis的数据类型及相关操作的Linux命令.所谓大厦千层基础承载,希望大家认真学习这一讲: 一.redis 的五大数据类型: 1.String(字符串): 2.List ...
- 实验三 kali下metasploit的漏洞攻击实践
一.实验内容 1.使用kali进行靶机的漏洞扫描,利用metasploit选择其中的一个漏洞进行攻击,并获取权限. 2.分析攻击的原理以及获取了什么样的权限. 二.实验要求 1.熟悉kali原理和使用 ...
- 【POJ3415】Common Substrings(后缀数组,单调栈)
题意: n<=1e5 思路: 我的做法和题解有些不同 题解是维护A的单调栈算B的贡献,反过来再做一次 我是去掉起始位置不同这个限制条件先算总方案数,再把两个串内部不合法的方案数减去 式子展开之后 ...
- 金鹰dreamweaver视频教程下载地址
原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] http://download2.gbaopan.com/c188aecb5e524ab2b2a484c061e0 ...
- MySql将查询结果插入到另外一张表
今天遇到一个业务需求是这样的:对在职员工超过55岁提醒.我想的思路是查询员工表,然后将超过55岁的人的信息存到另一个表,并且以消息的形式给用户提示,用户处理掉之后此消息失效(在数据库做标记). 不管是 ...
- 在 VirtualBox 5.0 系列中让虚拟机支持 USB 3.0 必须开启 APIC
VirtualBox 5.0 系列正式支持 USB 3.0,能够在宿主机支持 USB 3.0 的情况下,让虚拟机也选择具备 USB 3.0 的功能.但是经过多方试验,发现必须在 VirtualBox ...
- eclipse 安卓虚拟机安装apk 及常见问题
首先必须启动虚拟机然后如图操作: