Skip to content


Repository files navigation


Copyright (C) 2018-2022 The Open Library Foundation

This software is distributed under the terms of the Apache License, Version 2.0. See the file "LICENSE" for more information.


Dependency Injection support for FOLIO backend modules


Creating Spring context

SpringContextUtil#init method initializes Spring context and adds it to Vertx Context object.

InitAPI hook can be used to initialize context during start up:

public class InitAPIImpl implements InitAPI {
 public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> handler) {
      future -> {
        SpringContextUtil.init(vertx, context, ApplicationConfig.class);
      result -> {
        if (result.succeeded()) {
        } else {

Accessing Spring context from endpoint classes

SpringContextUtil#autowireDependencies method gets Spring context from Vertx context and uses it to inject beans into target object. Example of injecting Spring beans into API object:

public class EholdingsProxyTypesImpl implements EholdingsProxyTypes {

 private final Logger logger = LoggerFactory.getLogger(EholdingsProxyTypesImpl.class);

 private RMAPIConfigurationService configurationService;
 private ProxyConverter converter;
 private HeaderValidator headerValidator;

 public EholdingsProxyTypesImpl() {
  SpringContextUtil.autowireDependencies(this, Vertx.currentContext());

Declaring Spring configuration

SpringContextUtil#init uses Spring configuration class.

Detailed documentation on how to declare Spring configuration can be found here:

One of the approaches is to use @ComponentScan to automatically detect beans and add them to context:

@ComponentScan(basePackages = {
public class ApplicationConfig {
 public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setLocation(new ClassPathResource(""));
    return configurer;

basePackages parameter specifies list of packages that will be searched. Any class in those packages that is annotated with @Component will be added to context.

PropertySourcesPlaceholderConfigurer class allows to use properties from file on classpath.

Example of bean:

public class RMAPIConfigurationCache {

  private Vertx vertx;
  private long expirationTime;

 public RMAPIConfigurationCache(Vertx vertx, @Value("${configuration.cache.expire}") long expirationTime) {
    this.vertx = vertx;
    this.expirationTime = expirationTime;

vertx and expirationTime parameters will be automatically injected by Spring, expirationTime will be set to the value of "configuration.cache.expire" property from file.

Additional information

Example of module that uses dependency injection support: