Chain autowiring

Report a typo

Imagine a Device needs a bean of a Printer:

Java
@Component
public class Device {

    private Printer printer;

    @Autowired
    public Device(Printer printer) {
        this.printer = printer;
    }
}
Kotlin
@Component
class Device @Autowired constructor(private val printer: Printer)

A Printer itself needs a bean of the InkSupply:

Java
@Component
public class Printer {

    @Autowired
    private InkSupply inkSupply;
}
@Configuration
public class CopyingConfiguration {

    @Bean
    public InkSupply getInkSupply(){
        return new InkSupply();
    }
}
Kotlin
@Component
class Printer {
    @Autowired
    private lateinit var inkSupply: InkSupply
}
@Configuration
class CopyingConfiguration {
    @Bean
    fun getInkSupply() : InkSupply {
        return InkSupply()
    }
}

Correctly place the beans in the "autowiring chain": the bean above is injected into the bean under it.

Put the items in the correct order
Device
Printer
InkSupply
___

Create a free account to access the full topic