Vertx, Guice and Config Retriever: Dependency Injection in Vertx 4.x

In computer science, dependency injection is defined as a pattern, whereby one component gets other components (dependencies) from outside. Numerous posts were written about various implementations of dependency injection in Vertx using the Google Guice library. All of them are good and I do not want to reinvent a bicycle here and repeat the same things again. However, in my opinion, it is a good idea to give a bit more systematic approach to this topic. In my experience, in most cases, for developers is not a big deal to implement a basic DI with Vertx, rather it seems hard to incorporate Vertx components into the DI pipeline. In this post, we will review how to do dependency injection in Vertx with Google Guice and how to build a basic injection that has the ConfigRetriever component (Vertx’s way to obtain an application configuration). Please note, that in this article we use Futures API, so it is focused on Vertx 4.x developers.

Basic DI with Google Guice

Google Guice is an established solution to implement a dependency injection technique in Java applications. Like most established libraries, it is quite simple to use, yet it does not mean that it is limited in any way. This is a very flexible and powerful solution. The main building blocks of the framework are modules and injectors. The module is used to define how to get dependencies. The injector serves as a main entry point of an application and is used for an actual component initialization. Let have a quick example, that uses a constructor injection technique. Imagine, that you develop a verticle, that has two external dependencies — a client class (that performed HTTP calls) and a repository class (that does database operations). For sure, we will not do here their precise implementations, because it is out of the scope of the post. In order to specify these dependencies classes inside the verticle, we need to use an @Inject annotation. If you are familiar with the Spring framework, you would find a process familiar. Basically, we need to create fields to keep references for components, define a constructor and annotate it with the @Inject, so Guice will know that we use constructor injection.

The Bag Data Structure From Eclipse Collections

In computer science, a bag is defined as an abstract data structure, that allows keeping duplicate elements in any order. This is similar to a physical bag, where you could also put any elements and take them out randomly. So, bags are different from lists (because lists care about a particular position of an element) and from sets (because sets do not allow duplicates). The bag is a good choice when you just need to collect items and do some processing using iterations. Java does not offer its "vanilla" implementation of the bag, however, you could find it in popular collections libraries. In this post, we will review the Bag from Eclipse Collections, which supplies both mutable and immutable versions.

Create Bags

Before we will proceed with various Bag methods, let observe how to initialize a new Bag instance within the Eclipse Collections framework. Likewise to other types of collections, there are presented both mutable (modifiable) and immutable (non-modifiable) versions. In general, we can use the Bags class, which allows utilizing static factory methods to obtain bags:

  • Bags.immutable.* calls ImmutableBagFactory to create immutable bags.
  • Bags.mutable.* calls MutableBagFactory to create mutable bags.

Both types use the same approaches, that can be separated into the following three categories:

Error Handling in Spring Webflux

The topic of error handling in web applications is very important. From a client perspective it is essential to know on how was the request proceeded and in case of any error is crucial to provide to the client a valid reason, especially if the error was due to the client’s actions. There are different situations, when notifying callers about concrete reasons is important – think about server-side validations, business logic errors that come due to bad requests or simple not found situations.

The mechanism of error handling in Webflux is different, from what we know from Spring MVC. Core building blocks of reactive apps – Mono and Flux brings a special way to deal with error situations, and while old exception-based error handling still may work for some cases, it violates Spring Webflux nature. In this post I will do an overview of how to process errors in Webflux when it comes to business errors and absent data. I will not cover technical errors in this article, as they are handled by Spring framework.

Validation Forms in Vue.js Apps Using Vuelidate library

Hello! One of the most common tasks in software development is input validation. Any app has different forms that submit some data, and we want to check numerous conditions: that fields are not empty and that they have a proper format (like emails), length, and so on. Yes, we can do it manually, but this is not the best path. It is better to use a specific validation library. For Vue.js apps, the popular choice is Vueildate. In this post, we will see how to install it to your Vue apps, how to use it, and observe the most important built-in validators.

What Is Vuelidate?

Let have a simple example. You’re building a signup form. You want to check that user provides a correct input, like:

Using Java Optional Vs. Vavr Option

When it comes to Java, there are so many Options...

Today, I would like to discuss an essential Java topic – the usage of Optional class — and compare it with an alternative from the Vavr library. Optional was initially introduced in Java 8 and defined as “a container object which may or may not contain a non-null value.”

You may also like: 26 Reasons Why Using Optional Correctly Is Not Optional

Developers utilize Optionals in order to avoid null checking in places when code execution leads to "not a result" but also to a null value, and it can, in this regard, result in a NullPointerException. In such cases, Optional offers us some fancy functionality, but not all of it was introduced in the 8th release, some features require Java 11. Another way to handle these issues is with Vavr’s Option class.