2.1 案例:
1 static final String XML_PATH = "applicationContxt.xml"; 2 3 @Test 4 public void testXMLBeanFactory() { 5 try { 6 Resource resource = new ClassPathResource(XML_PATH); 7 XmlBeanFactory beanFactory = new XmlBeanFactory(resource); 8 Car car = (Car) beanFactory.getBean("car"); 9 System.out.println(car); 10 } 11 catch (Exception e) { 12 e.printStackTrace(); 13 } 14 }
xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 5 6 <bean id="car" class="entity.CarFactoryBean"> 7 <property name="carInfo" value="超级跑车,400,2000000" /> 8 </bean> 9 </beans>
2.2 功能分析:
上面这段代码完成的功能无非是以下几点;
(1) 读取配置文件;
(2) 根据XMl中的配置找到对应的类的配置,并实例化;
(3) 把它打印出来