I am failing to see the utility of FactoryBean for creating a bean...Why coudn't you create the respective bean in an Configuration class? What need is there for the FactoryBean ?
I know I'm missing something here but I can't figure out what exactly
I am failing to see the utility of FactoryBean for creating a bean...Why coudn't you create the respective bean in an Configuration class? What need is there for the FactoryBean ?
I know I'm missing something here but I can't figure out what exactly
Spring Configuration and FactoryBean classes have something similar but Configuration has not only option to produce beans for container. @Configuration was created to implement in java everything that was possible to do in xml and even more.
With FactoryBean you can:
1. Create beans using some custom logic
And that is all.
Ref: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html
With Configuration you can:
1. Declare beans without any creational logic.
2. Create beans for container using custom logic(the same as FactoryBean).
3. Declare static beans(see https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-value-annotations)
4. Declare @ComponentScan
5. Declare @PropertySource
6. Import another configuration from java config using @Import
7. Import another configuration from xml using @ImportResource
8. Configuration class may be imported for testing usages(as test config)
....a lot of other usages
Ref: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html
And the most significant difference is that @Configuration class is managed by Spring container and all its public methods are proxied. FactoryBean is not managed by Spring container, it is just a tool to create beans for container.
NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style, but the object exposed for bean references (getObject()) is always the object that it creates.....FactoryBean is a programmatic contract. Implementations are not supposed to rely on annotation-driven injection or other reflective facilities.