首页 > 其他分享 >spring原理(一)

spring原理(一)

时间:2023-12-27 23:00:14浏览次数:21  
标签:return String spring beanName 原理 propertyValues public BeanDefinition

定义bean类的属性值类

public class PropertyValue {

    private final String name;

    private final Object value;

    public PropertyValue(String name, Object value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public Object getValue() {
        return value;
    }
}

定义bean类的属性多类

public class PropertyValues {
    private final List<PropertyValue> propertyValues = new ArrayList<>();


    //追加资源
    public void addPropertyValues(PropertyValue pv) {
        //属性值内容一样替换掉
        for (Integer i = 0; i < propertyValues.size(); i++) {
            PropertyValue propertyValue = propertyValues.get(i);
            if (propertyValue.getName().equals(pv.getName())) {
                propertyValues.set(i, pv);
            }
        }

        //追加属性值
        propertyValues.add(pv);
    }

    //获取所有资源
    public PropertyValue[] getPropertyValue() {
        return this.propertyValues.toArray(new PropertyValue[0]);
    }
}

创建beanDefinition 定义类(是bean的实际承载类)

public class BeanDefinition {
    public static String SCOPE_SINGLETON = "singleton";
    private static String SCOPE_PROTOTYPE = "prototype";

    //class类
    private Class beanClass;

    /**
     * class 属性值
     */
    private PropertyValues propertyValues;

    /**
     * 类的初始化方法 - 对应xml的init-method
     */
    private String initMethodName;

    /**
     * 类的初始化方法 - 对应xml的init-destroy
     */
    private String destroyMethodName;

    /**
     * 作用域 - scope
     */
    private String scope = SCOPE_SINGLETON;
    private boolean singleton = true;
    private boolean prototype = false;

    /**
     * 懒加载
     */
    private boolean lazyInit = false;

    public BeanDefinition(Class beanClass) {
        this(beanClass, null);
    }

    public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
        this.beanClass = beanClass;
        this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();  //确保资源类已初始化
    }

    public void setScope(String scope) {
        this.scope = scope;
        this.singleton = SCOPE_SINGLETON.equals(scope);
        this.prototype = SCOPE_PROTOTYPE.equals(scope);
    }

    public boolean isSingleton() {
        return singleton;
    }

    public boolean isPrototype() {
        return prototype;
    }
    public void setPropertyValues(PropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }

    public String getInitMethodName() {
        return initMethodName;
    }

    public void setInitMethodName(String initMethodName) {
        this.initMethodName = initMethodName;
    }

    public String getDestroyMethodName() {
        return destroyMethodName;
    }

    public void setDestroyMethodName(String destroyMethodName) {
        this.destroyMethodName = destroyMethodName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BeanDefinition that = (BeanDefinition) o;
        return beanClass.equals(that.beanClass);
    }

    @Override
    public int hashCode() {
        return Objects.hash(beanClass);
    }

    public void setLazyInit(boolean b){
        lazyInit=b;
    }

    public boolean isLazyInit(){
        return lazyInit;
    }
}

 

定义注册表基础接口:

注册表其实就是ConcurrentHashMap,上面beanDefinition会放入其中。但第一次获取会注入加入到三级缓存,后续会写到。

package org.springframework.beans.factory.supper;

import org.springframework.beans.factory.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;

public interface BeanDefinitionRegistry {
    /**
     * 向注册表中注BeanDefinition
     *
     * @param beanName
     * @param BeanDefinition
     */
    void registryBeanDefinition(String beanName, BeanDefinition BeanDefinition);

    /**
     * 向注册表里获取BeanDefinition
     *
     * @param beanName
     */
    BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 注册表是否存在BeanDefinition
     *
     * @param beanName
     */
    boolean containsBeanDefinition(String beanName);
}

 实现bean容器:此处实现容器功能

public class DefaultListableBeanFactory implements BeanDefinitionRegistry {
    private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();

    @Override
    public void registryBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeansException("找不到 beanName:" + beanName);
        }
        return beanDefinition;
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.get(beanName) != null;
    }
}

 

结合以上代码进行实例测试

package org.springFramework.test.ioc;

import org.junit.jupiter.api.Test;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.PropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.supper.DefaultListableBeanFactory;

import java.util.ArrayList;
import java.util.List;

public class BeanDefinitionAndBeanDefinitionRegistryTest {
    @Test
    public void testBeanFactory() throws Exception {
        //定义类值
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValues(new PropertyValue("age", "50"));
        propertyValues.addPropertyValues(new PropertyValue("name", "小辉"));

        //生成bean对象
        BeanDefinition beanDefinition = new BeanDefinition(ServiceUser.class, propertyValues);

        //放入map中
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        defaultListableBeanFactory.registryBeanDefinition("serverUser", beanDefinition);

        //获取指定serverUser
        BeanDefinition serviceUser = defaultListableBeanFactory.getBeanDefinition("serverUser");

        System.out.println();
    }
}

标签:return,String,spring,beanName,原理,propertyValues,public,BeanDefinition
From: https://www.cnblogs.com/jichenghui/p/17931617.html

相关文章

  • springboot 共享session
    在SpringBoot中,可以使用SpringSession来实现共享session。SpringSession是一个基于Spring的会话管理框架,它提供了跨多个服务器的会话共享功能。要使用SpringSession实现共享session,需要按照以下步骤进行配置:添加依赖在SpringBoot项目的pom.xml文件中添加SpringSession的依赖:x......
  • Spring Boot2.x 集成 Skywalking 9.1.0
    参考https://skywalking.apache.org/https://www.cnblogs.com/xiaqiuchu/p/17931230.html(本文使用的该文章的代码,进入可下载源码)https://juejin.cn/post/7001849172278116389#heading-7注意事项本文代码环境为单注册中心、单服务提供者、单消费者。管理面板左侧菜单在没......
  • Spring MVC (文件上传下载)
     1.介绍1)三层架构:表现层(UI):负责数据展示;业务层(BLL):负责业务处理;数据层(DAL):负责数据操作; 2)MVCSpringMVC(Model-View-Controller)是Spring的一部分,基于Java的Web框架,用于开发Web应用框架。提供一种模型(Model)-视图(Vie......
  • Spring Boot2.x 集成 Eureka 与 Feign
    参考https://blog.csdn.net/m0_37959155/article/details/122521406https://blog.csdn.net/Shnywe/article/details/123682758https://www.cnblogs.com/yxth/p/10845640.htmlhttps://juejin.cn/post/6973680096011878407本文完整代码下载注意事项Feign集成了Hystrix......
  • spring MVC 后端 接收 前端 批量添加的数据(简单示例)
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><html><head>  <title>Title</title></head><body><scriptsrc="${pageScope.request.ContextPath}/js/jquery-3.3.1.min.js&qu......
  • 计算机组成原理
    指令系统-按地址码数目分类零地址指令:$OP$含义:1.不需要操作数,如空操作,停机,关中断等指令2.两个操作数放在栈顶和次栈顶,结算结果押回栈顶一地址指令:$OP..A_1$含义:1.$OP(A_1)->A_1$,如加一,减一等操作二地址指令:$......
  • [JAVA基础]后端原理
    后端原理【【网站架构】5分钟了解后端工作原理。为什么Tomcat长时间运行会崩溃?高并发线程池怎么设置?】https://www.bilibili.com/video/BV1PB4y11795/?share_source=copy_web&vd_source=55965a967914567042ced99f130f6538后段部分运行原理Tomcat+war包jar包后端程......
  • 基于Spring Boot2.x 集成 Spring Cloud Gateway
    参考https://blog.csdn.net/DCTANT/article/details/108125229(boot与cloud的版本关系)https://blog.csdn.net/yuanchangliang/article/details/109579705https://blog.csdn.net/qq_38380025/article/details/102968559本文代码下载环境环境版本说明windows1......
  • 【Docker】基础原理
    基础原理基础流程Docker镜像讲解Docker容器讲解创建容器的两种方式容器创建命令详解......
  • 面试官:说说MVCC的执行原理?
    MVCC(Multi-VersionConcurrencyControl)是一种并发控制机制,用于解决数据库并发访问中,数据一致性问题。它通过在读写操作期间保存多个数据版本,以提供并发事务间的隔离性,从而避免了传统的锁机制所带来的资源争用和阻塞问题。所谓的一致性问题,就是在并发事务执行时,应该看到那些数......