对于刚开始工作的开发者平时使用spring的时候只管怎么用,很少去看底层的怎么实现的。都知道spring是容器可以从中获取bean。也都知道通过反射实现获取bean的对象。那么到底Spring是如何创建并管理bean的我们可以详细的分析一下。
第一步要解析applicationContext.xml文件
为此自己写一个类来模拟spring行为。此处加入一个参数为string类型的构造函数,用来读取配置文件及模拟spring以后的行为,
package BJ.YY.junit.test;
import org.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by YYBJ on 2018/9/10.
* ZCL
*/
public class BjyyClassPathXMLApplicationContext {
public BjyyClassPathXMLApplicationContext(String filename) {
//读取spring配置文件
this.readXML(filename);
//实例化bean
this.instanceBeans();
}
准备一个类BeanDefinition 用来存储解析后xml文件。
package BJ.YY.junit.test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by YYBJ on 2018/9/10.
* ZCL
*/
public class BeanDefinition {
//bean的id
private String id;
//bean的类
private String className;
//bean对象的属性
private List<PropertyDefinition> propertys=new ArrayList<>();
public BeanDefinition(String id, String className) {
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<PropertyDefinition> getPropertys() {
return propertys;
}
public void setPropertys(List<PropertyDefinition> propertys) {
this.propertys = propertys;
}
}
下面是解析xml文件的readXml方法:
/*读取xml配置文件
* */
private void readXML(String filename){
//创建一个读取器
3 SAXReader saxReader=new SAXReader();
4 Document document=null;
5 try {
6 //获取要读取的配置文件的路径
7 URL xmlPath=this.getClass().getClassLoader().getResource(fileName);
8 //读取文件内容
9 document=saxReader.read(xmlPath);
10 //获取xml中的根元素
11 Element rootElement=document.getRootElement();
12 for (Iterator iterator = rootElement.elementIterator(); iterator.hasNext();) {
13 Element element = (Element) iterator.next();
14 String id=element.attributeValue("id");//获取bean的id属性值
15 String clazz=element.attributeValue("class");//获取bean的class属性值
16 BeanDefinition beanDefinition=new BeanDefinition(id,clazz);
17 beanDefines.add(beanDefinition);
18 }
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
}
解析完xml后,接下来就是bean的实例化,我们在写一个实例化bean的方法。
spring中是使用getBean的方式来获取bean的,类似的可以用Map的get取值来模拟,因此定义一个Map,用来存储bean的id和bean的对应,所以完整页面
package BJ.YY.junit.test;
import org.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by YYBJ on 2018/9/10.
* ZCL
*/
public class BjyyClassPathXMLApplicationContext {
//用来存储所有的beans
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
//用来存储实例化后的bean
private Map<String, Object> sigletons = new HashMap<String, Object>();
public BjyyClassPathXMLApplicationContext(String filename) {
//读取spring配置文件
this.readXML(filename);
//实例化bean
this.instanceBeans();
}
然后我们来完成instanceBeans方法
/*
* 完成bean的实例化
* */
private void instanceBeans(){
for (BeanDefinition beanDefinition:beanDefines) {
try {
if (beanDefinition.getClassName()!=null &&!"".equals(beanDefinition.getClassName().trim())){
sigletons.put(beanDefinition.getId(),Class.forName(beanDefinition.getClassName()).newInstance());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
实例化后我们来写一个getBean方法,用来在外部获取实例化后的bean,这个搞个最简单的根据bean的id来获取
/*
* 获取bean实例
* */
public Object getBean(String beanName){
return this.sigletons.get(beanName);
}
在src下创建Service层的接口及Service.impl文件下的实现类
package cn.bjyy.service;
/**
* Created by YYBJ on 2018/9/13.
* ZCL
*/
public interface PepoleService {
void save();
}
package cn.bjyy.service.impl;
import BJ.YY.junit.test.BjyyResource;
import cn.bjyy.dao.PersonDao;
import cn.bjyy.service.PepoleService;
import javax.annotation.Resource;
/**
* Created by YYBJ on 2018/9/13.
* ZCL
*/
public class PepoleServiceImpl implements PepoleService {
@Override
public void save() {
System.out.println("save方法测试");
}
}
最后编写测试代码在src下创建测试类
package BJ.YY.junit.test;
import cn.bjyy.service.PepoleService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by YYBJ on 2018/9/10.
* ZCL
*/
public class SpringTest {
@Test
public void test(){
BjyyClassPathXMLApplicationContext ctx = new BjyyClassPathXMLApplicationContext("applicationContext.xml"); // 实例化Spring容器
PepoleService pepoleService= (PepoleService) ctx.getBean("pepoleService"); // 从Spring容器取得bean
pepoleService.save();
}
测试数据idea输出:
save方法测试
转载请说明转载来源:https://blog.csdn.net/weixin_41092717/article/details/82700102