Spring的Bean工厂
Ioc(Inversion of Control)意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。
包括依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)。
1. 基于接口的类型任意定制
2. 一定程度的生产过程定制(AOP)
由xml文件的内容指定生产什么产品
Test
package com.spring.factory;
import java.io.IOException;
import java.util.Properties;
import org.jdom2.JDOMException;
/**
* SpringBean工厂(IOC)
* */
public class Test {
public static void main(String[] args) throws IOException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
/* 从同一个包下的spring.properties文件读取配置信息
//根据配置文件进行某个类的实例化
Properties props = new Properties(); //用来读配置文件
props.load(Test.class.getClassLoader(). //拿到装载Test的class对象的类装载器
getResourceAsStream("com/spring/factory/spring.properties"));
//把某个文件当做InputStream
String VehicleTypeName = props.getProperty("VehicleType"); //根据key得到value的值
System.out.println(VehicleTypeName); //读到配置文件的信息:com.spring.factory.Car
Object o = Class.forName(VehicleTypeName).newInstance();//反射机制
//class文件装载到内存并调用参数为空的构造方法产生类的实例化对象(Object类型)
Moveable m = (Moveable)o;
m.run();
*/
BeanFactory f = null;
try {
f = new ClassPathXmlApplicationContext("com/spring/factory/applicationContext.xml");
} catch (JDOMException e) {
e.printStackTrace();
}
//反射机制,class文件装载到内存并调用参数为空的构造方法产生类的实例化对象(Object类型)
Object o2 = f.getBean("v");
Moveable m2 = (Moveable)o2;
m2.run();
}
}
/**
1
v:com.spring.factory.Car
开汽车
*/
BeanFactory
package com.spring.factory;
public interface BeanFactory {
Object getBean(String id);
}
ClassPathXmlApplicationContext
package com.spring.factory;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
/**
* 使用JDOM方法读取XML文件(jar包需要单独下载导入)
* */
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String,Object> container = new HashMap<String,Object>();
public ClassPathXmlApplicationContext(String fileName) throws
JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException{
SAXBuilder bean=new SAXBuilder();
Document doc=bean.build(this.getClass().getClassLoader().getResourceAsStream(fileName));
Element root=doc.getRootElement();//获取根元素
List list=root.getChildren("bean");//取名字为bean的所有元素
System.out.println(list.size());
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
String id=element.getAttributeValue("id"); //配置文件id的值
String clazz=element.getAttributeValue("class"); //配置文件class的值
System.out.println(id+":"+clazz);
Object o = Class.forName(clazz).newInstance();//newInstance默认调用无参构造器
container.put(id, o);
}
}
@Override
public Object getBean(String id) {
return container.get(id);
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="v" class="com.spring.factory.Car">
</bean>
</beans>
Moveable
package com.spring.factory;
public interface Moveable {
void run();
}
Car
package com.spring.factory;
public class Train implements Moveable {
@Override
public void run() {
System.out.println("开火车");
}
}
Train
package com.spring.factory;
import java.util.ArrayList;
import java.util.List;
/**
* 汽车产品
* */
public class Car implements Moveable{
@Override
public void run() {
System.out.println("开汽车");
}
}
【spring框架】模拟Spring框架(涉及Jdom的使用)
http://blog.csdn.net/acmman/article/details/43701177