In Spring XML, I can define a bean that instantiates a class annotated with @Configuration. When I do, that bean is post-processed. Any methods inside that class with @Bean are also added to the container. How do I perform a similar post-processing in JavaConfig?
Here's the XML version:
<bean id="test" class="com.so.Test">
<property name="prop" value="set before instantiating @Beans defined in Test"/>
</bean>
The associated Test class:
@Configuration
class Test {
private String prop;
void setProp(final String prop) {
this.prop = prop;
}
@Bean
NeedThisBean needThisBeanToo() {
return new NeedThisBean(prop);
}
}
If I use Spring XML Config, both test
and needThisBeanToo
are available in the container. needThisBeanToo
is added via a BeanPostProcessor, though I can't recall which one. If I use JavaConfig, only test
is available in the container. How do I make needThisBeanToo
available to the container? @Import
would work, except that prop
being set is required for needThisBeanToo
to be initialized correctly.
The part that makes all of this complicated is that Test
is vended from a library I'm consuming. I don't control Test
, nor can I change it. If I drive it from JavaConfig, it would look like this:
@Configuration
class MyConfiguration
{
@Bean
Test test() {
Test test = new Test();
test.setProp("needed to init `needThisBeanToo` and others");
return test;
}
}
The JavaConfig example does not instantiate needThisBeanToo
despite it being defined in Test
. I need to get needThisBeanToo
defined, preferably without doing it myself, since I don't want to copy code I don't own. Delegation isn't attractive, since there are a number of subsequent annotations/scopes defined on needThisBeanToo
(and others defined inside Test
).