Is there any way to use <bean parent="someParent">
with @Component annotation (creating spring beans using annotation)?
I would like to create spring bean that has "spring parent" using @Component annotation.
is it possible?
Following my comment, this piece of XML
<bean id="base" abstract="true">
<property name="foo" ref="bar"/>
</bean>
<bean class="Wallace" parent="base"/>
<bean class="Gromit" parent="base"/>
is more or less eqivalent to this code (note that I created artificial Base
class since abstract beans in Spring don't need a class
):
public abstract class Base {
@Autowired
protected Foo foo;
}
@Component
public class Wallace extends Base {}
@Component
public class Gromit extends Base {}
Wallace
and Gromit
now have access to common Foo
property. Also you can override it, e.g. in @PostConstruct
.
BTW I really liked parent
feature in XML which allowed to keep beans DRY, but Java approach seems even cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With