开发者

Configuring a spring bean (via XML) without repeating the class attribute [duplicate]

开发者 https://www.devze.com 2023-03-27 09:15 出处:网络
This question already has an answer here: 开发者_Go百科 How can I use the simple class name in Spring beans?
This question already has an answer here: 开发者_Go百科 How can I use the simple class name in Spring beans? (1 answer) Closed 5 years ago.

I need to create a lot of spring beans with the same class. Something like that :

<bean id="id1" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>

<bean id="id2" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>

<bean id="id3" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>

...

It would be more readable and pleasant to not have to copy the class attribute in each bean definition. Is there a way to avoid repeting x times the class ?


Try this:

<bean id="myLostClass" abstract="true" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass"/>

<bean id="id1" parent="myLostClass">
...
</bean>

<bean id="id2" parent="myLostClass">
...
</bean>

<bean id="id3" parent="myLostClass">
...
</bean>

Note that if you add some properties to a parent bean, then they will be automatically applied to all the children (convenient way to extract common properties).

0

精彩评论

暂无评论...
验证码 换一张
取 消