【深度剖析】spring1.0源码--1、Spring是什么

有兴趣的小伙伴可以看b站相关视频:https://www.bilibili.com/video/BV1bY41167rg/?spm_id_from=333.999.0.0

什么是Spring?我们先看下百度百科怎么说的?

Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)

再来看看spring的readme.txt是怎么介绍轻量级容器。

Spring is a very lightweight container in that:

1.Your objects don't need to implement Spring APIs to run in Spring, as Spring uses Inversion of Control (IoC). Spring configures your objects through JavaBean properties that they expose. Your objects don't need to ask Spring for collaborators, they receive respective references via their bean properties, just like any other property values.
2.A Spring application context definition and the application beans defined in it can be used in nearly every environment, from J2EE containers to applets. You will just use different bootstrap implementation for the context.
3.By default, all beans are directly accessible as plain old Java objects, without any overhead. Special capabilities can be added a la carte via respective AOP interceptors, e.g. declarative transactions or security checks.
4.A Spring bean factory can be initialized from any source: Default implementations are provided for XML, properties files, and programmatic registrations.
5.An application context is very quick to start up: You can start it in a single JUnit test, and you'll never notice the overhead.
The "spring-beans" jar file with just the bean container is very tiny (~90 KB in 1.0 M1). This jar even contains all of Spring's out-of-the-box bean factory implementations.
6.In short, Spring is such a lightweight container that you won't even notice it's there. It is as simple to handle as a library: It can be included in any application, running in any container, without deployment steps.

Spring轻量级容器:

什么是轻量级容器?

  • 对象在Spring中运行,不需要实现Spring的接口,因为其使用IoC帮你管理,你的对象不需要做任何操作,它就帮你完成装配工作,就像其他配置文件一样。

  • 几乎可以运行在所有环境中,只需要启动器实现Spring的ApplicationContext

  • 可以使用Aop拦截器增强类的行为,比如事务管理和安全校验

  • 支持多种配置

  • 快速启动

  • jar包很小,开箱即用

  • 可以无视它的存在,就像一个依赖包,引入后不用部署就可以运行在任何容器中

    总结起来就是耦合性低,开发难度低

什么是IoC

IoC这个词的来源

​现在我们到原文看下Spring的readme文件中提到IoC,IoC全称:Inversion-of-Control,翻译过来就是控制反转,使用过Spring的人应该不陌生,到底什么是控制反转?

* Powerful JavaBeans-based configuration management, applying Inversion-of-Control principles. This makes wiring up applications quick and easy. No more singletons littered throughout your codebase; no more arbitrary properties file. One consistent and elegant approach everywhere.

IoC-控制反转

接下来,我们看下不使用Spring和使用Spring来开发业务功能的区别。

class DemoService{
    private DemoDao dao;
    
    public void setDao(DemoDao dao){
        this.dao = dao;
    }
}


interface DemoDao{
}

class DemoDaoDefaultImpl implements DemoDao{
}
自己实现
class Demo{
    public static void main(String[] args){
        DemoService service = new DemoService();
        DemoDao dao = new DemoDaoDefaultImpl();
        service.setDao(dao);
      	// service CRUD
    }
}

需要自己创建对象,创建完之后还需要设置对象属性,类似于Spring的注入功能,整个过程是通过硬编码的方式完成。

接下来我们再看看Spring是怎么做的。

Spring实现
<beans>

  <bean id="demoDao" class="DemoDaoDefaultImpl"/>
    
  <bean id="demoService" class="DemoService">
    <property name="dao"><ref bean="demoDao"/></property>
  </bean>

</beans>

class Demo{
    public static void main(String[] args){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoService service = (DemoService)context.getBean("demoService");
        // service CRUD 业务逻辑
        
    }
}

现在假设,我们想要修改DemoDao的实现类,也就是不想用DemoDaoDefaultImpl,而是想要DemoDaoESImpl,如果是自己实现,需要修改创建DemoDao对象和设置DemoService的代码,如下:

class Demo{
    public static void main(String[] args){
        DemoService service = new DemoService();
        DemoDao dao = new DemoDaoESImpl();
        service.setDao(dao);
      	// service CRUD 业务逻辑
    }
}

而Spring则不同,只需要修改xml配置,修改DemoDaoESImpl类即可。于是Spring除了帮助我们创建对象之后,还维护着依赖关系,而这些都是在xml配置中完成,对象的创建和依赖关系是可配置的,想要替换对象,直接修改xml配置文件,不需要修改代码,达到解耦的效果,也就是对象的创建和业务逻辑解绑。

<beans>

  <bean id="demoDao" class="DemoDaoESImpl"/>
    
  <bean id="demoService" class="DemoService">
    <property name="dao"><ref bean="demoDao"/></property>
  </bean>

</beans>

​通过上面两个例子我们可以看到,没有Spring之前开发人员需要使用new创建对象然后给对象的成员变量赋值,有了Spring之后,只需要在配置文件中配置即可,创建对象(new)和属性赋值(set)等对象的管理工作交给了Spring。

​了解了Spring做了什么事情之后,我们来看看控制反转,原来创建对象是开发者自己做的,现在交给了Spring,简单理解就是创建和管理对象的控制权交给Spring,这就是我理解的IoC,说句不好听就是把脏活累活交给Spring,然后取个好听的名字 控制反转 ,谁干活谁就对”活”拥有控制权。

一言以蔽之:把管理对象的活交给Spring,然后取个高大上的名字:控制反转

​除了注入装配和生命周期由Spring管理之外,还有没有其他呢?看一看作者写的书**《Expert One-on-One J2EE Design and Development》**。英文,我也看不懂,那我们就用百度翻译看一下,有兴趣可以看看这本书。

第二期 2、初识Spring工厂

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值