2

I have an application using a framework that provides certain Spring beans via XML files in the framework. The configuration of my application is currently done partly in XML but mostly with Spring annotations.

Some of the XML bean definitions have parents referring to beans supplied by the framework, e.g.

<bean id="MyBean" parent="FrameworkBean">
    <property name="context">
        <map merge="true">
            <entry key="SomeKey" value-ref="SomeValue" />
        </map>
    </property>
</bean>

FramwworkBean is defined in an XML file in the framework. There is a chain of bean inheritance. At each step some entries are added to the context:

<bean id="FrameworkBean" parent="AbstractBean">
   <map merge="true">...

<bean id="AbstractBean" abstract="true" class="ClassWithContext"/>

I understand the result of all this is construction of a ClassWithContext instance with a map containing all the entries up the chain. Is it possible to write Java code to do the same, without duplicating code from the framework XML files?

@Bean("MyBean") ClassWithContext myBean() {
    return ??? // code that uses "FrameworkBean" somehow
}

The XML bean definition has no dependency on the type of AbstractBean. If MyBean can be created by Java code, can that code be written to be equally type-agnostic? Or should I just leave this in XML?

2
  • Sounds like you just need to do regular inheritance, via Java code... Is this correct? (Maybe this article will help: concretepage.com/spring/…) Commented Aug 17, 2017 at 21:10
  • @ochi: I clarified the question. I don't want to duplicate an artifact that I do not own and that could be changed by a new framework release. Commented Aug 17, 2017 at 22:32

1 Answer 1

1

If your "FrameworkBean" is not abstract bean you can try the following:

@Bean
public SomeType myBean(@Qualifier("FrameworkBean") FrameworkBeanType frameworkBean) {
    SomeType type = getSomeType();
    type.setFrameworkBean(frameworkBean);
    return type;
} 
Sign up to request clarification or add additional context in comments.

5 Comments

Clarified the question. This is not a good solution because it requires me to duplicate in Java the XML definition of FrameworkBean.
Update the answer with closer to actual request.
@kevincline, no you don't. Spring is smart enough to understand and handle both XML and Java-based configuration. If FrameworkBean has already been declared in the XML it will be autowired in Java without defining it as a separate @Bean
"FrameworkBean" is a descendant of an abstract parent.
Can you simply create another instance of FrameworkBean? You might want to make it @Primary to force spring to use your instance instead of the one created by your framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.