spring xml配置与注解配置 parent属性会影响scope吗?

当使用XML配置时,遇上父类子类继承问题,parent属性会不会影响该bean的默认scope值呢?

以下展示的是纯XML配置或者纯注解配置时的情况

 

1、XML配置

<bean id="father" class="com.Father" abstract="true" scope="prototype"/>
<bean id="son" class="com.Son" parent="father"/>
public abstract  class Father {
}
public class Son extends Father{
}

这种不使用任何注解的类,仅使用纯XML配置时,parent会影响bean的scope属性。

若子类不显式声明scope,此时默认的不是单例,而是会受parent的scope影响。

若father scope为prototype,则<bean id="son" class="com.Son" parent="father"/>的结果也为多例。

 

2、纯注解配置

当使用注解时,类的scope仅与自身声明有关。如下

  <context:component-scan base-package="com.**">

    </context:component-scan>
<!--<bean id="father" class="com.Father" abstract="true" scope="prototype"/>
       <bean id="son" class="com.Son" parent="father"/>-->

@Component
@Scope("prototype")
public abstract  class Father {
}

@Component
public class Son extends Father{
}

此时son默认为单例,除非显示声明为多例才为多例

 

这种现象与spring版本无关,试过从3.0到5.0的spring版本,都得出相同结论。

如果有这方面的学习资料请留言哈哈,欢迎探讨。

虽然现在springboot都在废掉XML配置了,但对这块还是想弄清楚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值