手写spring框架,实现简单的ioc功能
程序员文章站
2022-07-12 12:57:16
...
最近重新巩固了基础, 把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;
}
}
Personpackage 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>
上一篇: Spring bean作用域介绍
下一篇: 简单的spring IOC 实现