Code that chooses, at startup, which modules of beans to add.
Use it when a whole group of beans depends on the configuration, for
example S3 storage or disk storage depending on a property. Each
alternative is a BeanModule (a @ComponentScan of its own
directory), and the selector returns the ones to load. Most
applications use @Profile or conditions instead.
Put the class in @Import([...]) (it needs a no-argument constructor)
or pass an instance to context.register(...). It may implement
EnvironmentAware, BeanFactoryAware and ResourceLoaderAware; those
are called before selectImports. It runs while the importing class is
parsed, once that class's parse-phase conditions have matched. The
selected modules are parsed at once, and their classes are read before
the importing class, with their own conditions. They are not registered
when the importing class is skipped in the registerBean phase, unless
another kept class imports them too.
// The storage beans follow the `storage` property.
class StorageSelector implements ImportSelector, EnvironmentAware {
late Environment _environment;
@override
void setEnvironment(Environment environment) =>
_environment = environment;
@override
List<BeanModule> selectImports() =>
_environment.getProperty('storage') == 's3' ? [s3Beans] : [diskBeans];
}
Spring (Java):
class StorageSelector implements ImportSelector, EnvironmentAware {
public String[] selectImports(AnnotationMetadata metadata) {
return new String[] {"s3".equals(env.getProperty("storage"))
? S3Config.class.getName() : DiskConfig.class.getName()};
}
}
Unlike Spring it returns modules (there is no class loading by name) and gets no annotation metadata of the importing class.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
selectImports(
) → List< BeanModule> - Returns the modules to load; an empty list adds nothing.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited