【Spring框架】手动模拟IOC容器
一、思维导图
pom 依赖
<!-- 添加dom4j依赖-->
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!--使用的命名空间-->
<beans>
<!-- configuration details go here -->
<bean id="hellowService" class="com.lcySpring.service.HelloService"></bean>
<bean id="AccountService" class="com.lcySpring.service.AccountService"></bean>
<!-- id起别名-->
</beans>
这里最好还是写全生成小绿叶子好一点,不然后面test就报错!!!
<?xml version="1.0" encoding="UTF-8"?>
<!--使用的命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- configuration details go here -->
<!-- configuration details go here -->
<bean id="helloService" class="com.lcySpring.service.HelloService"></bean>
<bean id="AccountService" class="com.lcySpring.service.AccountService"></bean>
<!-- id起别名-->
</beans>
测试类
public class HelloService {
public void test(){
System.out.println("HelloService.......successful!!!!!");
}
}
模拟ioc
1…application.java 接口
public interface ApplicationContext {
public Object getBean(String id);
}
2.接口实现类
public class lcyApplicationContext implements ApplicationContext {
private String xmlPath;
private List<LcyBean> lcyBeans = new ArrayList<>();//存放解析结果
private Map<String,Object> beanMap = new HashMap<>();//存放反射生成的bean
public lcyApplicationContext(String xmlPath){
this.xmlPath=xmlPath;
readXml();
initBean();
}
private void initBean() {
//开始反射生成bean
if(lcyBeans.size()>0){
for(LcyBean lcyBean:lcyBeans){
String id=lcyBean.getId();
String clazz=lcyBean.getClazz();
try {
beanMap.put(id,Class.forName(clazz).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
private void readXml() {
if(null!=xmlPath&&""!=xmlPath){
//创建解析对象
SAXReader reader = new SAXReader();
try {
URL url = this.getClass().getClassLoader().getResource(xmlPath);
Document document = reader.read(url);
List<Node> list=document.selectNodes("//beans/bean");
for(Iterator<Node> iter =list.iterator();iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("id") + e.attributeValue("class"));
//存入集合
LcyBean lcyBean= new LcyBean();
lcyBean.setId(e.attributeValue("id"));
lcyBean.setClazz(e.attributeValue("class"));
lcyBeans.add(lcyBean);
}
} catch (DocumentException e) {
e.printStackTrace();
System.err.println("文件找不到");
}
}
else{
System.err.println("文件为空");
}
}
@Override
public Object getBean(String id) {
return beanMap.get(id);
}
}
3.bean 存放xml解析的信息
public class LcyBean {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
private String clazz;
}
单元测试
public class lcyApplicationContextTest {
@Test
public void test01(){
ApplicationContext context = new lcyApplicationContext("spring.xml");
context.getBean("hellowService");
HelloService helloService =(HelloService) context.getBean("hellowService");
helloService.test();
context.getBean("AccountService");
AccountService accountService = (AccountService) context.getBean("AccountService");
accountService.test();
}
}
附录(关于List 和关于List)
当前版本不再支持直接Element然后遍历取值
需要一个迭代器
List<Node> list=document.selectNodes("//beans/bean");
for(Iterator<Node> iter =list.iterator();iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("id") + e.attributeValue("class"));
}