spring 3 解读 (笔记一期)

2023-12-05 03:32
文章标签 java 笔记 spring 解读 一期

本文主要是介绍spring 3 解读 (笔记一期),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 spring 3 -- 概念篇

 

笔者先用svn获取spring3的源代码:

其代码地址是:  https://src.springframework.org/svn/spring-framework/

 

Spring3源代码的组织结构:

build-spring-framework是整个源代码的构建目录,里面是项目的构建脚本,若要自己动手构建Spring,可以进入这个目录使用ant进行构建。

其他目录从名字上可以看得出Spring的各个组成部份,比如:

org.springframework.context  是Ioc容器源代码目录,

org.springframework.aop 是Aop实现的源代码目录,

 

Spring为我们提供的主要解决方案就是 IoC容器 AOP 支持 :

 

 IoC容器系列:

总接口类BeanFactory 是IoC容器的功能规范,提供IoC容器的最基本功能规范。

org.springframework.beans.factory
Interface BeanFactory

All Known Subinterfaces:
ApplicationContext, AutowireCapableBeanFactory, ConfigurableApplicationContext, ConfigurableBeanFactory, ConfigurableListableBeanFactory, ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, WebApplicationContext
All Known Implementing Classes:
AbstractApplicationContext, AbstractAutowireCapableBeanFactory, AbstractBeanFactory, AbstractRefreshableApplicationContext, AbstractRefreshableConfigApplicationContext, AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AbstractXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, ClassPathXmlApplicationContext, DefaultListableBeanFactory, FileSystemXmlApplicationContext, GenericApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, SimpleJndiBeanFactory, StaticApplicationContext, StaticListableBeanFactory, StaticPortletApplicationContext, StaticWebApplicationContext, XmlBeanFactory, XmlPortletApplicationContext, XmlWebApplicationContext

在这些Spring提供的基本IoC容器中的接口和实现的基础上,Spring通过定义BeanDefinition来管理基于Spring的应用中的各种对象以及他们之间的依赖关系。

BeanDefinition抽象了我们对Bean的定义,是让容器起作用的主要数据类型,IoC的核心数据类型。

(用户使用容器时,可以使用转义字符 & 来得到FactoryBean本身,用来区分通过容器来获取FactoryBean产生的对象和获取FactoryBean本身。的Spring中所有的Bean都是由BeanFactory(IoC容器)来进行管理的,但对FactoryBean而言,这个Bean不是简单的Bean,他的实现与设计模式中的工厂模式装饰器模式 类似。)

BeanFactory方法:

booleancontainsBean(String name)
          Does this bean factory contain a bean with the given name? More specifically, isgetBean(java.lang.String) able to obtain a bean instance for the given name?
 String[]getAliases(String name)
          Return the aliases for the given bean name, if any.
<T> T
getBean(Class<T> requiredType)
          Return the bean instance that uniquely matches the given object type, if any.
 ObjectgetBean(String name)
          Return an instance, which may be shared or independent, of the specified bean.
<T> T
getBean(String name,Class<T> requiredType)
          Return an instance, which may be shared or independent, of the specified bean.
 ObjectgetBean(String name,Object... args)
          Return an instance, which may be shared or independent, of the specified bean.
 Class<?>getType(String name)
          Determine the type of the bean with the given name.
 booleanisPrototype(String name)
          Is this bean a prototype? That is, will getBean(java.lang.String) always return independent instances?
 booleanisSingleton(String name)
          Is this bean a shared singleton? That is, will getBean(java.lang.String) always return the same instance?
 booleanisTypeMatch(String name,Class targetType)
          Check whether the bean with the given name matches the specified type.

 

IoC容器XmlBeanFactroy的实现原理:

org.springframework.beans.factory.xml
Class XmlBeanFactory

java.lang.Objectextended by org.springframework.core.SimpleAliasRegistryextended by org.springframework.beans.factory.support.DefaultSingletonBeanRegistryextended by org.springframework.beans.factory.support.FactoryBeanRegistrySupportextended by org.springframework.beans.factory.support.AbstractBeanFactoryextended by org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactoryextended by org.springframework.beans.factory.support.DefaultListableBeanFactoryextended by org.springframework.beans.factory.xml.XmlBeanFactory

 

此IoC容器可以读取以XML形式定义的BeanDefinition。在XmlBeanFactroy中,初始化中一个XmlBeanDefinitionReader对象,有了这个Reader对象,那些以XMl的方式定义的BeanDefinition就有了处理的地方,对这些XML形式的信息的处理是有这个XmlBeanDefinitionReader来完成的。

XmlBeanFactory的功能是建立在DfaultListableBeanFactory这个基本容器的基础上的,在这个容器的基础实现上实现了其他诸如XMl读取的附加功能。

XmlBeanFactory的实现源代码:

 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
 /**
  * Create a new XmlBeanFactory with the given resource,
  * which must be parsable using DOM.
  * @param resource XML resource to load bean definitions from
  * @throws BeansException in case of loading or parsing errors
  */
 public XmlBeanFactory(Resource resource) throws BeansException {
  this(resource, null);
 }

 /**
  * Create a new XmlBeanFactory with the given input stream,
  * which must be parsable using DOM.
  * @param resource XML resource to load bean definitions from
  * @param parentBeanFactory parent bean factory
  * @throws BeansException in case of loading or parsing errors
  */
 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
  super(parentBeanFactory);
  this.reader.loadBeanDefinitions(resource);
 }


 

举例 编程式 使用IoC容器:

ClassPathResource res = new ClassPathResource("beans.xml");

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

XmlBeanFactroyReader reader = new XmlBeanFactroyReader(factory);

reader.loadBeanfinitions(res);

这样,我们就可以通过factory对象来使用DefaultListableBeanFactory这个IoC容器。在使用IoC容器,需要以下步骤:

1)创建IoC配置文件的抽象资源,此抽象资源包含了BeanDefinition的定义信息;

2)创建一个BeanFactory;

3)创建一个载入BeanDefinition的读取器;

4)成定义号的资源位置读入配置信息

 

 

ApplicaitonContext

扩展BeanFactory的 高级容器

org.springframework.context
Interface ApplicationContext

All Superinterfaces:
ApplicationEventPublisher, BeanFactory, HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader, ResourcePatternResolver
All Known Subinterfaces:
ConfigurableApplicationContext, ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext, WebApplicationContext
All Known Implementing Classes:
AbstractApplicationContext, AbstractRefreshableApplicationContext, AbstractRefreshableConfigApplicationContext, AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AbstractXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, GenericApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, StaticApplicationContext, StaticPortletApplicationContext, StaticWebApplicationContext, XmlPortletApplicationContext, XmlWebApplicationContext

ApplicaitonContext 提供了BeanFactory不具备的功能

 

 

 

IoC容器初次化:

 IoC容器的初始化包括BeanDefinition的Resource定位载入注册三个基本的过程。

Spring在实现中把这三个过程分开并使用不同的模块来完成,这样可以让用户更加灵活地对这三个过程进行减少或者扩展,定义出适合自己的IoC容器。


BeanDefinition的资料定位由ResourceLoader通过统一的Resource接口来完成,这个Resource对种种形式的BeanDefinition的使用提供了统一接口。对于这些BeanDefinition的存在形式,比如,在文件系统中的Bean定义信息可以使用FileSystemResource来进行抽象;在类路径中可以使用ClassPathResource来进行抽象,等等。这个过程类似于容器寻找数据的过程,就像用水桶装水先要把水找到一样。


第二个关键的部分是BeanDefinition的载入,此载入过程把用户定义好的Bean表示为IoC容器内部的数据结构,而这个容器内部的数据结构就是BeanDefinition。总的来说,这个BeanDefinition实际上就是POJO对象在IoC容器中的抽象,这个BeanDefinition定义了一系列的数据来使用IoC容器能够方便地对POJO对象也就是Spring的Bean进行管理 。即BeanDefinition就是Spring的领域对象。


第三个过程是向IoC容器注册这些BeanDefinition的过程。这个过程是通过调用BeanDefinition接口的实现来完成的,这个注册过程把载入过程中解析得到的BeanDefinition向IoC容器进行注册。可以看到在IoC容器内部,是通过使用一个HashMap来持有这些BeanDefinition数据的。


IoC容器的上下文的初始化一般不包含Bean依赖注入的实现。一般而言,依赖注入发生在应用第一次向容器通过getBean索取Bean时。但有一个例外,在使用IoC容器时有一个预实例化的配置,这个预实例化是可以配置的,具体来说可以通过在Bean定义信息中的lazyinit属性来设定;有了这个预实例化的特性,用户可以对容器的初始化过程作一个控制;从而改变这个被设置了lazyinit属性的Bean的依赖注入的发生,使得这个Bean的依赖注入在IoC容器初始化时就预先完成了。





 

 

 

 

 

 

 

这篇关于spring 3 解读 (笔记一期)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/455988

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S