当前位置: 首页 > wzjs >正文

国家住房和城乡建设厅网站大连建设网站制作

国家住房和城乡建设厅网站,大连建设网站制作,微信公众号私自建设电影网站,最大的房产网站### 该方法说明 先说总结作用,该方法的作用就是在xml 文件中通过parent属性与指定的类进行绑定,当调用getMergedLocalBeanDefinition 方法时将parent指定的bean定义对象与当前bean定义对象合并,将合并的对象放入mergedBeanDefinitions这个集…

### 该方法说明

先说总结作用,该方法的作用就是在xml 文件中通过parent属性与指定的类进行绑定,当调用getMergedLocalBeanDefinition 方法时将parent指定的bean定义对象与当前bean定义对象合并,将合并的对象放入mergedBeanDefinitions这个集合中

```java
private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<>(256);
```

从名称上来看是合并的bean 定义对象,刚开始看的我一直不明白这个集合的作用,之后我了解到**Spring 允许通过配置让一个 Bean 定义继承另一个 Bean 定义**。例如我们要配置两个不同的数据库,分别是使用了不同的密码和账号,但是某些属性上是相同的通常我们会有如下配置:

```xml
<bean id="dataSource1" class="com.zaxxer.hikari.HikariDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db1"/>
    <property name="username" value="user1"/>
    <property name="password" value="pass1"/>
    <property name="maximumPoolSize" value="20"/>
    <property name="minimumIdle" value="5"/>
</bean>

<bean id="dataSource2" class="com.zaxxer.hikari.HikariDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db2"/>
    <property name="username" value="user2"/>
    <property name="password" value="pass2"/>
    <property name="maximumPoolSize" value="20"/>
    <property name="minimumIdle" value="5"/>
</bean>
```

spring 为了简化这个配置允许我们使用parent 属性实现bean定义的继承

```xml
<bean id="parentDataSource" class="com.zaxxer.hikari.HikariDataSource" abstract="true">
    <property name="maximumPoolSize" value="20"/>
    <property name="minimumIdle" value="5"/>
</bean>

<bean id="dataSource1" class="com.zaxxer.hikari.HikariDataSource" parent="parentDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db1"/>
    <property name="username" value="user1"/>
    <property name="password" value="pass1"/>
</bean>

<bean id="dataSource2" class="com.zaxxer.hikari.HikariDataSource" parent="parentDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db2"/>
    <property name="username" value="user2"/>
    <property name="password" value="pass2"/>
</bean>
```

<br/>

### 源码讲解

而通过getMergedLocalBeanDefinition 方法将会将这些bean定义的对象合并,具体代码如下,首先尝试从已合并的Bean定义对象集合中获取,如果能获取到直接返回否则尝试合并bean定义对象:

```java
// Quick check on the concurrent map first, with minimal locking.
// 先通过mergedBeanDefinitions获取Bean 对象
RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
if (mbd != null && !mbd.stale) {
    return mbd;
}
return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
```

getMergedBeanDefinition方法首先调用了当前方法的重写方法:

```java
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
        throws BeanDefinitionStoreException {

    return getMergedBeanDefinition(beanName, bd, null);
}
```

首先锁定了bean定义集合对象

```
synchronized (this.mergedBeanDefinitions)
```

由于containingBd对象传入的是一个null,所以这段代码忽略掉:

```java
if (containingBd == null) {
     mbd = this.mergedBeanDefinitions.get(beanName);
}
```

主要核心代码在这块,首先判断parentName是否为空:

```java
if (bd.getParentName() == null)
```

如果为空,表示不需要合并,直接创建一个 RootBeanDefinition对象:

```java
if (bd.getParentName() == null) {
    if (bd instanceof RootBeanDefinition rootBeanDef) {
      // cloneBeanDefinition方法是new RootBeanDefinition(this)
      // 相当于创建了一个根节点对象
        mbd = rootBeanDef.cloneBeanDefinition();
    } else {
      
        mbd = new RootBeanDefinition(bd);
    }
}
```

否则通过getMergedBeanDefinition(parentBeanName)方法获取parent属性的对象

```java
String parentBeanName = transformedBeanName(bd.getParentName());
if (!beanName.equals(parentBeanName)) {
    pbd = getMergedBeanDefinition(parentBeanName);
}
```

然后覆盖相关属性:

```java
mbd = new RootBeanDefinition(pbd);
mbd.overrideFrom(bd);
```

mbd = new RootBeanDefinition(pbd);这段代码的具体内容如下,其实就是将parent的bean定义对象的属性覆盖到子bean定义对象当中:

```java
setParentName(original.getParentName());
setBeanClassName(original.getBeanClassName());
setScope(original.getScope());
setAbstract(original.isAbstract());
setFactoryBeanName(original.getFactoryBeanName());
setFactoryMethodName(original.getFactoryMethodName());
setRole(original.getRole());
setSource(original.getSource());
copyAttributesFrom(original);

if (original instanceof AbstractBeanDefinition originalAbd) {
    if (originalAbd.hasBeanClass()) {
        setBeanClass(originalAbd.getBeanClass());
    }
    if (originalAbd.hasConstructorArgumentValues()) {
        setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
    }
    if (originalAbd.hasPropertyValues()) {
        setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
    }
    if (originalAbd.hasMethodOverrides()) {
        setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides()));
    }
    Boolean lazyInit = originalAbd.getLazyInit();
    if (lazyInit != null) {
        setLazyInit(lazyInit);
    }
    setAutowireMode(originalAbd.getAutowireMode());
    setDependencyCheck(originalAbd.getDependencyCheck());
    setDependsOn(originalAbd.getDependsOn());
    setAutowireCandidate(originalAbd.isAutowireCandidate());
    setPrimary(originalAbd.isPrimary());
    copyQualifiersFrom(originalAbd);
    setInstanceSupplier(originalAbd.getInstanceSupplier());
    setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed());
    setLenientConstructorResolution(originalAbd.isLenientConstructorResolution());
    setInitMethodNames(originalAbd.getInitMethodNames());
    setEnforceInitMethod(originalAbd.isEnforceInitMethod());
    setDestroyMethodNames(originalAbd.getDestroyMethodNames());
    setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod());
    setSynthetic(originalAbd.isSynthetic());
    setResource(originalAbd.getResource());
```

最后列出该方法的所有内容:

```java
    protected RootBeanDefinition getMergedBeanDefinition(
            String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)
            throws BeanDefinitionStoreException {

        synchronized (this.mergedBeanDefinitions) {
            RootBeanDefinition mbd = null;
            RootBeanDefinition previous = null;

            // Check with full lock now in order to enforce the same merged instance.
            if (containingBd == null) {
                mbd = this.mergedBeanDefinitions.get(beanName);
            }

            if (mbd == null || mbd.stale) {
                previous = mbd;
                if (bd.getParentName() == null) {
                    // Use copy of given root bean definition.
                    if (bd instanceof RootBeanDefinition rootBeanDef) {
                        mbd = rootBeanDef.cloneBeanDefinition();
                    }
                    else {
                        mbd = new RootBeanDefinition(bd);
                    }
                }
                else {
                    // Child bean definition: needs to be merged with parent.
                    BeanDefinition pbd;
                    try {
                        String parentBeanName = transformedBeanName(bd.getParentName());
                        if (!beanName.equals(parentBeanName)) {
                            pbd = getMergedBeanDefinition(parentBeanName);
                        }
                        else {
                            if (getParentBeanFactory() instanceof ConfigurableBeanFactory parent) {
                                pbd = parent.getMergedBeanDefinition(parentBeanName);
                            }
                            else {
                                throw new NoSuchBeanDefinitionException(parentBeanName,
                                        "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +
                                                "': cannot be resolved without a ConfigurableBeanFactory parent");
                            }
                        }
                    }
                    catch (NoSuchBeanDefinitionException ex) {
                        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
                                "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
                    }
                    // Deep copy with overridden values.
                    mbd = new RootBeanDefinition(pbd);
                    mbd.overrideFrom(bd);
                }

                // Set default singleton scope, if not configured before.
                if (!StringUtils.hasLength(mbd.getScope())) {
                    mbd.setScope(SCOPE_SINGLETON);
                }

                // A bean contained in a non-singleton bean cannot be a singleton itself.
                // Let's correct this on the fly here, since this might be the result of
                // parent-child merging for the outer bean, in which case the original inner bean
                // definition will not have inherited the merged outer bean's singleton status.
                if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                    mbd.setScope(containingBd.getScope());
                }

                // Cache the merged bean definition for the time being
                // (it might still get re-merged later on in order to pick up metadata changes)
                if (containingBd == null && isCacheBeanMetadata()) {
                    this.mergedBeanDefinitions.put(beanName, mbd);
                }
            }
            if (previous != null) {
                copyRelevantMergedBeanDefinitionCaches(previous, mbd);
            }
            return mbd;
        }
    }
```


文章转载自:

http://x3oliZDS.Lskyz.cn
http://lERMvTjr.Lskyz.cn
http://K5kJ5pvY.Lskyz.cn
http://NWU4Pndd.Lskyz.cn
http://gjpjzi95.Lskyz.cn
http://bTETsVYh.Lskyz.cn
http://cWT5Wc0o.Lskyz.cn
http://vlnGqT1K.Lskyz.cn
http://4DOBaQm8.Lskyz.cn
http://BNLdD1fJ.Lskyz.cn
http://i9bViEKf.Lskyz.cn
http://9dqg0XiC.Lskyz.cn
http://fluukxG7.Lskyz.cn
http://snO1cYeL.Lskyz.cn
http://AUuuyphL.Lskyz.cn
http://Sji1Jw1H.Lskyz.cn
http://o2Y3eCID.Lskyz.cn
http://jfK7UJbN.Lskyz.cn
http://TRfBv5xQ.Lskyz.cn
http://REmecb0f.Lskyz.cn
http://1uaoxQen.Lskyz.cn
http://by8QtqR1.Lskyz.cn
http://xCPishAB.Lskyz.cn
http://QFak4cAK.Lskyz.cn
http://D6AUFWm4.Lskyz.cn
http://2fUZfQoV.Lskyz.cn
http://0Ae9BxA3.Lskyz.cn
http://ny5JjsRx.Lskyz.cn
http://lA33P6XE.Lskyz.cn
http://zWgfRLGH.Lskyz.cn
http://www.dtcms.com/wzjs/665636.html

相关文章:

  • 特定ip段访问网站代码外包公司的业务员
  • 六十岁一级a做爰片免费网站怎么做微信小程序商城
  • html5 电商网站模板固戍网站建设
  • 网站首页欣赏企业网站的内容营销
  • 做自己的首席安全官的网站青岛市住房和城乡建设局官方网站
  • 一级a做爰片365网站家纺网站建设
  • 做网站的人跑了网站可以恢复吗网站推广优化排名教程
  • 优秀学习网站vps网站权限
  • 个人如何学习做网站如网站性质为公司 请以企业备案
  • 新乡网站建设哪家权威城市网站联盟
  • 网站建设公司排名前十建网站是什么专业类别
  • 做调研有哪些网站杭州百度推广代理商
  • 特步的网站建设策划网站模板含数据库下载
  • 重庆网站建设模板软件 行业门户网站
  • 工商网站如何做实名那里可以做工作室做网站
  • 苏州建网站要多少钱中国建设银行网站缴费系统
  • 网站内搜索关键字校园网站模版
  • jsp网站开发职位要求找事做网站怎么弄
  • 绍兴网站建设方案推广公司企业网站模板
  • 邢台网站优化平面设计比较好的网站
  • 章丘做网站哪家强wordpress后台密码忘记
  • 杭州响应式网站制作网站建设与维护教程
  • 广东网站设计费用做电影网站代理合法么
  • 做网站搭建环境防城港市网站建设
  • 小学网站建设工作小组购物网站设计图
  • 怎么做cc网站南充市住房和城乡建设厅网站
  • 医院网站和微信公众号建设网站建设阿胶膏的作用
  • 北方工业大学网站建设东莞网络推广托管
  • wordpress分类页seo牡丹江网站seo
  • 网站排名大全长沙专业网站优化定制