手写简单的spring框架(注解版)

第一步新建两个注解类如下

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);
	}
}

完成!!!!

猜你喜欢

转载自blog.csdn.net/liukaitydn/article/details/81161710