@Factory

Factory beans allow logic to be used when creating a bean. Often this logic is based on environment variables or system properties (e.g. AWS region).

This is equivalent to Spring DI @Configuration.

@Factory
class Configuration {

  private final StartConfig startConfig;

  /**
   * Factory beans can have dependencies.
   */
  @Inject
  Configuration(StartConfig startConfig) {
    this.startConfig = startConfig;
  }

  /**
   * Builder method.
   */
  @Bean
  FooBean buildFoo() {

    // maybe read System properties or environment variables
    return new FooBean(...);
  }

  /**
   * Builder method with dependencies as method parameters.
   */
  @Bean
  BarBean buildBar(FooBean foo, CoffeeMaker maker) {

    // maybe read System properties or environment variables
    return new BarBean(...);
  }
}