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
objPool = Collections.synchronizedMap(new HashMap
());
// 保存对应文件中的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 Bean
private void initPool() throws Exception {
// 遍历配置文件里每个
元素
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属性不存在,或为singleton
if (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);
// 遍历元素的每个
元素
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());
// 如果
元素的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"?>