Card 05/ 07

ExampleDifficulty: Intermediate1 min

Wiring a Class You Don't Own

LegacyRideGateway ships inside a jar you depend on. You cannot add @Component to it — there is no source to edit — so scanning will never find it.

java
// no source available — cannot be annotated
class LegacyRideGateway {
    LegacyRideGateway(String endpoint) { /* ... */ }
    void call() { /* ... */ }
}

@Configuration
class GatewayConfig {
    @Bean
    LegacyRideGateway legacyRideGateway() {
        return new LegacyRideGateway("https://legacy.internal/rides");
    }
}
text
calling legacy gateway at https://legacy.internal/rides
context.getBean(LegacyRideGateway.class).call() — run in a container

A @Bean method inside a @Configuration class does what scanning can't: it is ordinary Java, called by the container, and whatever it returns becomes a bean — regardless of what annotations that class carries, or doesn't.