在我们学习spring之前,根据spring的特性,我们来自己来模拟一个spring出来,也就是说不利用spring来实现spring的效果。本实例主要是实现spring的IOC功能。
首先我们把我们用的dao、service、entity定义出来:
Student.java :
- package com.bzu.entity;
- public class Student {
- private int id;
- private String name;
- private String address;
- ******************set、get方法省略
- }
package com.bzu.entity; public class Student { private int id; private String name; private String address; ******************set、get方法省略 }
因为spring提倡的就是面向接口编程,所以在我们写dao层和service层具体实现之前,我们先定义接口,让我们的具体实现实现接口。接口的代码很简单,在这就不贴出来了。
- StudentdaoImp.java
- public class StudentDaoImp implements StudentDao {
- public void add(Student stu) {
- System.out.println("stu is saved");
- }
- }
StudentdaoImp.java public class StudentDaoImp implements StudentDao { public void add(Student stu) { System.out.println("stu is saved"); } }
StudentServiceImp.java
- public class StudentServiceImp implements StudentService {
- StudentDao stuDao=null;
- public StudentDao getStuDao() {
- return stuDao;
- }
- public void setStuDao(StudentDao stuDao) {
- this.stuDao = stuDao;
- }
- @Override
- public void add(Student stu) {
- stuDao.add(stu);
- }
- }
public class StudentServiceImp implements StudentService { StudentDao stuDao=null; public StudentDao getStuDao() { return stuDao; } public void setStuDao(StudentDao stuDao) { this.stuDao = stuDao; } @Override public void add(Student stu) { stuDao.add(stu); } }
这里要注意的是,我们这里是模拟spring,主要模拟spring中的IOC功能,所以在此我们一样要在service层中定义dao的实例,当然不用new出来,我们就通过spring的IOC把这里的dao层注入进来。不要忘了对dao提供set。Get方法,因为IOC的底层其实就是利用反射机制实现的,他把dao注入进来,其实底层就是通过反射set进来的。
好了,我们所需的dao层、service层还有entity定义好了之后,万事俱备只欠东风了,下一步我们就是定义我们自己的ClassPathXmlApplicationContext类了,通过他,在我们new出他的对象的时候,他来加载配置文件,然后把我们的dao操作注入到我们的service层,在spring中,ClassPathXmlApplicationContext类实现了BeanFactory接口,在此我们也定义一个BeanFactory接口,其实这个接口没什么具体的作用,我们就是为了来模拟spring。在定义这个接口和实现类之前,我们先来看一下我们所需的xml是怎么编写的,下面我们就具体来看一下beans.xml的配置:
Beans.xml:
- <beans>
- <bean id="stuDao" class="com.bzu.dao.imp.StudentDaoImp" />
- <bean id="stuService" class="com.bzu.service.imp.StudentServiceImp" >
- <property name="stuDao" bean="stuDao"/>
- </bean>
- </beans>
<beans> <bean id="stuDao" class="com.bzu.dao.imp.StudentDaoImp" /> <bean id="stuService" class="com.bzu.service.imp.StudentServiceImp" > <property name="stuDao" bean="stuDao"/> </bean> </beans>
看到这,相信大家都能感觉到这个配置文件太简单了,没有spring中那么多繁琐的配置,当然啦,我们这是只是实现其中的一个功能,spring提供了很多那么强大的功能,配置当然繁琐一些了。相信上边的代码不用我解释大家也能看懂了吧。
好了,配置文件我们看完了,下一步我们一起来看一下我们的spring容器——ClassPathXmlApplicationContext具体是怎么实现的,我们首先还是来看一下他的接口定义:
BeanFactory.java:
- public interface BeanFactory {
- public Object getBean(String id);
- }
public interface BeanFactory { public Object getBean(String id); }
我们看到,接口其实很简单,就定义了一个getBean方法,下面我们来看一下具体的实现类:
ClassPathXmlApplicationContext.java
- public class ClassPathXmlApplicationContext implements BeanFactory {
- private Map<String, Object> beans = new HashMap<String, Object>();
- public ClassPathXmlApplicationContext() throws Exception, Exception {
- SAXBuilder sb = new SAXBuilder();
- Document doc = sb.build(this.getClass().getClassLoader()
- .getResourceAsStream("beans.xml")); // 构造文档对象
- Element root = doc.getRootElement(); // 获取根元素HD
- List list = root.getChildren("bean");// 取名字为disk的所有元素
- for (int i = 0; i < list.size(); i++) {
- Element element = (Element) list.get(i);
- String id = element.getAttributeValue("id");
- String clazz = element.getAttributeValue("class");
- Object o = Class.forName(clazz).newInstance();
- System.out.println(id);
- System.out.println(clazz);
- beans.put(id, o);
- for (Element propertyElement : (List<Element>) element
- .getChildren("property")) {
- String name = propertyElement.getAttributeValue("name"); // userDAO
- String bean = propertyElement.getAttributeValue("bean"); // u
- Object beanObject = beans.get(bean);// UserDAOImpl instance
- String methodName = "set" + name.substring(0, 1).toUpperCase()
- + name.substring(1);
- System.out.println("method name = " + methodName);
- Method m = o.getClass().getMethod(methodName,
- beanObject.getClass().getInterfaces()[0]);
- m.invoke(o, beanObject);
- }
- }
- }
- @Override
- public Object getBean(String id) {
- return beans.get(id);
- }
- }
public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String, Object> beans = new HashMap<String, Object>(); public ClassPathXmlApplicationContext() throws Exception, Exception { SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(this.getClass().getClassLoader() .getResourceAsStream("beans.xml")); // 构造文档对象 Element root = doc.getRootElement(); // 获取根元素HD List list = root.getChildren("bean");// 取名字为disk的所有元素 for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); String id = element.getAttributeValue("id"); String clazz = element.getAttributeValue("class"); Object o = Class.forName(clazz).newInstance(); System.out.println(id); System.out.println(clazz); beans.put(id, o); for (Element propertyElement : (List<Element>) element .getChildren("property")) { String name = propertyElement.getAttributeValue("name"); // userDAO String bean = propertyElement.getAttributeValue("bean"); // u Object beanObject = beans.get(bean);// UserDAOImpl instance String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); System.out.println("method name = " + methodName); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject); } } } @Override public Object getBean(String id) { return beans.get(id); } }
代码贴出来了,不知道大家看懂没有。下面我来解释一下这段代码:
首先我们定义了一个容器Map<String, Object> beans,这个容器的作用就是用来装我们从配置文件里解析来的一个个bean,为什么要用map类型,我想大家也差不多能猜到吧,我们配置文件中每一个bean都有一个id来作为自己的唯一身份。我们把这个id存到map的key里面,然后value就装我们的具体bean对象。说完这个容器之后,下面我们在来看一下ClassPathXmlApplicationContext的构造方法,这个构造方法是我们spring管理容器的核心,这个构造方法的前半部分是利用的jdom解析方式,把xml里面的bean一个个的解析出来,然后把解析出来的bean在放到我们bean容器里。如果这段代码看不懂的话,那你只好在去看看jdom解析xml了。好了,我们下面在来看一下这个构造的方法,后半部分主要是在对配置文件进行解析出bean的同时去查看一下这个bean中有没有需要注射bean的,如果有的话,他就去通过这些里面的property属性获取他要注射的bean名字,然后构造出set方法,然后通过反射,调用注入bean的set方法,这样我们所需要的bean就被注入进来了。如果这段代码你看不懂的话,那你只能去看一下有关反射的知识了。最后我们就来看一下实现接口的getBean放了,其实这个方法很简单,就是根据提供的bean的id,从bean容器内把对应的bean取出来。
好了,我们所需的东西都定义好了,下面我们据来测试一下,看看我们自己模仿的spring到底能不能自动把我们所需要的dao层给我们注入进来。
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
- Student stu = new Student();
- StudentService service = (StudentService) context.getBean("stuService");
- service.add(stu);
- }
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); Student stu = new Student(); StudentService service = (StudentService) context.getBean("stuService"); service.add(stu); }
运行代码,控制台输出:
com.bzu.service.imp.StudentServiceImp
method name = setStuDao
stu is saved
相关推荐
代码详解:http://blog.csdn.net/csh624366188/article/details/7637705
NULL 博文链接:https://bushyou.iteye.com/blog/1219562
模拟Spring IOC 自己实现依赖注入 自己的Spring 自己的代码模拟spring的依赖注入 自己写的 绝对独一无二
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
模拟实现__Spring的Ioc 1、Spring主要两个作用:实例化Bean,动态装配Bean。并将所有的bean放到spring容器中,调用时从容器中取。Spring容器就是一个bean的Map:private Map, Object> beans = new HashMap, Object>...
spring 控制反转的模拟程序 直观 帮助大家学习spring 共享 不要分
自己写了一个Spring IoC的模拟,有详细注释,可供刚入门学习spring的童鞋加深理解.
NULL 博文链接:https://huangminwen.iteye.com/blog/1874200
这是个模拟spring ioc的学习例子,是个java工程。有助于对aop和ioc的理解
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/759878
自己实现spring-session,实现单点登陆的功能 使用filter拦截用户的请求,在filter中包装request,在request的包装类requestWrapper中,重写getSession(), 和getSession(boolean create)。自己实现httpSession,...
Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理...
上课时给学生讲spring课的例子,自己手工开发的类似spring的框架,只实现了ioc的功能,比较简单容易理解,分享给想学习spring底层原理的朋友
Spring源码深度解析第二版
模拟spring框架的ioc功能,简单实用的实现,高效。
Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring boot专注于快速、方便集成的单个个体,Spring Cloud是关注...
通过java代码模拟spring的依赖注入
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
通过动态代理模拟Spring AOP,通过动态代理模拟Spring AOP