spring实现原理简介

大家好,我是IT修真院上海分院第02期学员,一枚正直善良的java程序员。
今天给分享一下关于Java中AWT的相关知识。

背景介绍
spring是一个从实际开发中抽取出来的框架,因此它完成了大量开发中的通用步骤,留给开发者的仅仅是与特定应用相关的部分,从而大大提高了企业应用的开发效率

知识剖析
spring核心容器:
spring核心容器就是一个超级大工厂,所有的对象都会被当成spring核心容器管理的对象,spring把容器中的一切对象统称为bean。对于spring框架而言,一切Java的对象都是bean
spring使用XML配置文件来管理容器中的bean,配置文件中的bean元素默认以反射方式来调用该类无参数的构造器

spring框架通过反射根据bean元素的class属性指定的类名创建一个Java对象,并以bean元素的id属性的值为key,将该对象放入spring容器中,这个Java对象就成为了spring容器中的bean

property元素是bean元素的子元素,它驱动spring在底层以反射方式执行一次setter方法,name属性值决定执行哪个setter方法,value值或ref值决定执行setter方法的传入参数

编码实战
定义一个类:

public class A1 implements AA {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public void say() {
        System.out.println("---这是--1---");
    }

    @Override
    public String toString() {
        return "A1{" +
                "name='" + name + '\'' +
                '}';
    }
}

spring配置文件:

<bean id="A1" class="principle.spring.A1"/>

运行:

public static void main(String[] args) {
        ApplicationContext cxt = new ClassPathXmlApplicationContext("principle.xml");
        A1 a1 = cxt.getBean("A1",A1.class);
        a1.say();
    }

以上是采用spring来实现的,接下来尝试采用反射来实现类似功能:
建立一个txt文件,用来配置信息:

id=A1
class=principle.spring.A1
name=name
value=Hello,Spring

读取文件信息:

File f1 = new File("C:\\Users\\kelis\\IdeaProjects\\Spring_A\\src\\principle.txt");
        Properties config = new Properties();
        config.load(new FileInputStream(f1));


        String id = config.getProperty("id");
        String className = config.getProperty("class");
        String propertyName = config.getProperty("name");
        propertyName = "set" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
        String value = config.getProperty("value");

利用反射获取对象:

Class clazz = Class.forName(className);
        Constructor constructor = clazz.getConstructor();
        Object obj = constructor.newInstance();

将获取的对象放入容器,这里用HashMap来代替了:

Map testSpring = new HashMap();
       testSpring.put(id,obj);

从容器中根据id获取实例,进行类型转换:

AA aa = (AA) testSpring.get(id);
        aa.say();

还可以模拟spring的设值注入,通过调用setter方法来进行赋值:

Method method = clazz.getMethod(propertyName,String.class);
        method.invoke(obj,value);

最后,可以对这个过程进行封装:

class Context{
        private File f1;
        private Map testSpring;

        public Context(String fileName) throws Throwable{
            this.f1 = new File(fileName);
            Properties config = new Properties();
            config.load(new FileInputStream(f1));
            String id = config.getProperty("id");
            String className = config.getProperty("class");

            String propertyName = config.getProperty("name");
            propertyName = "set" + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
            String value = config.getProperty("value");

            Class clazz = Class.forName(className);
            Constructor constructor = clazz.getConstructor();
            Object obj = constructor.newInstance();

            //设置注入
            System.out.println(propertyName);
            Method method = clazz.getMethod(propertyName,String.class);
            method.invoke(obj,value);

            testSpring = new HashMap();
            testSpring.put(id,obj);
        }


        Object getBean(String id){

            return testSpring.get(id);
        }
    }

获取context对象,然后通过getBean方法就可以获取对象了:

Context context = new Context("C:\\Users\\kelis\\IdeaProjects\\Spring_A\\src\\principle.txt");
        AA aa = (AA) context.getBean("A1");
        aa.say();

        System.out.println(aa.toString());

和之前使用spring时的格式基本一致,效果也是一样的
不过这里的txt文件只能配置一个类,不过大体上实现了spring的基本功能

更多讨论:
怎么实现通过ref的方式来注入
答:
大概是先用id为key,在容器中找到对应的bean,再调用setter方法,将找到的bean作为参数传入,完成注入

采用注解的方式是怎么实现spring的配置的呢
答:
spring的核心容器与配置方式是松耦合的,所以采用何种方式,影响并不大,采用注解方式时,我估计spring会提供一个注解解析器,能够通过注解来获取相关的配置信息,然后将信息传入核心容器中,之后的流程就和xml配置方式一样了

PPT:https://ptteng.github.io/PPT/PPT-java/java-task10-spring%20principle.html#/
视频:https://v.qq.com/x/page/v0535bnh0lx.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 4,220评论 2 7
  • 每日一图 图(单反)/少帅 那些曾经谁笔尖下的分离, 伪装成笑语。 那些曾经关于你我的戏, 微笑着老去! 文/少帅...
    J少帅阅读 114评论 0 0
  • 黑蚂蚁的悲伤 徐 宏 黑蚂蚁还不知道它的灾难 排着队跳着舞狂欢 搅动着黑色的波浪 一颗陨石从天上坠落 ...
    sunxuhong阅读 312评论 0 0