Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
public class InitAPIImpl implements InitAPI {
  @Override
 public void init(Vertx vertx, Context context, Handler<AsyncResult<Boolean>> handler) {
    vertx.executeBlocking(
      future -> {
        SpringContextUtil.init(vertx, context, ApplicationConfig.class);
        future.complete();
      },
      result -> {
        if (result.succeeded()) {
          handler.handle(Future.succeededFuture(true));
        } else {
          handler.handle(Future.failedFuture(result.cause()));
        }
      });
  }
}

...

Code Block
languagejava
public class EholdingsProxyTypesImpl implements EholdingsProxyTypes {

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

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

  @SuppressWarnings("squid:S1172")
  public EholdingsProxyTypesImpl(Vertx vertx, String tenantId) {
    SpringContextUtil.autowireDependencies(this, vertxVertx.getOrCreateContextcurrentContext());
  }


SpringContextUtil#autowireDependencies method gets Spring context from Vertx context and uses it to inject beans into EholdingsProxyTypesImpl.

RestVerticle will call constructor with parameters (Vertx vertx, String tenantId) default constructor through reflection.

@SuppressWarnings("squid:S1172") annotation is needed because Sonar can't see reflection calls and considers tenantId parameter to be unnecessary.

Declaring Spring configuration

...

Expand
@Autowired
private RMAPIConfigurationService configurationService;
@Autowired
private PackagesConverter converter;
@Autowired
private HeaderValidator headerValidator;
@Autowired
private PackageParametersValidator packageParametersValidator;
@Autowired
private PackagePutBodyValidator packagePutBodyValidator;
@Autowired
private CustomPackagePutBodyValidator customPackagePutBodyValidator;
@Autowired
private PackagesPostBodyValidator packagesPostBodyValidator;
@Autowired
private TitleParametersValidator titleParametersValidator;
@Autowired
private ResourcesConverter resourceConverter;
@Autowired
private IdParser idParser;
@SuppressWarnings("squid:S1172")

public EholdingsPackagesImpl(Vertx vertx, String tenantId) {
SpringContextUtil.autowireDependencies(this, vertxVertx.getOrCreateContextcurrentContext());
}


4) It resolves circular dependencies between objects.

...

6) Objects are created once instead of being created on each request.



Presentation on dependency injection Spring_DI.pptx