xml方式
测试类
public static void main(String[] args){
ApplicationContextXml applicationContext = new ApplicationContextXml("haoziContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
}
ApplicationContextXml
package com.haozi.application;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationContextXml {
private String path;
private Map<String,Object> beanContainer;
public ApplicationContextXml(String path){
this.path = path;
beanContainer = new HashMap<String, Object>();
try {
try {
loadBean();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("类名错误:"+e.getMessage());
}
}
private void loadBean() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Document document = XmlUtil.readXML(path);
Element rootElement = document.getDocumentElement();
List<Element> beansElement = XmlUtil.getElements(rootElement,"bean");
for(Element elementItem:beansElement){
String beanid = elementItem.getAttribute("id");
String className = elementItem.getAttribute("class");
Class clazz = Class.forName(className);
beanContainer.put(beanid,clazz.newInstance());
}
}
public Object getBean(String beanId){
if(beanContainer.containsKey(beanId)){
return beanContainer.get(beanId);
}
return null;
}
}
xml
<beans>
<bean id = "userDao" class = "com.haozi.dao.UserDao">
</bean>
</beans>
注解实现
测试类
public static void main(String[] args){
ApplicationContextAnnon applicationContextAnnon = new ApplicationContextAnnon("com.haozi");
StudentService studentService =(StudentService)applicationContextAnnon.getBean("studentService");
System.out.println(studentService);
}
ApplicationContextAnnon
package com.haozi.application;
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.haozi.annon.AutoWired;
import com.haozi.annon.Service;
import com.haozi.util.ClassUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
public class ApplicationContextAnnon {
private HashMap<String, Object> beansContainer;
private String packageName;
public ApplicationContextAnnon(String packageName) {
this.packageName = packageName;
beansContainer = new HashMap<String, Object>();
List<Class<?>> classList = ClassUtil.getClasses(packageName);
loadBean(classList);
injectProperties(classList);
}
public Object getBean(String beanId){
if(beansContainer.containsKey(beanId)){
Object object = beansContainer.get(beanId);
return object;
}
return null;
}
private void injectProperties(List<Class<?>> classList) {
for (Class clazz : classList) {
Field[] fields = ReflectUtil.getFields(clazz);
for (Field field : fields) {
AutoWired autoWiredAnnon = (AutoWired)field.getDeclaredAnnotation(AutoWired.class);
if (autoWiredAnnon!=null) {
String fieldName = field.getName();
if(beansContainer.containsKey(fieldName)){
Object object = beansContainer.get(fieldName);
String beanId = classNameToLower(clazz.getSimpleName());
Object injectObj = beansContainer.get(beanId);
ReflectUtil.setFieldValue(injectObj,fieldName,object);
}
}
}
}
}
private void loadBean(List<Class<?>> classList) {
for (Class clazz : classList) {
Annotation[] annotations = clazz.getAnnotations();
List<Annotation> list = CollectionUtil.newArrayList(annotations);
Service serviceAnnon = (Service)clazz.getDeclaredAnnotation(Service.class);
if (serviceAnnon!=null) {
String beanId = classNameToLower(clazz.getSimpleName());
try {
beansContainer.put(beanId, clazz.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
private String classNameToLower(String className) {
char[] chars = className.toCharArray();
chars[0] += 32;
return String.valueOf(chars);
}
}