手写spring框架,实现简单的ioc功能

 最近重新巩固了基础, 把spring框架重新学习了一遍。

现在用自己的理解将spring框架写一遍。这次先简单实现,以后会慢慢拓展,暂时定的计划是spirngmvc和mybatis的整合。

整体思路是使用dom4j解析xml文件,然后反射注入到Person类中。简单明了,不做过多解释。

毕竟菜鸟一个,现在肯定漏洞百出,希望大佬们能多多指教,我会尽力完善,也请多多评论一下。刷点存在感~,嘿嘿

前提是有时间的情况下。

首先是目录结构

手写spring框架,实现简单的ioc功能_第1张图片

然后是代码部分

ApplicationContext.java

package spring.achieve;

public interface ApplicationContext {
	
	Object getBean(String beanId);

}

ClassPathXmlApplicationContext.java

package spring.achieve;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ClassPathXmlApplicationContext implements ApplicationContext {

	private String fileName;

	public ClassPathXmlApplicationContext(String fileName) {
		this.fileName = fileName;

	}

	@Override
	public Object getBean(String beanId) {
		//使用dom4j读取xml文件
		//单一,慢慢扩展,多个bean的时候,并且只有value,现在只有属性注入,并且异常没有解决
		SAXReader reader = new SAXReader();
		Document document = null;
		Object object = nullValue();
		String currentPath = this.getClass().getResource("").getPath();
	/*	问题1 如何读取到的resources下文件
	 * String currentPath1 = this.getClass().getResource("/").getPath();
	 * currentPath1 返回/E:/java/CX/test/target/test-classes/
	 * currentPath返回 /E:/java/CX/test/target/classes/spring/achieve/  
	 * 而resources下的文件是E:\java\CX\test\target\classes
	   */
		try {
			document = reader.read(new File(currentPath+fileName));
			System.out.println(currentPath+fileName);
            Element root =document.getRootElement();
            Iterator iterable = root.elementIterator();
            while(iterable.hasNext()){
            	Element element = (Element) iterable.next();
            	Iterator proiterable = element.elementIterator();//selectsinglenode不好使,这样很麻烦
            	while(proiterable.hasNext()){
            		Element proelement = (Element) proiterable.next();
            		String propertyName = proelement.attributeValue("name");
            		String propertyValue = proelement.attributeValue("value");
            		String url = element.attributeValue("class");
            		String setMethod = "set"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1);
            		System.out.println(setMethod);
                	try {
    					object = Class.forName(url).newInstance();
    					Method[] methods  =object.getClass().getMethods();
    					for(Method method:methods){
    						if (setMethod.equals(method.getName())) {
    							System.out.println(method.getName());
    							try {
									method.invoke(object, propertyValue);
								} catch (IllegalArgumentException e) {
									e.printStackTrace();
								} catch (InvocationTargetException e) {
									e.printStackTrace();
								}
							}
    						
    					}
    					
    					
    				} catch (InstantiationException e) {
    					e.printStackTrace();
    				} catch (IllegalAccessException e) {
    					e.printStackTrace();
    				} catch (ClassNotFoundException e) {
    					e.printStackTrace();
    				}
            	}
            	
            	
            
            	
            }
            
           
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	
		
		
		

		return null;
	}

}
Person
package spring.achieve;

public class Person {
	String userName;



	public void setUserName(String userName) {
		System.out.println(userName);
		this.userName = userName;
	}
	

}

application.xml




	
	
	
	

	
	

TestMySpring

package test;

import org.junit.Test;

import spring.achieve.ApplicationContext;
import spring.achieve.ClassPathXmlApplicationContext;
import spring.achieve.Person;

public class TestMySpring {
	@Test
	public void testGetBean(){
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		Person person = (Person)context.getBean("pro");
		
	}

}

pom.xml差点忘记贴出来了


	4.0.0
	MySpring
	test
	0.0.1-SNAPSHOT
	war


	
		 
			dom4j
			dom4j
			1.6.1
		 
			
			junit
			junit
			4.12
		
		
			javax.servlet
			servlet-api
			2.5
		
		
			javax.servlet.jsp
			jsp-api
			2.1.3-b06
		


	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
				
			
		
	



你可能感兴趣的:(spring)