- 浏览: 62034 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
2140345:
拿着Spring的源码一抄,翻译软件翻译注释,就叫源码分析了, ...
Spring IOC ( 一 ) : DefaultSingletonBeanRegistry源码分析 -
激情成就梦想:
不错哦 很有意义!
我最近笔试的Java题目 -
jy0902:
不错 正在运用分析。好好学习!
Spring-MVC 源码分析(二):DispacherServlet 初始化
public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable { /** * 常数为默认作用域的名称:“”,相当于单身状态,但是从父bean定义(如适用)所覆盖。 */ public static final String SCOPE_DEFAULT = ""; /** * 常数,指示没有自动装配。 */ public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO; /** * 常数,表示按name自动装配bean的属性。 */ public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; /** * 常数,指示按类型自动装配bean的属性。 */ public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE; /** * 常数,表示自动装配一个构造。 */ public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; /** * 常数指示不依赖检查。 */ public static final int DEPENDENCY_CHECK_NONE = 0; /** * 常数,表示为对象引用的依赖检查。 */ public static final int DEPENDENCY_CHECK_OBJECTS = 1; /** * 常数,指出“简单”的属性的依赖检查。 */ public static final int DEPENDENCY_CHECK_SIMPLE = 2; /** * 常数,指示所有属性的依赖检查 */ public static final int DEPENDENCY_CHECK_ALL = 3; //应该是这个bean的Class类型吧 private volatile Object beanClass; //默认scope为“”,相当于单例 private String scope = SCOPE_DEFAULT; //bean默认为单例的 private boolean singleton = true; //bean默认不是原型的 private boolean prototype = false; //bean默认不是抽象的 private boolean abstractFlag = false; //bean默认不开启延迟初始化 private boolean lazyInit = false; //bean默认自动装配功能是关闭的 private int autowireMode = AUTOWIRE_NO; //bean的默认依赖检查方式 private int dependencyCheck = DEPENDENCY_CHECK_NONE; //这个bean要初始化依赖的bean名称数组 private String[] dependsOn; //自动装配候选者 private boolean autowireCandidate = true; //默认不是主要候选者 private boolean primary = false; //不太清楚 private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<String, AutowireCandidateQualifier>(0); //允许访问非公开的构造器和方法 private boolean nonPublicAccessAllowed = true; //不太清楚 private boolean lenientConstructorResolution = true; // 构造参数持有者 private ConstructorArgumentValues constructorArgumentValues; // 一个或多个属性的持有者 private MutablePropertyValues propertyValues; // 方法重写的持有者 private MethodOverrides methodOverrides = new MethodOverrides(); // 这个bean的工厂bean名称 private String factoryBeanName; // 这个bean的工厂方法名 private String factoryMethodName; //这个bean的初始化方法名 private String initMethodName; //这个bean的销毁方法名 private String destroyMethodName; //默认执行初始化方法 private boolean enforceInitMethod = true; //默认执行销毁方法 private boolean enforceDestroyMethod = true; // 不太清楚 private boolean synthetic = false; //暗示角色是应用程序的重要组成部分 private int role = BeanDefinition.ROLE_APPLICATION; //这个bean的描述 private String description; //这个bean定义的资源 private Resource resource; /** * 创建新的AbstractBeanDefinition实例并给定ConstructorArgumentValues实例和MutabePropertyValues实例 */ protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) { setConstructorArgumentValues(cargs); setPropertyValues(pvs); } /** * 创建一个新的AbstractBeanDefinition并深入拷贝给定的BeanDefinition实例 */ protected AbstractBeanDefinition(BeanDefinition original) { // 抽象方法,由子类实现,设置父类名 setParentName(original.getParentName()); // 设置这个bean的类名称 setBeanClassName(original.getBeanClassName()); // 设置这个bean的工厂bean名称 setFactoryBeanName(original.getFactoryBeanName()); // 设置这个bean的工厂方法名称 setFactoryMethodName(original.getFactoryMethodName()); // 设置这个bean的作用范围,如单例的还是原型的,也有可能是其它的 setScope(original.getScope()); // 设置这个bean是否是抽象的 setAbstract(original.isAbstract()); // 设置这个bean是否开启延载初始化 setLazyInit(original.isLazyInit()); // 设置这个bean的角色 setRole(original.getRole()); // 设置这个bean的构造参数持有者 setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); // 设置这个bean的Property持有者 setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); // 设置这个bean的配置源 setSource(original.getSource()); // 这个bean复制属性组名称 copyAttributesFrom(original); //判断origina是否是AbstractBeanDefinition子类 if (original instanceof AbstractBeanDefinition) { AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original; if (originalAbd.hasBeanClass()) { // 设置这个bean的Class类型 setBeanClass(originalAbd.getBeanClass()); } // 设置这个bean的自动装配模式 setAutowireMode(originalAbd.getAutowireMode()); // 设置这个bean的依赖检查 setDependencyCheck(originalAbd.getDependencyCheck()); // 设置这个bean初始化要依赖的bean名称数组 setDependsOn(originalAbd.getDependsOn()); // 设置这个bean是否自动装配候选 setAutowireCandidate(originalAbd.isAutowireCandidate()); // copyQualifiersFrom(originalAbd); // 设置这个bean是否是主要候选者 setPrimary(originalAbd.isPrimary()); // 设置是否允许访问非public的构造器和方法 setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed()); //不清楚 setLenientConstructorResolution(originalAbd.isLenientConstructorResolution()); // 设置这个bean的初始化方法名 setInitMethodName(originalAbd.getInitMethodName()); // 设置是否执行初始化方法 setEnforceInitMethod(originalAbd.isEnforceInitMethod()); // 设置这个bean的销毁方法名 setDestroyMethodName(originalAbd.getDestroyMethodName()); // 设置是否执行销毁方法 setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod()); // 设置这个bean的方法重写持有者 setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides())); // 设置这个bean是人造的或者是应用程序本身 setSynthetic(originalAbd.isSynthetic()); // setResource(originalAbd.getResource()); } else { setResourceDescription(original.getResourceDescription()); } } /** * Override settings in this bean definition (assumably a copied parent * from a parent-child inheritance relationship) from the given bean * definition (assumably the child). * <ul> * <li>Will override beanClass if specified in the given bean definition. * <li>Will always take <code>abstract</code>, <code>scope</code>, * <code>lazyInit</code>, <code>autowireMode</code>, <code>dependencyCheck</code>, * and <code>dependsOn</code> from the given bean definition. * <li>Will add <code>constructorArgumentValues</code>, <code>propertyValues</code>, * <code>methodOverrides</code> from the given bean definition to existing ones. * <li>Will override <code>factoryBeanName</code>, <code>factoryMethodName</code>, * <code>initMethodName</code>, and <code>destroyMethodName</code> if specified * in the given bean definition. * </ul> */ public void overrideFrom(BeanDefinition other) { if (StringUtils.hasLength(other.getBeanClassName())) { setBeanClassName(other.getBeanClassName()); } if (StringUtils.hasLength(other.getFactoryBeanName())) { setFactoryBeanName(other.getFactoryBeanName()); } if (StringUtils.hasLength(other.getFactoryMethodName())) { setFactoryMethodName(other.getFactoryMethodName()); } if (StringUtils.hasLength(other.getScope())) { setScope(other.getScope()); } setAbstract(other.isAbstract()); setLazyInit(other.isLazyInit()); setRole(other.getRole()); getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues()); getPropertyValues().addPropertyValues(other.getPropertyValues()); setSource(other.getSource()); copyAttributesFrom(other); if (other instanceof AbstractBeanDefinition) { AbstractBeanDefinition otherAbd = (AbstractBeanDefinition) other; if (otherAbd.hasBeanClass()) { setBeanClass(otherAbd.getBeanClass()); } setAutowireCandidate(otherAbd.isAutowireCandidate()); setAutowireMode(otherAbd.getAutowireMode()); copyQualifiersFrom(otherAbd); setPrimary(otherAbd.isPrimary()); setDependencyCheck(otherAbd.getDependencyCheck()); setDependsOn(otherAbd.getDependsOn()); setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed()); setLenientConstructorResolution(otherAbd.isLenientConstructorResolution()); if (StringUtils.hasLength(otherAbd.getInitMethodName())) { setInitMethodName(otherAbd.getInitMethodName()); setEnforceInitMethod(otherAbd.isEnforceInitMethod()); } if (StringUtils.hasLength(otherAbd.getDestroyMethodName())) { setDestroyMethodName(otherAbd.getDestroyMethodName()); setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod()); } getMethodOverrides().addOverrides(otherAbd.getMethodOverrides()); setSynthetic(otherAbd.isSynthetic()); setResource(otherAbd.getResource()); } else { setResourceDescription(other.getResourceDescription()); } } /** * 套用这个bean提供的默认值。 */ public void applyDefaults(BeanDefinitionDefaults defaults) { setLazyInit(defaults.isLazyInit()); setAutowireMode(defaults.getAutowireMode()); setDependencyCheck(defaults.getDependencyCheck()); setInitMethodName(defaults.getInitMethodName()); setEnforceInitMethod(false); setDestroyMethodName(defaults.getDestroyMethodName()); setEnforceDestroyMethod(false); } //************************各个属性信息的get/set方法这里忽略***********************// /** * Set the originating (e.g. decorated) BeanDefinition, if any. */ public void setOriginatingBeanDefinition(BeanDefinition originatingBd) { this.resource = new BeanDefinitionResource(originatingBd); } public BeanDefinition getOriginatingBeanDefinition() { return (this.resource instanceof BeanDefinitionResource ? ((BeanDefinitionResource) this.resource).getBeanDefinition() : null); } /** * Validate this bean definition. * @throws BeanDefinitionValidationException in case of validation failure */ public void validate() throws BeanDefinitionValidationException { if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) { throw new BeanDefinitionValidationException( "Cannot combine static factory method with method overrides: " + "the static factory method must create the instance"); } if (hasBeanClass()) { prepareMethodOverrides(); } } /** * Validate and prepare the method overrides defined for this bean. * Checks for existence of a method with the specified name. * @throws BeanDefinitionValidationException in case of validation failure */ public void prepareMethodOverrides() throws BeanDefinitionValidationException { // Check that lookup methods exists. MethodOverrides methodOverrides = getMethodOverrides(); if (!methodOverrides.isEmpty()) { for (MethodOverride mo : methodOverrides.getOverrides()) { prepareMethodOverride(mo); } } } /** * Validate and prepare the given method override. * Checks for existence of a method with the specified name, * marking it as not overloaded if none found. * @param mo the MethodOverride object to validate * @throws BeanDefinitionValidationException in case of validation failure */ protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException { int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName()); if (count == 0) { throw new BeanDefinitionValidationException( "Invalid method override: no method with name '" + mo.getMethodName() + "' on class [" + getBeanClassName() + "]"); } else if (count == 1) { // Mark override as not overloaded, to avoid the overhead of arg type checking. mo.setOverloaded(false); } } /** * Clone this bean definition. * To be implemented by concrete subclasses. * @return the cloned bean definition object */ public abstract AbstractBeanDefinition cloneBeanDefinition(); }
发表评论
-
Spring IOC ( 五) :DefaultListableBeanFactory
2011-10-13 23:18 0... -
Spring IOC ( 四 ) : BeanDefinition( 转载 )
2011-09-06 22:49 1363BeanDefinition org.springfram ... -
Spring IOC ( 二) : FactoryBean接口(转载)
2011-08-29 01:59 1196原网址:http://weilingfeng98.iteye. ... -
Spring IOC ( 一 ) : DefaultSingletonBeanRegistry源码分析
2011-08-28 16:32 6525这是DefaultSingletonBeanRegistr ... -
BeanFactory类
2011-08-23 02:22 1283/* * Copyright 2002-2010 t ...
相关推荐
本文深入探讨了Spring框架中IoC容器的源码机制,涵盖了容器的初始化、Bean工厂的实例化、Bean定义的读取及Spring Bean的生命周期管理。通过精细的分析,本文揭示了AnnotationConfigApplicationContext的实例化过程,...
spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: spring-aop:面向切面编程,CGLB,JDKProxy spring-aspects:集成AspectJ,Aop应用框架 spring-instrume
springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC...
这是spring_ioc部分的内容。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...
Spring IoC源码深度剖析开源架构源码2021.pdf
Spring源码分析_Spring_IOC
一部分详细分析了Spring的核心:IoC容器和AOP的实现,能帮助读者了解Spring的运行机制;第二部分深入阐述了各种基于IoC容器和AOP的Java EE组件在Spring中的实现原理;第三部分讲述了ACEGI安全框架、DM模块以及Flex...
本文深入探讨了Spring IoC容器的加载过程及其源码实现,揭示了Spring中最为根本的概念之一。这包括从AnnotationConfigApplicationContext的实例化开始,到DefaultListableBeanFactory工厂的建立,再到...
Spring IOC:找对象的一次革命(1)
Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理 Spring源代码解析(七):Spring AOP中对拦截器...
SpringIOC源码解说,以及对应的代码解读。
spring version: 5.0.0; jdk: 1.8 IOC大致调用顺序(IOC调用的AOP标签解析)
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合
spring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demo...
个人整理SpringIOC源码解读,望大婶们指点指点,后续继续添加
SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码
spring Ioc容器配置 IOC容器数据源配置 <!-- 配置数据源 --> destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root ...
你不仅能从木书中参透Spring框架的优秀架构和设计思想,而且还能从Spring优雅的实现源码中一窥Java语言的精髓。此外,《Spring技术内幕:深入解析Spring架构与设计原理》还展示了阅读源代码的卓越方法,不仅授你以鱼...
第一部分详细分析了Spring的核心:IoC容器和AOP的实现,能帮助读者了解Spring的运行机制;第二部分深入阐述了各种基于IoC容器和AOP的Java EE组件在Spring中的实现原理;第三部分讲述了ACEGI安全框架、DM模块以及Flex...