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

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

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

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

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

前提是有时间的情况下。

首先是目录结构


然后是代码部分

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

<?xml version="1.0" encoding="UTF-8"?>
<beans>

	<bean id="hostess" class="spring.achieve.Person">
	<property name ="userName" value = "admin">
	
	</property>

	</bean>
	
</beans>

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差点忘记贴出来了

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>MySpring</groupId>
	<artifactId>test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>


	<dependencies>
		 <dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency> 
			<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1.3-b06</version>
		</dependency>


	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值