Reputation: 18961
With Spring Configuration class i.e without using XML based configurations. Is it possible to Inject the factory method like in Guice ?
In Guice if I had a provider method like
@Provides
public DatastoreService datastoreService(String url) { // Spring will inject this method parameter, if it is mapped of course.
return DatastoreServiceFactory(url);
}
Is this possible with Spring ?
Like
@Bean
public Performer poeticDuke(Poem poem) {
return new PoeticJuggler(poem);
}
rather than:
@Bean
public Performer poeticDuke() {
return new PoeticJuggler(sonnet29());
}
This is from the book Spring in Action 3
Upvotes: 1
Views: 61
Reputation: 64011
Yes that will work as long as the argument to the method is also a Spring bean!
Check out one of the many examples used in Spring Boot like this, this or this
Upvotes: 1
Reputation: 115328
Yes, sure it is possible if Poem
is bean too. Otherwise it seems strange: where can Spring find Poem
otherwise?
Upvotes: 1