简单模拟 spring的bean容器的实现
来源:互联网 发布:mac windows10连接网络 编辑:程序博客网 时间:2024/06/05 19:43
原理:从配置文件读取类的地址,通过Class.forName()方法实例化对象。方法静态化,使其工厂启动时,所有的bean以被实例化,可以随时取。
思路 :1.定义beanFactory 接口 2. beanFactoryFactory的实现
定义javabean
public class Hello { public void hello(){ System.out.println("hello spring"); }}
写配置文件
<?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"> <!-- xmlns 即 xml namespace xml 使用的命名空间 xmlns:xsi 即 xml schema instance xml 遵守的具体规范 xsi:schemaLocation 本文档 xml 遵守的规范 官方指定 --> <bean id="hello" class="com.shsxt.Hello"/></beans>
描述对象属性的类
public class Entry { String name; String clazz; public Entry(String name,String clazz){ this.name=name; this.clazz=clazz; }}
beanFactory接口定义
public interface BeanFactory { Object getBean(String id);}
beanFactory实现类
public class BeanFactoryImpl implements BeanFactory { private static final String BEAN_ID = "id"; private static final String BEAN_CLASS = "class"; //对象存放的容器 private static Map<String,Object> container = new ConcurrentHashMap<String,Object>(); //从配置文件读取出来的id和class的键值对才存入list private static List<Entry> list = new LinkedList<Entry>(); /** * 构造器,传入配置文件,并初始化bean池 * @param filePath */ public BeanFactoryImpl(String filePath){ readXMLHelper(filePath); beanInit(); } /** * 读取配置文件,获取id和class,list * @param filePath */ private static void readXMLHelper(String filePath) { URL url = BeanFactoryImpl.class.getClassLoader().getResource(filePath); SAXReader saxReader = new SAXReader(); try{ Document document = saxReader.read(url); Map<String,Object> map = new HashMap<String,Object>(); map.put("sxt","http://www.springframework.org/schema/beans"); XPath xPath = document.createXPath("//sxt:bean"); xPath.setNamespaceURIs(map); List<Element> elements = xPath.selectNodes(document); for (Element element:elements){ list.add(new Entry(element.attributeValue(BEAN_ID),element.attributeValue(BEAN_CLASS))); } } catch (DocumentException e) { e.printStackTrace(); } } /** * 根据list中的class值,实例化对象,并将对应的id-Object存入容器container内 */ private static void beanInit() { Iterator<Entry> iterator = list.iterator(); while(iterator.hasNext()){ Entry en = iterator.next(); try{ Class<?> cls = Class.forName(en.clazz); Object obj = cls.newInstance(); container.put(en.name,obj); }catch (Exception e){ e.printStackTrace(); } } } /** * 重写接口的getBean方法,从容器container中取值 * @param name * @return */ public Object getBean(String name) { return container.get(name); }}
测试类
public class HelloTest { public static void main(String[] args) { BeanFactoryImpl ac = new BeanFactoryImpl("spring.xml"); Object object = ac.getBean("hello"); Hello hello = (Hello) object; hello.hello(); }}
阅读全文
0 0
- 简单模拟 spring的bean容器的实现
- 最简单的模拟spring容器例子
- 模拟实现spring的bean工厂
- Spring容器-Bean的生命周期
- Spring容器-Bean的生命周期
- Spring的Ioc容器简单实现
- 笨鸟之Spring实例化bean的模拟实现
- 模拟spring,自定义容器一(属性注入、Bean的实例化)
- Spring容器、Bean配置信息。Bean实现类及应用程序的关系
- Spring 架构的简单模拟实现
- 自定义spring容器--spring容器读取bean配置文件的原理
- java解析xml模拟容器的实现 如spring等
- Spring框架的ioc容器核心功能模拟实现
- Spring容器和被管理的Bean
- java类获取spring容器的bean
- Java类获取spring 容器的bean
- 获取Spring 容器中bean的方法
- Spring容器对Bean组件的管理
- iOS之热修复JSPatch 实现原理详解
- java 编写 输入三个边长 判断是否组成三角行 若能组成三角行 求面积
- Linux使用diff和patch制作和安装补丁实例
- 经验分享:如何系统学习 Web 前端技术?
- python的文件操作
- 简单模拟 spring的bean容器的实现
- ecshop适应PHP7的修改及事务处理
- MXNet官方文档中文版教程(8):使用预训练模型预测
- 万能地图下载器中在任务列表中如何快速选择多个任务?
- 安卓xml文件中设置动画匀速旋转无效?
- U-boot主Makefile分析
- 订单支付功能对接支付宝支付接口
- Android Studio 3.0 更新后的各种坑
- Hibernate框架第一天(入门)