java工厂模式——Spring示例package com.fsti.factory.factorybean;public interface Output {public final static Integer MAX_CACHE_LINE = 3;void getData(String str);void out();}package com.fsti.factory.factorybean;public class Printer implements Output {private String[] printData = new String[MAX_CACHE_LINE];// 用以记录当前需打印的作业数private int dataNum = 0;public void getData(String str) {if (dataNum >= MAX_CACHE_LINE) {System.out.println("输出队列一满,添加失败");} else {printData[dataNum++] = str;}}public void out() {while (dataNum > 0) {System.out.println("打印机打印:" + printData[0]);// 把作业整体前移一位,并将剩下的作业数减一System.arraycopy(printData, 1, printData, 0, --dataNum);}}}package com.fsti.factory.factorybean;public class BetterPrinter implements Output {private String[] printData = new String[MAX_CACHE_LINE];// 用以记录当前需打印的作业数private int dataNum = 0;public void getData(String str) {if (dataNum >= MAX_CACHE_LINE) {System.out.println("输出队列一满,添加失败");} else {printData[dataNum++] = str;}}public void out() {while (dataNum > 0) {System.out.println("Better打印机打印:" + printData[0]);// 把作业整体前移一位,并将剩下的作业数减一System.arraycopy(printData, 1, printData, 0, --dataNum);}}}package com.fsti.factory.factorybean;public interface ApplicationContext {Object getBean(String name) throws Exception;}package com.fsti.factory.factorybean;import java.io.File;import java.lang.reflect.Method;import java.util.Collections;import java.util.HashMap;import java.util.Map;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class SJxmlApplicationContext implements ApplicationContext {// 保存容器中所有单利模式的Bean对象private Map<String, Object> objPool = Collections.synchronizedMap(new HashMap<String, Object>());// 保存对应文件中的Document对象private Document doc;// 保存配置文件中的根元素private Element root;public SJxmlApplicationContext(String filePath) throws Exception {SAXReader reader = new SAXReader();doc = reader.read(new File(filePath));root = doc.getRootElement();initPool();initProp();}public Object getBean(String name) throws Exception {Object target = objPool.get(name);// 对于singleton Bean,容器已经初始化了所有Bean实例if (target.getClass() != String.class) {return target;} else {String clazz = (String) target;// 对于prototype并未注入属性值return Class.forName(clazz).newInstance();}}// 初始化容器中的所有singleton Beanprivate void initPool() throws Exception {// 遍历配置文件里每个<bean.../>元素for (Object obj : root.elements()) {Element beanEle = (Element) obj;// 取得元素的id属性String beanId = beanEle.attributeValue("id");// 取得元素的class属性String beanClazz = beanEle.attributeValue("class");// 取得元素的scope属性String beanScope = beanEle.attributeValue("scope");// 如果scope属性不存在,或为singletonif (beanScope == null || beanScope.equals("singleton")) {// 从默认的构造器创建Bean实例,并将其放入objPool中objPool.put(beanId, Class.forName(beanClazz).newInstance());} else {// 对于非singleton Bean,存放该Bean实现类的类名objPool.put(beanId, beanClazz);}}}private void initProp() throws Exception {for (Object obj : root.elements()) {Element beanEle = (Element) obj;// 取得元素的id属性String beanId = beanEle.attributeValue("id");// 取得元素的scope属性String beanScope = beanEle.attributeValue("scope");if (beanScope == null || beanScope.equals("singleton")) {// 取出objPool指定的bean实例Object bean = objPool.get(beanId);// 遍历元素的每个<property.../>元素for (Object prop : beanEle.elements()) {Element propEle = (Element) prop;// 取得name属性的值String propName = propEle.attributeValue("name");// 取得value属性的值String propValue = propEle.attributeValue("value");// 取得元素的ref属性String propRef = propEle.attributeValue("ref");// 将属性的首字母大写String propNameCamelize = propName.substring(0, 1).toUpperCase() + propName.substring(1, propName.length());// 如果<property.../>元素的value属性值存在if (propValue != null && propValue.length() > 0) {Method setter = bean.getClass().getMethod("set" + propNameCamelize, String.class);// 执行setter注入setter.invoke(bean, propValue);}if (propRef != null && propRef.length() > 0) {// 取得需要被依赖注入的Bean实例Object target = objPool.get(propRef);// objPool池中不存在指定Bean实例if (target == null) {// 此处还应处理singleton Bean依赖prototype Bean的情形}// 定义设置注入所需的setter方法Method setter = null;// 遍历target对象所实现的所有接口for (Class superInterface : target.getClass().getInterfaces()) {setter = bean.getClass().getMethod("set" + propNameCamelize, superInterface);// 如果成功取得该接口对应的方法,直接跳出循环break;}// 如果setter方法依然是null// 则直接取得target实现对应的setter方法if (setter == null) {setter = bean.getClass().getMethod("set" + propNameCamelize, target.getClass());}// 执行setter注入setter.invoke(bean, target);}}}}}}package com.fsti.factory.factorybean;public class Computer {private Output out;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Output getOut() {return out;}public void setOut(Output out) {this.out = out;}public Computer() {}public Computer(Output out) {this.out = out;}public void keyIn(String str) {out.getData(str);}public void print() {out.out();}}package com.fsti.factory.factorybean;public class IocTest {public static void main(String[] args) throws Exception {ApplicationContext ctx = new SJxmlApplicationContext("bean.xml");Computer c = (Computer) ctx.getBean("computer");c.keyIn("工厂模式之");c.keyIn("简单工厂模式(spring方式)");c.print();System.out.println(c.getName());System.out.println(ctx.getBean("now"));}}<?xml version="1.0" encoding="UTF-8"?><beans><bean id="computer" class="com.fsti.factory.factorybean.Computer"><property name="name" value="工厂的Bean方式"/><property name="out" ref="printer"/></bean><bean id="printer" class="com.fsti.factory.factorybean.Printer"></bean><bean id="betterPrinter" class="com.fsti.factory.factorybean.BetterPrinter"></bean><bean id="now" class="java.util.Date" scope="prototype"></bean></beans>