手寫簡單的spring框架(註解版)
阿新 • • 發佈:2018-11-12
第一步新建兩個註解類如下
Component.java
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Component { public String name() default ""; }
SetValue.java
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface SetValue { public String value() default ""; public String isPOJO() default "no"; }
第二步準備兩個JAVA Bean
Studnet.java
package pojo; import annotation.Component; import annotation.SetValue; @Component public class Student { @SetValue(value = "1001") private String id; @SetValue(value = "張三") private String name; public Student(String id, String name) { super(); this.id = id; this.name = name; } public Student() { super(); // TODO Auto-generated constructor stub } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
StuManage.java
package pojo;
import annotation.Component;
import annotation.SetValue;
@Component(name="sm")
public class StuManage {
@SetValue(isPOJO="yes",value="student")
private Student student;
public StuManage() {
super();
// TODO Auto-generated constructor stub
}
public StuManage(Student student) {
super();
this.student = student;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "StuManage [student=" + student + "]";
}
}
第三步寫spring容器
ApplicationContext.java
package app;
public interface ApplicationContext {
public Object getBean(String beanName);
}
AnnotationClassPathXmlApplicationContext.java
package app;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import annotation.Component;
import annotation.SetValue;
public class AnnotationClassPathXmlApplicationContext implements AppcationContext{
private Map map = new HashMap<>();
public AnnotationClassPathXmlApplicationContext(String packageName) throws DocumentException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException {
//掃描包下面的類是否有註解
String path = packageName.replace(".","/");
//專案路徑
URL url = this.getClass().getClassLoader().getResource(path);
//判斷路徑是否存在
if(url != null) {
File file = new File(url.getPath());
//判斷是否是目錄
if(file.isDirectory()) {
File[] listFiles = file.listFiles();
for (File file2 : listFiles) {
String fileName = file2.getName();
String className =packageName+"."+fileName.substring(0,fileName.indexOf("."));
//System.out.println(className);
Class cls = Class.forName(className);
Object obj = cls.newInstance();
//判斷是否有Component註解
if(cls.isAnnotationPresent(Component.class)) {
//獲取例項名
String id = ((Component)cls.getAnnotation(Component.class)).name();
if("".equals(id)) {
id = cls.getSimpleName().toLowerCase();
}
//注入屬性值
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
SetValue setValue = field.getAnnotation(SetValue.class);
if(setValue != null) {
if("no".equals(setValue.isPOJO())) {
field.set(obj,setValue.value());
}else {
field.set(obj,map.get(setValue.value()));
}
}
}
map.put(id,obj);
}
}
}
}
}
@Override
public Object getBean(String beanName) {
return map.get(beanName);
}
}
Test.java
package test;
import org.dom4j.DocumentException;
import app.AnnotationClassPathXmlApplicationContext;
import app.AppcationContext;
import pojo.StuManage;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, DocumentException {
AppcationContext app = new AnnotationClassPathXmlApplicationContext("pojo");
StuManage manage = (StuManage)app.getBean("sm");
System.out.println(manage);
}
}
完成!!!!