首页 > 代码库 > 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

	@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被抛弃了,如果再用的化,会显示

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px ".SF NS Text" }</style>

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());
	}

 

至于被抛弃的原因,如下

/**
 * 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获取方法的改变