Spring vs. Jakarta EE: Injecting Dependencies

The article “Spring vs. Jakarta EE: Defining Injectable Beans” addressed the “first half” of Dependency Injection in Spring and Jakarta EE: the definition of beans that shall be injected. The current article discusses the details of how these beans can be injected. This forms the second part of the dependency injection process, which can be further broken down into the following aspects:

  • Annotations: Which annotations can be used for injection?
  • Disambiguation: How to perform disambiguation if there are multiple candidates for injection?
  • Optional and Multiple Injection: How to gracefully deal with the absence of injectable beans, and how to inject several beans of the same type?
  • Programmatic Injection: How to inject dependencies without annotations?

For the impatient, here is a summary table for these topics, with more details following below:                                                                         

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.

Inversion of Control (IOC), Dependency Injection (DI) Oversimplified

Introduction

IOC and DI are very useful patterns that are used in almost every production project. But, for me personally, they are pretty misleading and difficult concepts to understand. Here, I'll example what both of them are and implement a simplified proof of concept. 

Inversion of Control

Inversion of control is a pattern responsible for your class initialization. They will be created and set on startup or before each class is initialized. So initialization is not done in constructor but by classes themselves (control is inverted). In a configuration class, you can what implementation each interface will be used. 

Angular Basics: Inject a Service/DI With Constructor in the Main.ts File

Well, the main.ts is the entry point for the Angular application. Here, we're not able to query instances of services or get them injected because we're outside of the Angular application. It looks like that storage object is completely decoupled from the Angular application as well, so any dependency injection you're after you'd have to do yourself.

Also of note: while I can’t find any explicit documentation on this, Amazon’s example of this Storage class has all of its properties as static, so I’m not sure if the configuration is expecting an instance of a class at all.

Understanding Inversion of Control and Dependency Injection

There are two key concepts that you must understand when you start with Spring or JakartaEE/CDI–inversion of control and dependency injection.

In brief, inversion of control means letting a framework take control of the execution flow of your program to do things like create instances of your classes and inject the required dependencies.

Migrating to MRKT2: Using Extension Services for Dependency Injection

Learn more about dependency injection and extension services for MRTK2.

Coming from business development, you might get a little shock coming into Unity. Traditionally, game developers are much more focused on making the outside prettier than the inside. Things like dependency injection are kind of unheard of or considered 'too heavy' for game development. But if you are still in the process of development, actually being able to access a consistent (mock) data service instead of the real live data service might be a big advantage, especially when that data service is rate limited or expensive.

You may also like: Migrating to MRTK2: Interacting With the Spacial Map

The Mixed Reality Toolkit 2 offers a great feature for that: extension services. And it's actually pretty easy to use, and I am going to show a simple sample. I have written about this in a very early alpha stage almost a year ago, but it's now to a point that it's actually usable.

Dependency Injection in Azure Functions

Azure Functions V2 supports ASP.NET Core functions like dependency injection. It is especially good if we write wrapper functions for shared libraries and components we are also using in web and other applications of our solution. This blog post shows how to use dependency injection in Azure Functions.

To get started with the code, check out Azure Functions with DI GitHub repository by @MikaBerglund. It's simple and minimal Visual Studio solution without any overhead.

Running Cucumber With Maven

A little while ago (ok, maybe in the distant past), I started writing about Cucumber. My very first post detailed how to set up Cucumber JVM, and we’ve come a long way from then. I thought it was worth revisiting, as there are simpler ways to get set up, and better tools out there to use. So with that, let’s dive back in!

Build Tools

Previously, I was setting up the project by manually identifying and downloading each of the required jars into a lib directory. That is so last decade to do things. What we should be doing is orchestrating and controlling these dependencies with a build tool.

Spring Framework Basics: What Is Inversion of Control?

Developers starting with the Spring Framework often get confused with the terminology, specifically dependencies, dependency injection, and Inversion of Control. In this article, we introduce you to the concept of Inversion of Control.

What You Will Learn

  • What is Inversion of Control?
  • What are some examples of Inversion of Control?
  • How does the Spring Framework implement Inversion of Control?
  • Why is Inversion of Control important and what are its advantages?

What Is Inversion of Control?

Approach-1

Have a look at the following implementation of ComplexAlgorithmImpl:

Inversion of Control (Explained Non-Technically)

I will use how businesses evolve to provide an analogy for Inversion of Control.

Businesses don't set out on day one to be a Fortune 500 company. Typically, they start with you in your garage (maybe with a friend).Over time, your business grows and you hire people, assign clearer functional responsibilities, and start to scale up your business. Businesses have to do this, while also changing quickly to stay competitive.

Functional Programming Is Just a Safety Restriction on Object Orientation

I find that functional languages are an admiration of it's own idealized self. Their pursuit of mathematical (lambda calculus) purity forgets that systems run in the real world and not in theory. Yes, for academia and advancing compilers, they are great where idealized theory is the endeavour. However, their idealism is neglectful of the limitations and realities of computing for commercial development. Why be shamed for wanting to allow side effects and mutating objects? Especially when functional programming just further restricts object orientation.

Limitations

To illustrate my point of limitations within computers, let's use the typical functional programming example of calculating a particular value for the Fibonacci sequence.

Dependency Injection in .NET Core Console Applications

ASP.NET Core uses a built-in dependency injection mechanism provided by Microsoft. This blog post introduces how to use the same mechanism in .NET Core console applications. For those who like other DI/IoC frameworks, this article provides a demo about how to use Autofac with .NET Core framework-level dependency injection.

Framework-Level Dependency Injection in ASP.NET Core

I don't describe here details of dependency injection in ASP.NET Core. Those who want to find out more about it can skim through these writings: