Changes to the Bean acquisition method after the 3.1 version of Spring

Source: Internet
Author: User
Tags deprecated

The XML configuration is unchanged, as follows

<?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>

  

Before version 3.0

The way to get the bean is to use 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 ());}

  

After version 3.1, for some reason, xmlbeanfactory was discarded, and if re-used, it would show

The type xmlbeanfactory is deprecated

The method of obtaining the bean in the source code is as follows

@Testpublic void BeanTest2 () {Resource rs = new Classpathresource ("Test.xml", aatest.class);;D Efaultlistablebeanfactory grandparent = new Defaultlistablebeanfactory (); new Xmlbeandefinitionreader (GrandParent). Loadbeandefinitions (RS); Testbean TB = (Testbean) grandparent.getbean ("Test3"); Assertequals ("Custom", Tb.getname ());}

As for the reasons for being abandoned, the following

/** * Convenience extension of {@link defaultlistablebeanfactory} that reads beans definitions * from an XML document. Delegates to {@link xmlbeandefinitionreader} underneath; Effectively * equivalent to using a xmlbeandefinitionreader with a defaultlistablebeanfactory. * * <p>the structure, element and attribute names of the required XML document * is hard-coded in this class. (Of course a transform could is run if necessary * to produce this format). "Beans" doesn ' t need to be the root element of the XML * Document:this class would parse all bean definition elements in t He XML file. * * <p>this class registers each bean definition with the {@link defaultlistablebeanfactory} * superclass, and Relie s on the latter ' s implementation of the {@link beanfactory} interface. * It supports singletons, prototypes, and references to either of these of beans. * 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 Xmlbeande Finitionreader}.</b> the latter allows for reading from multiple XML * Resources and are highly configurable in its a Ctual XML parsing behavior.* * @author Rod Johnson * @author Juergen hoeller * @author Chris beams * @since 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 ...}

  

 

Changes to the Bean acquisition method after the 3.1 version of Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.