<!-- FactroyBean:* We should specify the full class name of Factory to the property "class="* Actually this bean will return the object from the method "getObject" of the Factory class. --><bean id="car1" class="com.home.factoryBean.CarBeanFactory"><property name="id" value="1"></property><property name="brand" value="Ford"></property></bean>里面bean的配置写法跟反射机制的十分类似。但是一般利用反射机制的bean配置, class= 的值就是bean对象的全类名, 但是利用FactoryBean的方式中, class= 必须是工厂类的全类名。一但这个工厂类实现了FactoryBean接口, 那么这个bean item 返回的就是它里面的getObject()方法return的对象。
client代码
package com.home.factoryBean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class FactoryBeanClient {public static void f(){ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-FactoryBean.xml");Car car1 = (Car)ctx.getBean("car1");System.out.println(car1);}
输出结果
Jun 04, 2016 1:26:00 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@627e9505: startup date [Sat Jun 04 13:26:00 CST 2016]; root of context hierarchyJun 04, 2016 1:26:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [bean-FactoryBean.xml]Car [id=1, name=Ford, price=0]<!-- Baidu Button BEGIN --> <!-- Baidu Button END -->本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138190.htm