给大家带来的是xml方式 ,下次有时间加有能力了写完整吧!
下面是我的整个工程
在这里使用我们的dom4j
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
下面直接上我们的代码
ApplicationContext类
package com.prosay.util;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.InitialContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
public class ApplicationContext {
private String ConfigLocation;
//存储我们的加载好的类
private Map<String, Object> map = new HashMap<>();
private ApplicationContext() {}
public ApplicationContext(String ConfigLocation) {
this.ConfigLocation = ConfigLocation;
init();
}
private void init() {
//获取系统目录
String [] str = this.getClass().getResource("/").toString().replace("file:", "").split("/");
StringBuilder sb = new StringBuilder();
for (int i = 0;i<4;i++) {
sb.append(str[i]+File.separator);
}
// 1.获取当前文件的系统路径
String fileLocation = sb+"target"+File.separator+"classes"+File.separator+ ConfigLocation;
System.out.println("当前系统路径" + fileLocation);
try {
// dom4j使用
SAXReader sr = new SAXReader();
// 读取xml文档
Document doucument = sr.read(fileLocation);
// 获得根路径
Element rootelement = doucument.getRootElement();
// 获得所有bean的子节点
List<Element> elements = rootelement.elements("bean");
// 循环子节点的属性 --> 控制反转
for (Element element : elements) {
// 读取id的值
String id = element.attributeValue("id");
// 读取class的路径
String clazzName = element.attributeValue("class");
System.out.println("获得的id:" + id + " 获得的类路径:" + clazzName);
Object o = Class.forName(clazzName).newInstance();
map.put(id, o);
System.out.println("--------------");
// --> 依赖注入
List<Element> propertys = element.elements("property");
// 注入值
for (Element elementPropertys : propertys) {
String ref = elementPropertys.attributeValue("ref");
String name = elementPropertys.attributeValue("name");
System.out.println("ref:" + ref + " name:"+ name);
// 只对属性修改 --> 属性固定了 --> 对象
//获取存储在Bean中的值
Object beanObject = map.get(ref);
//反射设置对象
Field field = o.getClass().getDeclaredField(name.toLowerCase());
field.setAccessible(true);
field.set(o, beanObject);
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name) {
return this.map.get(name);
}
/*public static void main(String[] args) {
new ApplicationContext().init();
}*/
}
Computer类
package com.prosay.service;
import com.prosay.entity.impl.Windons;
public class Computer {
private Windons windons;
public void work(){
this.windons.run();
}
}
Windons7类
package com.prosay.entity;
import com.prosay.entity.impl.Windons;
public class Windons7 implements Windons{
@Override
public void run() {
System.out.println("Windons7正在运行");
}
}
Windons10类
package com.prosay.entity;
import com.prosay.entity.impl.Windons;
public class Windons10 implements Windons{
@Override
public void run() {
System.out.println("Windons10正在运行");
}
}
Windons类
package com.prosay.entity.impl;
public interface Windons {
void run();
}
Test类
package com.prosay.test;
import com.prosay.service.Computer;
import com.prosay.util.ApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext app = new ApplicationContext("applicationContext.xml");
Computer computer = (Computer) app.getBean("Computer");
computer.work();
}
}