A factory is a class annotated with the annotation that provides 1 or more methods annotated with a bean scope annotation. Which annotation you use depends on what scope you want the bean to be in. See the section on bean scopes for more information.

    The return types of methods annotated with a bean scope annotation are the bean types. This is best illustrated by an example:

    1. class CrankShaft {
    2. }
    3. class V8Engine implements Engine {
    4. final int cylinders = 8
    5. final CrankShaft crankShaft
    6. V8Engine(CrankShaft crankShaft) {
    7. this.crankShaft = crankShaft
    8. }
    9. String start() {
    10. "Starting V8"
    11. }
    12. }
    13. @Factory
    14. class EngineFactory {
    15. @Singleton
    16. Engine v8Engine(CrankShaft crankShaft) {
    17. new V8Engine(crankShaft)
    18. }
    19. }
    1. @Singleton
    2. internal class CrankShaft
    3. internal class V8Engine(private val crankShaft: CrankShaft) : Engine {
    4. private val cylinders = 8
    5. override fun start(): String {
    6. return "Starting V8"
    7. }
    8. }
    9. @Factory
    10. internal class EngineFactory {
    11. @Singleton
    12. fun v8Engine(crankShaft: CrankShaft): Engine {
    13. return V8Engine(crankShaft)
    14. }
    15. }

    In this case, the V8Engine is built by the EngineFactory class’ v8Engine method. Note that you can inject parameters into the method and these parameters will be resolved as beans. The resulting V8Engine bean will be a singleton.

    Factory methods can return throw to allow for beans to be disabled conditionally. The use of @Requires should always be the preferred method to conditionally create beans and throwing an exception a factory method should only be done if using is not possible.

    For example:

    1. public interface Engine {
    2. Integer getCylinders();
    3. }
    4. @EachProperty("engines")
    5. public class EngineConfiguration implements Toggleable {
    6. private boolean enabled = true;
    7. private Integer cylinders;
    8. @NotNull
    9. public Integer getCylinders() {
    10. return cylinders;
    11. }
    12. public void setCylinders(Integer cylinders) {
    13. this.cylinders = cylinders;
    14. }
    15. public boolean isEnabled() {
    16. return enabled;
    17. }
    18. public void setEnabled(boolean enabled) {
    19. this.enabled = enabled;
    20. }
    21. }
    22. @Factory
    23. public class EngineFactory {
    24. @EachBean(EngineConfiguration.class)
    25. public Engine buildEngine(EngineConfiguration engineConfiguration) {
    26. if (engineConfiguration.isEnabled()) {
    27. return engineConfiguration::getCylinders;
    28. } else {
    29. throw new DisabledBeanException("Engine configuration disabled");
    30. }
    31. }
    32. }
    1. interface Engine {
    2. Integer getCylinders()
    3. }
    4. @EachProperty("engines")
    5. class EngineConfiguration implements Toggleable {
    6. boolean enabled = true
    7. @NotNull
    8. Integer cylinders
    9. }
    10. @Factory
    11. class EngineFactory {
    12. @EachBean(EngineConfiguration)
    13. Engine buildEngine(EngineConfiguration engineConfiguration) {
    14. if (engineConfiguration.enabled) {
    15. (Engine){ -> engineConfiguration.cylinders }
    16. } else {
    17. throw new DisabledBeanException("Engine configuration disabled")
    18. }
    19. }
    20. }

    Injection Point

    A common use case with factories is to take advantage of annotation metadata from the point at which an object is injected such that behaviour can be modified based on said metadata.

    1. @Documented
    2. @Retention(RUNTIME)
    3. @Target(ElementType.PARAMETER)
    4. public @interface Cylinders {
    5. int value() default 8;
    6. }
    1. @Documented
    2. @Retention(RUNTIME)
    3. @Target(ElementType.PARAMETER)
    4. @interface Cylinders {
    5. int value() default 8
    6. }
    1. @MustBeDocumented
    2. @Retention(AnnotationRetention.RUNTIME)
    3. @Target(AnnotationTarget.VALUE_PARAMETER)
    4. annotation class Cylinders(val value: Int = 8)

    The above annotation could be used to customize the type of engine we want to inject into a vehicle at the point at which the injection point is defined:

    1. @Singleton
    2. class Vehicle {
    3. Vehicle(@Cylinders(6) Engine engine) {
    4. this.engine = engine;
    5. }
    6. String start() {
    7. return engine.start();
    8. }
    9. }
    1. @Singleton
    2. internal class Vehicle(@param:Cylinders(6) private val engine: Engine) {
    3. fun start(): String {
    4. return engine.start()
    5. }
    6. }

    The above Vehicle class specifies an annotation value of @Cylinders(6) indicating an Engine of six cylinders is needed.

    To implement this use case you can define a factory that accepts the instance which can be used to analyze the annotation values defined:

    1. @Factory
    2. class EngineFactory {
    3. @Prototype
    4. Engine v8Engine(InjectionPoint<?> injectionPoint, CrankShaft crankShaft) { (1)
    5. final int cylinders = injectionPoint
    6. .getAnnotationMetadata()
    7. .intValue(Cylinders.class).orElse(8); (2)
    8. switch (cylinders) { (3)
    9. case 6:
    10. return new V6Engine(crankShaft);
    11. case 8:
    12. return new V8Engine(crankShaft);
    13. default:
    14. throw new IllegalArgumentException("Unsupported number of cylinders specified: " + cylinders);
    15. }
    16. }
    17. }
    1. @Factory
    2. class EngineFactory {
    3. @Prototype
    4. Engine v8Engine(InjectionPoint<?> injectionPoint, CrankShaft crankShaft) { (1)
    5. final int cylinders = injectionPoint
    6. .getAnnotationMetadata()
    7. .intValue(Cylinders.class).orElse(8) (2)
    8. switch (cylinders) { (3)
    9. case 6:
    10. return new V6Engine(crankShaft)
    11. case 8:
    12. return new V8Engine(crankShaft)
    13. default:
    14. throw new IllegalArgumentException("Unsupported number of cylinders specified: $cylinders")
    15. }
    16. }
    17. }
    1. @Factory
    2. internal class EngineFactory {
    3. @Prototype
    4. fun v8Engine(injectionPoint: InjectionPoint<*>, crankShaft: CrankShaft): Engine { (1)
    5. val cylinders = injectionPoint
    6. .annotationMetadata
    7. .intValue(Cylinders::class.java).orElse(8) (2)
    8. return when (cylinders) { (3)
    9. 6 -> V6Engine(crankShaft)
    10. 8 -> V8Engine(crankShaft)
    11. else -> throw IllegalArgumentException("Unsupported number of cylinders specified: $cylinders")
    12. }
    13. }