gpt4 book ai didi

java - Spring beans 认识

转载 作者:行者123 更新时间:2023-11-29 05:24:33 30 4
gpt4 key购买 nike

spring bean文件中的extends和parent属性有什么用。它是否与扩展另一个类的类有关。如果有人可以就此分享一些想法,那就太好了。一些链接和示例也会有所帮助。

最佳答案

abstractparent 机制用于保持您的 XML 配置 DRY(不要重复自己)。

假设您有 2 个具有 3 个相似属性和 2 个不同属性的 bean。

您可以这样做,而不是在两个 bean 中重复这 3 个相似的属性:

  • 制作一个 抽象 的 bean 并拥有这 3 个通用属性。
  • 在您的 2 个 bean 上设置 parent 属性,以指向抽象 bean。

一个例子是here .

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean>

<bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>

</beans>

关于java - Spring beans 认识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193781/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com