Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Spring Top – Temp – Non-Geo (Lightrun)

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING
NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this tutorial, we'll see how to inject Spring beans by generic parameters.

2. Autowiring Generics in Spring 3.2.

Spring supports injection of generic types since version 3.2.

Suppose we have an abstract class called Vehicle and a concrete sub-class of it called Car:

public abstract class Vehicle {
    private String name;
    private String manufacturer;
 
    // ... getters, setters etc
}
public class Car extends Vehicle {
    private String engineType;
 
    // ... getters, setters etc
}

Suppose we want to inject a list of objects of type Vehicle into some handler class:

@Autowired
private List<Vehicle> vehicles;

Spring will autowire all the Vehicle instance beans into this list. It doesn't matter how we instantiate these beans through Java or XML configuration.

We may also use qualifiers to get only specific beans of the Vehicle type. Then we create @CarQualifier and annotate it with @Qualifier:

@Target({
  ElementType.FIELD, 
  ElementType.METHOD,
  ElementType.TYPE, 
  ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface CarQualifier {
}

Now we may use this annotation on our list to get only some specific Vehicles:

@Autowired
@CarQualifier
private List<Vehicle> vehicles;

In this case, we may create several Vehicle beans but Spring will only inject those with @CarQualifier into the list above:

public class CustomConfiguration {
    @Bean
    @CarQualifier
    public Car getMercedes() {
        return new Car("E280", "Mercedes", "Diesel");
    }
}

3. Autowiring Generics in Spring 4.0.

Suppose we have another Vehicle sub-class called Motorcycle:

public class Motorcycle extends Vehicle {
    private boolean twoWheeler;
    //... getters, setters etc
}

Now, if we want to inject only the Car beans into our list but no Motorcycle ones, we can do this by using the specific sub-class as a type parameter:

@Autowired
private List<Car> vehicles;

Spring lets us use a generic type as a qualifier without the need for an explicit annotation since version 4.0.

Prior to Spring 4.0, the code above wouldn't work with beans of multiple subclasses of Vehicle. Without explicit qualifiers, we would receive a NonUniqueBeanDefinitionException.

4. ResolvableType

The generics autowiring feature works with the help of ResolvableType class behind the scenes.

It was introduced in Spring 4.0 to encapsulate Java Type and handle access to supertypes, interfaces, generic parameters and finally resolve to a Class:

ResolvableType vehiclesType = ResolvableType.forField(getClass().getDeclaredField("vehicles"));
System.out.println(vehiclesType);

ResolvableType type = vehiclesType.getGeneric();
System.out.println(type);

Class<?> aClass = type.resolve();
System.out.println(aClass);

The output of the above code would show the corresponding simple and generic types:

java.util.List<com.example.model.Vehicle>
com.example.model.Vehicle
class com.example.model.Vehicle

5. Conclusion

Injection of generic types is a powerful feature, which saves the developer the effort of assigning explicit qualifiers, making code cleaner and much more understandable.

As always the code can be found over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Generic footer banner
Comments are closed on this article!