Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You are building an internal library that must create one HTTP client bean per downstream service listed in configuration. Compare doing this with registered bean definitions versus a single factory bean holding a map.

Registering one bean per client lets other beans inject a specific client by qualifier, and lets Actuator, metrics, health checks and other bean-aware features see each client individually. The price is dynamic registration code that is harder to read, debug and make AOT-compatible. A single registry bean holding a Map of clients is simple and explicit, but the clients are not beans. I would start with the registry and choose dynamic registration only when per-bean integration features are really needed, then document and test it.

Why this question is asked

Internal libraries live much longer than the service that first needed them, and every team that adds the dependency inherits their design choices. Creating clients from configuration is a common library task, and the two designs look similar on day one but behave very differently in debugging, upgrades and native images. Interviewers ask this to see whether we can compare options by their consequences for users. In this answer, we will recap the two designs in code, compare what each gives consumers, look at the costs of dynamic registration, and end with a recommendation and how to make either choice safe.

1A Quick Recap of the Two Designs

The library reads a list such as acme.clients.inventory.base-url and acme.clients.pricing.base-url and must create an HTTP client for each entry. Design A registers one Spring bean per entry, named inventoryClient and pricingClient, using a BeanDefinitionRegistryPostProcessor or, in Spring Framework 7, a BeanRegistrar. Design B registers one ordinary bean, a ClientRegistry, that builds all clients in its constructor and keeps them in a Map.

Both read the same configuration and create the same RestClient objects. The difference is whether the container knows about each client.

Java
// Design B: one bean, a map inside
@ConfigurationProperties("acme")
public record AcmeProperties(Map<String, ClientProps> clients) {}
public final class ClientRegistry {
private final Map<String, RestClient> clients;
public ClientRegistry(AcmeProperties props, RestClient.Builder builder) {
this.clients = props.clients().entrySet().stream().collect(Collectors.toUnmodifiableMap(
Map.Entry::getKey,
e -> builder.clone().baseUrl(e.getValue().baseUrl().toString()).build()));
}
public RestClient get(String name) {
RestClient c = clients.get(name);
if (c == null) throw new IllegalArgumentException("No client configured: " + name);
return c; // get("inventory") -> the inventory client
}
}

Notice that design B can use @ConfigurationProperties normally, because the registry is an ordinary bean created after binding. It also uses Spring Boot's auto-configured RestClient.Builder, so observation and message converters are applied to every client. Design A has to read the Environment with Binder during registration, as covered in the coding question.

2What One Bean per Client Gives Consumers

When each client is a bean, it is a first-class citizen of the container. A service can declare a constructor parameter @Qualifier("inventoryClient") RestClient inventory and receive exactly that client; tests can replace one client with @MockitoBean; the Actuator beans endpoint lists every client. Any feature that discovers beans by type also sees them: a custom HealthIndicator per client, BeanPostProcessors that add metrics or tracing, and lifecycle callbacks that close each client at shutdown.

These are real advantages when the library's users want per-client wiring, for example a service that uses only two of ten configured clients and wants each injected separately, or a platform team that expects every outbound client to appear in a standard health check.

3What Dynamic Registration Costs

The bean names do not appear anywhere in source code. A developer who searches for inventoryClient finds nothing but a string concatenation in a post-processor. Errors such as a missing bean or two matching beans of type RestClient appear at startup and point to code the developer did not write. Registration runs before @ConfigurationProperties binding, so the library reimplements validation with Binder. And registering several beans of the same type changes autowiring for everyone: a consumer who injects a plain RestClient now gets an error about multiple candidates.

Spring AOT and native images add another cost. AOT evaluates registration at build time, so the set of clients becomes fixed by the configuration present during the build. If environments configure different clients, the generated code no longer matches. That can be managed, but it needs documentation and tests that the library's users must follow.

Comparing the two designs
ConcernOne bean per clientRegistry bean with a Map
Inject one client by qualifieryesno, inject the registry and call get()
Visible to Actuator and bean-aware toolseach clientonly the registry
Replace one client in a test@MockitoBean on that beanreplace the registry or its map
Uses @ConfigurationPropertiesno, Binder at registrationyes, with validation
Readable and debuggableharder, names built at runtimeplain Java
Spring AOT and native imagesclients fixed at build timeworks, built at runtime
Effect on consumers' autowiringmany RestClient beansnone

4What I Would Choose

I would start with design B, the registry. It is explicit, it uses the normal configuration binding with validation, it behaves the same on the JVM and in native images, and it adds nothing surprising to the consumer's context. The small cost, calling registry.get("inventory") instead of injecting a qualified bean, is acceptable for most services. Per-client health and metrics are still possible: the registry itself can expose a HealthIndicator that checks every client, and the shared builder applies observations to each.

I would switch to design A only if a concrete requirement needs individual beans, such as platform tooling that discovers outbound clients by type, or consumers who must override a single client by defining their own bean. In that case I would make it safe: register the clients with clear names and qualifiers, mark them as not default autowire candidates or provide one @Primary bean, document the generated names in the library's README, test the registration with ApplicationContextRunner for several configurations, and add an AOT test if consumers build native images.

Step throughChoosing the design
  1. Do consumers need each client as its own bean?
    YesGo onNoRegistry bean with a Map
  2. Can the need be met by the registry exposing health or metrics?
    YesRegistry bean with a MapNoGo on
  3. Register one bean per clientwith names, qualifiers, docs
  4. Test with ApplicationContextRunner and AOTseveral configurations

4 steps in order. Play them, or step through them one at a time.

5Follow-up questions

Can we offer both, a registry and individual beans?

Yes, but it doubles the surface area to document and support, and consumers may mix the two styles in one codebase. If we do it, the individual beans should be thin views onto the registry's clients so there is only one object per client, and the feature should be opt-in through a property.

How would you test the dynamic registration?

With ApplicationContextRunner from Spring Boot's test support. It starts a small context with given properties, so we can assert that acme.clients with two entries produces beans inventoryClient and pricingClient, that an empty list produces none, and that a consumer-defined bean with the same name wins when that is the intended rule.

Does the registry approach hide failures until runtime?

Only if we let it. The registry builds every client in its constructor, so a bad URL fails at startup just like a bean would. What it cannot do is fail at startup when code asks for a client name that is not configured; to catch that, consumers can call get() during their own initialisation, or the library can validate a list of required names.

6Conclusion

In this answer, we compared registering one bean per configured client with one registry bean holding a Map. Individual beans let other beans inject a client by qualifier and let Actuator, metrics and other bean-aware features see each one, but dynamic registration is harder to read, debug and make AOT-compatible. The registry is simpler, explicit and uses normal configuration binding, but its clients are not beans. We would choose dynamic registration only when per-bean integration features are truly needed, and then document and test it carefully.

What to say in the interviewAbout 90 seconds aloud

Open with

Both designs create the same clients; the difference is whether the container knows about each one. If I register a bean per client, consumers can inject a specific client with a qualifier, replace one in a test with @MockitoBean, and anything bean-aware, Actuator, health indicators, metrics post-processors, sees each client.

Then, if they want more

That is a real benefit.

The cost is that registration is dynamic: the bean names are built from configuration at runtime, so nobody can find them in the code, it has to use Binder on the Environment rather than @ConfigurationProperties because it runs before binding, several RestClient beans can break a consumer's plain autowiring, and with Spring AOT the list of clients is fixed at build time.

A single ClientRegistry bean that builds a Map of clients in its constructor is plain Java, uses normal validated configuration properties, works the same in native images and adds nothing surprising to the consumer's context; the downside is that clients are not individual beans, so consumers call registry.get.

My default for a library would be the registry, possibly exposing one health indicator for all clients. I would only switch to per-client beans when there is a concrete need, like platform tooling that discovers clients by type, and then I would document the names, use qualifiers, and test registration with ApplicationContextRunner and with AOT.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You are building an internal library that must create one HTTP client bean per...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/e3fff09d14/