Tuesday 15 March 2011

Using prototype beans defined in a different java-based Spring config file -



Using prototype beans defined in a different java-based Spring config file -

assume have 2 spring config files: configa.java , configb.java.

here's how configa.java may like:

@configuration class configa { @scope("prototype") @bean public foo fooprototype() { homecoming new foo(params); } }

and want inject few instances of foo number of singleton-scoped beans declared in configb.java:

@configuration @import(configa.class) class configb { @bean public bar bar() { homecoming new bar(*** how inject foo instance here? ***); } @bean public buzz buzz() { homecoming new buzz(*** how inject foo instance here? ***); } }

if had single configuration file, replace blocks enclosed in asterisks fooprototype().

but, how inject different foo instances bar() , buzz() beans provided fooprototype() declared in different configuration file?

this looks similar illustration in spring documentation §5.12.5 composing java-based configurations.

this same page gives solution: can autowire configuration beans.

@configuration @import(configa.class) class configb { @autowired configa configa; @bean public bar bar() { homecoming new bar(configa.fooprototype()); } @bean public buzz buzz() { homecoming new buzz(configa.fooprototype()); } }

java spring spring-3

No comments:

Post a Comment