interface FactoryBean<T> {
public T getObject() throws Exception;
public Class<?> getObjectType();
public default boolean isSingleton() { return true; }
}
Example
public class AppServiceFactoryBean implements FactoryBean<AppService> {
public AppService getObject() throws Exception {
...
return appService:
}
public Class<?> getObjectType() {
return AppService.class;
}
}
FactoryBeans with Java Configuration:
Spring calls getObject() automatically.
@Configuration
public class ServiceConfig {
@Bean
public AppServiceFactoryBean appService() {
return new AppServiceFactoryBean();
}
@Bean
public OtherService otherService(AppService appService) {
return new OtherService(appService);
}
}
Examples
Used in Spring by:
- EmbeddedDatabaseFactoryBean
- ProxyFactoryBean
- JndiObjectFactoryBean
- HibernateJpaSessionFactoryBean