4

How I can provide bean inheritance in Spring using annotations? In XML config I used <parent="parentBean"> tag. Is there some annotation?

For example, I have two beans(cacheEventLogger extends fileEventLogger):

 <bean id="fileEventLogger" class="com.myuspring.core.loggers.FileEventLogger" init-method="init">
    <constructor-arg value="d:/1.txt"/>
</bean>


<bean id="cacheEventLogger" class="com.myspring.core.loggers.CacheFileEventLogger" init-method="init"
      parent="fileEventLogger" >
    <constructor-arg value="15"/>
    <property name="cacheSize" value="2"/>

I've create AppConfig class:

@Configuration

public class AppConfig {

@Bean(initMethod = "init")
public FileEventLogger fileEventLogger() {
    return new FileEventLogger("d:/1.txt");
}

@Bean(initMethod = "init")
public CacheFileEventLogger cacheFileEventLogger() {
   ???

}

}

What annotation should I set to cacheEventLogger for extending fileEventLogger?

2 Answers2

2

Spring annotations doesn't have an equivalent of spring XML parent tag/attribute. There is a workaround specified in the JIRA with some custom coding.

Refer to the related spring JIRA https://jira.spring.io/browse/SPR-5580 and a reference to stackoverflow answer from this JIRA Bean definition inheritance with annotations?

I think this is what you are looking for.

Community
  • 1
  • 1
1

There's no such parent property for the Spring-specific annotations, because the Java language provides everything needed (i.e inheritance, abstraction) to create a template and use it as a parent for some Spring-beans.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147