Defining bean in xml without class attribute

陌路散爱 提交于 2020-01-01 12:07:09

问题


I am a newbie to the world of Spring. In an interview, it was asked if we can create a bean in XML without specifying the class (that is, the bean would only have an id attribute). I did not have the answer to this. Please advise if we can create a bean in XML in Spring without specifying the class attribute and under which conditions we would normally do this.


回答1:


Spring documentation makes it quite clear:

<!--
    Each bean definition must specify the fully qualified name of the class,
    except if it pure serves as parent for child bean definitions.
-->
<!ATTLIST bean class CDATA #IMPLIED>

This section explains it in detail and provides useful examples.




回答2:


As described by Szymon, bean definition inheritance is one way. Schema based configuration is another way, see. For example, taken from Spring's documentation,

<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails">
    <value>pechorin@hero.org</value>
    <value>raskolnikov@slums.org</value>
    <value>stavrogin@gov.org</value>
    <value>porfiry@gov.org</value>
</util:list>


来源:https://stackoverflow.com/questions/15220957/defining-bean-in-xml-without-class-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!