Copyright © 2018–2025 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 using Spring Framework and Vert.x.
Requirements:
- Vert.x 5.0.x+
- Spring Framework 6.2.x+
- Java 21+
The SpringContextUtil.init() method initializes a Spring application context and adds it to the Vert.x Context object.
An InitAPI hook can be used to initialize the context during startup:
public class InitAPIImpl implements InitAPI {
@Override
public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> handler) {
vertx.executeBlocking(promise -> {
SpringContextUtil.init(vertx, context, ApplicationConfig.class);
promise.complete();
}).onComplete(result -> {
if (result.succeeded()) {
handler.handle(Future.succeededFuture(true));
} else {
handler.handle(Future.failedFuture(result.cause()));
}
});
}
}The SpringContextUtil.autowireDependencies() method retrieves the Spring context from the Vert.x context and uses it to inject beans into the target object.
Example of injecting Spring beans into an API class:
public class EholdingsProxyTypesImpl implements EholdingsProxyTypes {
@Autowired
private RMAPIConfigurationService configurationService;
@Autowired
private ProxyConverter converter;
@Autowired
private HeaderValidator headerValidator;
public EholdingsProxyTypesImpl() {
SpringContextUtil.autowireDependencies(this, Vertx.currentContext());
}
}The SpringContextUtil.init() method requires a Spring configuration class.
For detailed documentation on declaring Spring configuration, see the Spring Framework Reference.
One approach is to use @ComponentScan to automatically detect beans and add them to the context:
@Configuration
@ComponentScan(basePackages = {
"org.folio.rest.converter",
"org.folio.rest.parser",
"org.folio.rest.validator",
"org.folio.http",
"org.folio.config.impl",
"org.folio.config.cache"})
public class ApplicationConfig {
@Bean
public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("application.properties"));
return configurer;
}
}The basePackages parameter specifies the list of packages to scan. Any class in those packages annotated with @Component will be added to the context.
The PropertySourcesPlaceholderConfigurer bean allows the use of properties from the application.properties file on the classpath.
@Component
public class RMAPIConfigurationCache {
private Vertx vertx;
private long expirationTime;
@Autowired
public RMAPIConfigurationCache(Vertx vertx, @Value("${configuration.cache.expire}") long expirationTime) {
this.vertx = vertx;
this.expirationTime = expirationTime;
}
}The vertx and expirationTime parameters are automatically injected by Spring. The expirationTime value is set from the configuration.cache.expire property in the application.properties file.
Example module using this dependency injection support: mod-kb-ebsco-java
For more FOLIO developer documentation, visit dev.folio.org
See project FDIS at the FOLIO issue tracker.
See CONTRIBUTING.md for contribution guidelines.