首页 > 其他分享 >手写spring框架

手写spring框架

时间:2023-06-07 14:31:59浏览次数:29  
标签:case String 框架 spring value break bean actualValue 手写

Spring IoC容器的实现原理:工厂模式 + 解析XML + 反射机制。

我们给自己的框架起名为:myspring(我的春天)

1. 第一步:创建模块myspring 62

采用Maven方式新建Module:myspring

手写spring框架_spring

打包方式采用jar,并且引入dom4j和jaxen的依赖,因为要使用它解析XML文件,还有junit依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.myspringframework</groupId>
    <artifactId>myspring</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

</project>

2. 第二步:准备好我们要管理的Bean  62

准备好我们要管理的Bean(这些Bean在将来开发完框架之后是要删除的)

注意包名,不要用org.myspringframework包,因为这些Bean不是框架内置的。是将来使用我们框架的程序员提供的。

package com.powernode.myspring.bean;

//手写spring框架  62
//一个普通的bean
public class User {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package com.powernode.myspring.bean;

//手写spring框架  62
//一个dao
public class UserDao {
    public void insert(){
        System.out.println("mysql数据库正在保存用户信息");
    }
}
package com.powernode.myspring.bean;

//手写spring框架  62
//一个service
public class UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void save(){
        userDao.insert();
    }
}

3. 第三步:准备myspring.xml配置文件  62

将来在框架开发完毕之后,这个文件也是要删除的。因为这个配置文件的提供者应该是使用这个框架的程序员。

文件名随意,我们这里叫做:myspring.xml

文件放在类路径当中即可,我们这里把文件放到类的根路径下。

<?xml version="1.0" encoding="UTF-8"?>

<!--这个配置文件是使用myspring框架的开发人员提供的 62-->
<beans>

    <bean id="user" class="com.powernode.myspring.bean.User">
        <property name="name" vlaue="张三"></property>
        <property name="age" vlaue="30"></property>
    </bean>

    <bean id="userService" class="com.powernode.myspring.bean.UserService">
        <property name="userDao" ref="userDaoBean"/>
    </bean>

    <bean id="userDaoBean" class="com.powernode.myspring.bean.UserDao"></bean>

</beans>

使用value给简单属性赋值。使用ref给非简单属性赋值。

4. 第四步:编写ApplicationContext接口  63

ApplicationContext接口中提供一个getBean()方法,通过该方法可以获取Bean对象。

注意包名:这个接口就是myspring框架中的一员了。

package org.myspringframework.core;

/**
 * 手写spring框架  63
 * MySpring框架应用上下文接口。  63
 **/
public interface ApplicationContext {

    /**
     * 根据bean的名称获取对应的bean对象。
     * @param beanName myspring配置文件中bean标签的id。
     * @return 对应的单例bean对象。
     */
    Object getBean(String beanName);

}

5. 第五步:编写ClassPathXmlApplicationContext  63

ClassPathXmlApplicationContext是ApplicationContext接口的实现类。该类从类路径当中加载myspring.xml配置文件。

package org.myspringframework.core;
public class ClassPathXmlApplicationContext implements ApplicationContext{
    @Override
    public Object getBean(String beanId) {
        return null;
    }
}

6. 第六步:确定采用Map集合存储Bean  63

确定采用Map集合存储Bean实例。Map集合的key存储beanId,value存储Bean实例。Map

在ClassPathXmlApplicationContext类中添加Map属性。

并且在ClassPathXmlApplicationContext类中添加构造方法,该构造方法的参数接收myspring.xml文件。

同时实现getBean方法。

package org.myspringframework.core;

import java.util.HashMap;
import java.util.Map;
public class ClassPathXmlApplicationContext implements ApplicationContext{
    /**
     * 存储bean的Map集合
     */
    private Map beanMap = new HashMap<>();

    /**
     * 在该构造方法中,解析myspring.xml文件,创建所有的Bean实例,并将Bean实例存放到Map集合中。
     * @param resource 配置文件路径(要求在类路径当中)
     */
    public ClassPathXmlApplicationContext(String resource) {

    }

    @Override
    public Object getBean(String beanId) {
        return beanMap.get(beanId);
    }
}

7. 第七步:解析配置文件实例化所有Bean  64

在ClassPathXmlApplicationContext的构造方法中解析配置文件,获取所有bean的类名,通过反射机制调用无参数构造方法创建Bean。并且将Bean对象存放到Map集合中

/**
* 在该构造方法中,解析myspring.xml文件,创建所有的Bean实例,并将Bean实例存放到Map集合中。
* @param resource 配置文件路径(要求在类路径当中)
*/
public ClassPathXmlApplicationContext(String resource) {
    try {
        SAXReader reader = new SAXReader();
        Document document = reader.read(ClassLoader.getSystemClassLoader().getResourceAsStream(resource));
        // 获取所有的bean标签
        List<Node> beanNodes = document.selectNodes("//bean");
        // 遍历集合
        beanNodes.forEach(beanNode -> {
            Element beanElt = (Element) beanNode;
            // 获取id
            String id = beanElt.attributeValue("id");
            // 获取className
            String className = beanElt.attributeValue("class");
            try {
                // 通过反射机制创建对象
                Class<?> clazz = Class.forName(className);
                Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
                Object bean = defaultConstructor.newInstance();
                // 存储到Map集合
                beanMap.put(id, bean);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

8. 第八步:测试能否获取到Bean  64

编写测试程序。


//测试
public class MySpringTest {
    @Test
    public void testMySpring(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myspring.xml");
    }
}

手写spring框架_构造方法_02

9. 第九步:给Bean的属性赋值  65-67

通过反射机制调用set方法,给Bean的属性赋值。

继续在ClassPathXmlApplicationContext构造方法中编写代码。

// 再次重新把所有的bean标签遍历一次,这一次主要是给对象的属性赋值。  65
            nodes.forEach(node -> {
                try {
                    Element beanElt = (Element) node;
                    // 获取id
                    String id = beanElt.attributeValue("id");
                    // 获取className
                    String className = beanElt.attributeValue("class");
                    // 获取Class
                    Class<?> aClass = Class.forName(className);
                    // 获取该bean标签下所有的属性property标签
                    List<Element> propertys = beanElt.elements("property");
                    // 遍历所有的属性标签
                    propertys.forEach(property -> {
                        try {
                            // 获取属性名
                            String propertyName = property.attributeValue("name");
                            // 获取属性类型
                            Field field = aClass.getDeclaredField(propertyName);
                            logger.info("属性名:" + propertyName);
                            // 获取set方法名
                            String setMethodName = "set" + propertyName.toUpperCase().charAt(0) + propertyName.substring(1);
                            // 获取set方法
                            Method setMethod = aClass.getDeclaredMethod(setMethodName, field.getType());
                            // 获取具体的值   67
                            String value = property.attributeValue("value"); // "30"
                            Object actualValue = null; // 真值
                            String ref = property.attributeValue("ref");
                            if (value != null) {
                                // 说明这个值是简单类型
                                // 调用set方法(set方法没有返回值)
                                // 我们myspring框架声明一下:我们只支持这些类型为简单类型
                                // byte short int long float double boolean char
                                // Byte Short Integer Long Float Double Boolean Character
                                // String
                                // 获取属性类型名
                                String propertyTypeSimpleName = field.getType().getSimpleName();
                                switch (propertyTypeSimpleName) {
                                    case "byte":
                                        actualValue = Byte.parseByte(value);
                                        break;
                                    case "short":
                                        actualValue = Short.parseShort(value);
                                        break;
                                    case "int":
                                        actualValue = Integer.parseInt(value);
                                        break;
                                    case "long":
                                        actualValue = Long.parseLong(value);
                                        break;
                                    case "float":
                                        actualValue = Float.parseFloat(value);
                                        break;
                                    case "double":
                                        actualValue = Double.parseDouble(value);
                                        break;
                                    case "boolean":
                                        actualValue = Boolean.parseBoolean(value);
                                        break;
                                    case "char":
                                        actualValue = value.charAt(0);
                                        break;
                                    case "Byte":
                                        actualValue = Byte.valueOf(value);
                                        break;
                                    case "Short":
                                        actualValue = Short.valueOf(value);
                                        break;
                                    case "Integer":
                                        actualValue = Integer.valueOf(value);
                                        break;
                                    case "Long":
                                        actualValue = Long.valueOf(value);
                                        break;
                                    case "Float":
                                        actualValue = Float.valueOf(value);
                                        break;
                                    case "Double":
                                        actualValue = Double.valueOf(value);
                                        break;
                                    case "Boolean":
                                        actualValue = Boolean.valueOf(value);
                                        break;
                                    case "Character":
                                        actualValue = Character.valueOf(value.charAt(0));
                                        break;
                                    case "String":
                                        actualValue = value;
                                }

                                setMethod.invoke(singletonObjects.get(id), actualValue);
                            }
                            if (ref != null) {
                                // 说明这个值是非简单类型  66
                                // 调用set方法(set方法没有返回值)
                                setMethod.invoke(singletonObjects.get(id), singletonObjects.get(ref));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

10. 第十步:测试 68

//测试
public class MySpringTest {
    @Test
    public void testMySpring(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myspring.xml");
        Object user = applicationContext.getBean("user");
        System.out.println(user);

        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.save();
    }
}

手写spring框架_构造方法_03

11. 第十一步:打包发布   68

将多余的类以及配置文件删除,使用maven打包发布。

手写spring框架_spring_04

手写spring框架_maven_05

12. 代码汇总

org.myspringframework.core

ApplicationContext

package org.myspringframework.core;

/**
 * 手写spring框架  63
 * MySpring框架应用上下文接口。  63
 **/
public interface ApplicationContext {

    /**
     * 根据bean的名称获取对应的bean对象。
     * @param beanName myspring配置文件中bean标签的id。
     * @return 对应的单例bean对象。
     */
    Object getBean(String beanName);

}

ClassPathXmlApplicationContext

package org.myspringframework.core;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 手写spring框架  63
 * ClassPathXmlApplicationContext是ApplicationContext接口实现类  63
 **/
public class ClassPathXmlApplicationContext implements ApplicationContext{

    private static final Logger logger = LoggerFactory.getLogger(ClassPathXmlApplicationContext.class);

    private Map<String, Object> singletonObjects = new HashMap<>();

    /**
     * 解析myspring的配置文件,然后初始化所有的Bean对象。  63
     * @param configLocation spring配置文件的路径。注意:使用ClassPathXmlApplicationContext,配置文件应当放到类路径下。
     */
    public ClassPathXmlApplicationContext(String configLocation) {
        try {
            //  64
            // 解析myspring.xml文件,然后实例化Bean,将Bean存放到singletonObjects集合当中。
            // 这是dom4j解析XML文件的核心对象。
            SAXReader reader = new SAXReader();
            // 获取一个输入流,指向配置文件
            //ClassLoader获取类加载器,getSystemClassLoader获取系统类加载器
            //getResourceAsStream以流的形式获取系统资源
            InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(configLocation);
            // 读文件
            Document document = reader.read(in);
            // 获取所有的bean标签
            List<Node> nodes = document.selectNodes("//bean");
            // 遍历bean标签
            nodes.forEach(node -> {
                try {
                    // 向下转型的目的是为了使用Element接口里更加丰富的方法。
                    Element beanElt = (Element) node;
                    // 获取id属性
                    String id = beanElt.attributeValue("id");
                    // 获取class属性
                    String className = beanElt.attributeValue("class");
                    logger.info("beanName=" + id);
                    logger.info("beanClassName="+className);
                    // 通过反射机制创建对象,将其放到Map集合中,提前曝光。
                    // 获取Class
                    Class<?> aClass = Class.forName(className);
                    // 获取无参数构造方法
                    Constructor<?> defaultCon = aClass.getDeclaredConstructor();
                    // 调用无参数构造方法实例化Bean
                    Object bean = defaultCon.newInstance();
                    // 将Bean曝光,加入Map集合
                    singletonObjects.put(id, bean);
                    // 记录日志
                    logger.info(singletonObjects.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            // 再次重新把所有的bean标签遍历一次,这一次主要是给对象的属性赋值。  65
            nodes.forEach(node -> {
                try {
                    Element beanElt = (Element) node;
                    // 获取id
                    String id = beanElt.attributeValue("id");
                    // 获取className
                    String className = beanElt.attributeValue("class");
                    // 获取Class
                    Class<?> aClass = Class.forName(className);
                    // 获取该bean标签下所有的属性property标签
                    List<Element> propertys = beanElt.elements("property");
                    // 遍历所有的属性标签
                    propertys.forEach(property -> {
                        try {
                            // 获取属性名
                            String propertyName = property.attributeValue("name");
                            // 获取属性类型
                            Field field = aClass.getDeclaredField(propertyName);
                            logger.info("属性名:" + propertyName);
                            // 获取set方法名
                            String setMethodName = "set" + propertyName.toUpperCase().charAt(0) + propertyName.substring(1);
                            // 获取set方法
                            Method setMethod = aClass.getDeclaredMethod(setMethodName, field.getType());
                            // 获取具体的值   67
                            String value = property.attributeValue("value"); // "30"
                            Object actualValue = null; // 真值
                            String ref = property.attributeValue("ref");
                            if (value != null) {
                                // 说明这个值是简单类型
                                // 调用set方法(set方法没有返回值)
                                // 我们myspring框架声明一下:我们只支持这些类型为简单类型
                                // byte short int long float double boolean char
                                // Byte Short Integer Long Float Double Boolean Character
                                // String
                                // 获取属性类型名
                                String propertyTypeSimpleName = field.getType().getSimpleName();
                                switch (propertyTypeSimpleName) {
                                    case "byte":
                                        actualValue = Byte.parseByte(value);
                                        break;
                                    case "short":
                                        actualValue = Short.parseShort(value);
                                        break;
                                    case "int":
                                        actualValue = Integer.parseInt(value);
                                        break;
                                    case "long":
                                        actualValue = Long.parseLong(value);
                                        break;
                                    case "float":
                                        actualValue = Float.parseFloat(value);
                                        break;
                                    case "double":
                                        actualValue = Double.parseDouble(value);
                                        break;
                                    case "boolean":
                                        actualValue = Boolean.parseBoolean(value);
                                        break;
                                    case "char":
                                        actualValue = value.charAt(0);
                                        break;
                                    case "Byte":
                                        actualValue = Byte.valueOf(value);
                                        break;
                                    case "Short":
                                        actualValue = Short.valueOf(value);
                                        break;
                                    case "Integer":
                                        actualValue = Integer.valueOf(value);
                                        break;
                                    case "Long":
                                        actualValue = Long.valueOf(value);
                                        break;
                                    case "Float":
                                        actualValue = Float.valueOf(value);
                                        break;
                                    case "Double":
                                        actualValue = Double.valueOf(value);
                                        break;
                                    case "Boolean":
                                        actualValue = Boolean.valueOf(value);
                                        break;
                                    case "Character":
                                        actualValue = Character.valueOf(value.charAt(0));
                                        break;
                                    case "String":
                                        actualValue = value;
                                }

                                setMethod.invoke(singletonObjects.get(id), actualValue);
                            }
                            if (ref != null) {
                                // 说明这个值是非简单类型  66
                                // 调用set方法(set方法没有返回值)
                                setMethod.invoke(singletonObjects.get(id), singletonObjects.get(ref));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getBean(String beanName) {
        return singletonObjects.get(beanName);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.myspringframework</groupId>
    <artifactId>course15</artifactId>
    <version>1.0.0</version>
<!--    打包方式  62-->
    <packaging>jar</packaging>
<!--    依赖-->
    <dependencies>
<!--        dom4j是一个能够解析xml文件的java组件-->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.2.0</version>
        </dependency>

<!--        单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!--log4j2的依赖-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.19.0</version>
        </dependency>

    </dependencies>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

</project>

标签:case,String,框架,spring,value,break,bean,actualValue,手写
From: https://blog.51cto.com/u_15784725/6431507

相关文章

  • 测试我们手写的myspring框架
    引入我们的myspring框架<!--用myspring框架需要引入框架--><dependency><groupId>org.myspringframework</groupId><artifactId>course15</artifactId><version>1.0.0</version>......
  • day10-Spring Cloud Alibaba Nacos-服务注册与配置中心
    SpringCloudAlibabaNacos-服务注册与配置中心官网:https://github.com/alibaba/nacos,Nacos官方中文手册Nacos:SpringCloudAlibaba服务注册与配置中心(非常详细)(biancheng.net)SpringCloudAlibaba系列-一文读懂Nacos原理-掘金(juejin.cn)1.什么是NacosNacos/nɑ:k......
  • springcloud中的组件
    1、注册中心组件有多种支持,例如:服务治理NetflixEureka、Consul、Zookeeper2、负载均衡组件NetflixRibbon客户端负载均衡组件2、容错组件NetflixHystrix用于服务熔断降级容错,Resilience4j是G版本推荐的轻量级容错方案,专为Java8和函数式编程而设计,借鉴了Hy......
  • springboot集成swagger
    引入并配置引入依赖这里引入的是3版本<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version></dependency>配置拦截器(若有,则配置)该配置是在有使用到拦截器,例如鉴权的拦截,就需要配置拦截器放......
  • SpringBoot之Spring Data JPA入门学习
    JPA(JavaPersistenceAPI)Java持久化API,是Java持久化的标准规范,Hibernate是持久化规范的技术实现,而SpringDataJPA是在Hibernate基础上封装的一款框架。一、添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</a......
  • 通过redis学网络(1)-用go基于epoll实现最简单网络通信框架
    本系列主要是为了对redis的网络模型进行学习,我会用golang实现一个reactor网络模型,并实现对redis协议的解析。系列源码已经上传githubhttps://github.com/HobbyBear/tinyredis/tree/chapter1redis的网络模型是基于epoll实现的,所以这一节让我们先基于epoll,实现一个最简单的服......
  • springBoot自动装配机制
    自动配置原理@SpringBootApplication是一个组合注解,由@ComponentScan、@EnableAutoConfiguration和@SpringBootConfiguration组成@SpringBootConfiguration与普通@Configuration相比,唯一区别是前者要求整个app中只出现一次@ComponentScanexcludeFilters-用......
  • 记一次用SpringBoot默认连接池HikariCP拿不到连接问题
    记一次用SpringBoot默认连接池HikariCP拿不到连接问题1、问题发现最近项目总接到反馈有用户登录不上系统情况,通过查看日志,并验证多次访问都是正常。2、排查问题安装pinpoint监控后,通过pinpoint监控发现确实会存在获取连接超时情况,如下图查看最近访问情况,发现存在大量超时情况,如下......
  • Spring中如何使用RestTemplate将MultipartFile类型数据发送给被调用方
    被调用方代码@PostMapping("/certificateUpload")publicResult<?>certificateUpload(@RequestPart("file")MultipartFilefile,@RequestParamStringcertificateType){returncertifica......
  • 三天吃透Spring面试八股文
    摘自我的面试网站:topjavaer.cnSpring是什么?Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。最全面的Java面试网站:最全面的Java面试网站Spring的优点通过控制反转和依赖注入实现松耦合。支持面向切面的编程,并且把应用业务逻辑和系统服务分开。通过切面和模......