當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring在3.1版本后的bean获取方法的改变
生活随笔
收集整理的這篇文章主要介紹了
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
@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获取方法的改变的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交警回应小鹏汽车撞人致死:暂不确认车祸为
- 下一篇: etcd raft library设计原