FactoryBean<T extends Object> class
abstract
A bean whose job is to make another object: the container hands out what getObject returns, not the factory itself.
Use it when building the object takes logic that a library wants to
keep behind one bean: a client set up from many settings, a proxy, an
object from a builder API. In application code a @Bean method is
usually simpler.
Looking the bean up by name or by the product type returns the product;
the factory itself is available under &beanName or by its own type.
The product of a singleton factory is created once and cached. Products
get postProcessAfterInitialization applied, but no injection or init
callbacks (like in Spring).
The generator recognizes FactoryBean subclasses and fills in
BeanDefinition.factoryObjectTypes; by hand it looks like this:
// An HttpClient that every bean gets already configured.
class ClientFactory extends FactoryBean<HttpClient> {
@override
HttpClient getObject() => HttpClient()..idleTimeout = Duration.zero;
}
final definition = BeanDefinition<ClientFactory>(
name: 'client',
types: const [ClientFactory, FactoryBean<HttpClient>],
factoryObjectTypes: const [HttpClient],
create: (_) => ClientFactory(),
);
factory.registerBeanDefinition(definition);
factory.getBean<HttpClient>(); // the product
factory.getBeanByName('client'); // the product
factory.getBeanByName('&client'); // the ClientFactory itself
Spring (Java):
@Component("client")
class ClientFactory implements FactoryBean<HttpClient> {
@Override
public HttpClient getObject() { return HttpClient.newHttpClient(); }
@Override
public Class<?> getObjectType() { return HttpClient.class; }
}
HttpClient client = context.getBean(HttpClient.class);
ClientFactory factory = (ClientFactory) context.getBean("&client");
The product types of a definition are known at build time from the
type argument; objectType (Spring's getObjectType()) is used for a
factory given to registerSingleton.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isSingleton → bool
-
Whether getObject returns the same instance every time; the
container then caches it. Override with
falsefor a new product on every lookup (Spring'sisSingleton()).no setter - objectType → Type
-
The product type,
Tby default (Spring'sgetObjectType()): a factory registered withregisterSingletonis found by it.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getObject(
) → T - Creates (or returns) the product.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited