用spring也有两年多了 最近一段时间一直在看框架的源代码 从连接池,tomcat到spring 从中学到最多的是代模式理,java反射,设计思想。
我们不但要知其然,还要知其所以然。“知其所以然”的最好 办法就是下载源代码,仔细研读,揣摩并领会源代 码的精义,看看这些经过诸多高手修改的源代码究竟藏有什么玄机,我们能从其中学习到哪些设计思想及设计模式,代码架构如何,软件配置管理又是怎样进行的……,等,我们从源代码中学习的东西太多了。
下面我根据spring源码 简单实现自己的依赖注入 通过xml形式配置 在对象中获取xml文件 获取定义好的bean 从而对bean对应的class 实现实例化 使用接口形式
接口
- public interface PersonDao {
-
- public void add();
-
- }
实现类
- package cn.leam.dao.impl;
-
- import cn.leam.dao.PersonDao;
-
- public class PersonDaoBean implements PersonDao {
- public void add(){
- System.out.println("执行add()方法");
- }
- }
服务接口
- public interface PersonService {
-
- public void save();
-
- }
服务实现类
- public class PersonServiceBean implements PersonService {
- private PersonDao personDao;
-
- public PersonDao getPersonDao() {
- return personDao;
- }
-
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
-
- public void save(){
- personDao.add();
- }
- }
首先配置beans.xml 配置DAO,SERVICE实现类
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="personDao" class="cn.leam.dao.impl.PersonDaoBean"></bean>
- <bean id="personService" class="cn.leam.service.impl.PersonServiceBean">
- <property name="personDao" ref="personDao"></property>
- </bean>
- </beans>
下面模拟spring对xml配置的类进行实例化
存放属性的对象
- public class prosDefinition {
- private String name;
- private String ref;
-
- public ProsDefinition(String name, String ref) {
- this.name = name;
- this.ref = ref;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getRef() {
- return ref;
- }
- public void setRef(String ref) {
- this.ref = ref;
- }
-
- }
存放bean的 对象
- public class Definition {
- private String id;
- private String className;
- private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();
-
- public Definition(String id, String className) {
- this.id = id;
- this.className = className;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getClassName() {
- return className;
- }
- public void setClassName(String className) {
- this.className = className;
- }
- public List<PropertyDefinition> getPropertys() {
- return propertys;
- }
- public void setPropertys(List<PropertyDefinition> propertys) {
- this.propertys = propertys;
- }
-
- }
这里是关键点 所有代码都在这里 使用dom4j 解析xml文件中的bean 并获取id和class 再判断元素中是否有引用元素对其一并获取出来存放才Map中 利用java反射一个一个进行实例化
- public class LeamClassPathXMLApplicationContext {
- private List<Definition> beanDefines = new ArrayList<Definition>();
- private Map<String, Object> sigletons = new HashMap<String, Object>();
-
- public LeamClassPathXMLApplicationContext(String filename){
- this.readXML(filename);
- this.instanceBeans();
- this.injectObject();
- }
-
- private void injectObject() {
- for(Definition beanDefinition : beanDefines){
- Object bean = sigletons.get(beanDefinition.getId());
- if(bean!=null){
- try {
- PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
- for(ProsDefinition propertyDefinition : beanDefinition.getPropertys()){
- for(PropertyDescriptor properdesc : ps){
- if(propertyDefinition.getName().equals(properdesc.getName())){
- Method setter = properdesc.getWriteMethod();
- if(setter!=null){
- Object value = sigletons.get(propertyDefinition.getRef());
- setter.setAccessible(true);
- setter.invoke(bean, value);
- }
- break;
- }
- }
- }
- } catch (Exception e) {
- }
- }
- }
- }
-
- private void instanceBeans() {
- for(Definition beanDefinition : beanDefines){
- try {
- if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim()))
- sigletons.put(beanDefinition.getId(),
- Class.forName(beanDefinition.getClassName()).newInstance());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
-
- private void readXML(String filename) {
- SAXReader saxReader = new SAXReader();
- Document document=null;
- try{
- URL xmlpath = this.getClass().getClassLoader().getResource(filename);
- document = saxReader.read(xmlpath);
- Map<String,String> nsMap = new HashMap<String,String>();
- nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
- XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
- xsub.setNamespaceURIs(nsMap);
- List<Element> beans = xsub.selectNodes(document);
- for(Element element: beans){
- String id = element.attributeValue("id");
- String clazz = element.attributeValue("class");
- Definition beanDefine = new Definition(id, clazz);
- XPath propertysub = element.createXPath("ns:property");
- propertysub.setNamespaceURIs(nsMap);
- List<Element> propertys = propertysub.selectNodes(element);
- for(Element property : propertys){
- String propertyName = property.attributeValue("name");
- String propertyref = property.attributeValue("ref");
- ProsDefinition propertyDefinition = new ProsDefinition(propertyName, propertyref);
- beanDefine.getPropertys().add(propertyDefinition);
- }
- beanDefines.add(beanDefine);
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- public Object getBean(String beanName){
- return this.sigletons.get(beanName);
- }
- }
上面简单的依赖注入 基本完成 当然spring的源码会管家复杂 我们主要是理解其思想 下面我们来测试
- public class SpringTest {
-
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
-
- @Test public void instanceSpring(){
- LeamClassPathXMLApplicationContext ctx = new
- LeamClassPathXMLApplicationContext("beans.xml");
- PersonService personService = (PersonService)ctx.getBean("personService");
- personService.save();
- }
- }