Table of contents
1.
Introduction
2.
Dropwizard Dependency Injection
3.
HK2 in Dropwizard
4.
Dropwizard Dependency Injection Using HK2
5.
Frequently Asked Questions
5.1.
What is Dropwizard? 
5.2.
What distinguishes the spring boot from the Dropwizard?
5.3.
Is Dropwizard an open source?
5.4.
Does Dropwizard use Jetty?
5.5.
Name the libraries gathered in Dropwizard.
6.
Conclusion 
Last Updated: Mar 27, 2024
Medium

Dropwizard Dependency Injection

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Are you also wondering what Dropwizard is and where they are used? Don't worry, Ninja. Let's understand all about Dropwizard as well as Dropwizard Client.

 Dropwizard is a lightweight RESTful Java framework based on the Jetty app server, Jersey REST framework, and Jackson JSON parser. Yammer developed the Dropwizard. It supports JVM(Java Virtual Machine) based backend. It provides the best Java libraries to embed in your applications.

Dropwizard Dependency Injection

This article will discuss the dropwizard dependency injection and its work.

Dropwizard Dependency Injection

 Dropwizard uses HK2 to provide you with a simple dependency injection mechanism. You can also add support for a more robust DI using the Guice  package.

HK2 in Dropwizard

HK2 is a Dependency Injection (DI) framework that complies with JSR-330 and is integrated with Jersey 2.0, just like Spring and Guice. Even though HK2 is less well-known than other DI frameworks, Dropwizard is pre-installed with it by default. 

To use HK2 in dropwizard, we must create an AbstractBinder, register it with the Jersey application, and specify how HK2 should inject classes within it.

Dropwizard Dependency Injection Using HK2

The dropwizard dependency injection configuration that can override during test execution for mocking purposes can be created by putting it into your app Configuration for a bundle to consume:

public interface DependencyInjectionConfiguration {
   List<Class<?>> getSingletons();
   List<NamedProperty<? extends Object>> getNamedProperties();
}
public class NamedProperty<T> {
   private final String id;
   private final T value;
   private final Class<T> clazz;
   @JsonCreator
   public NamedProperty(@JsonProperty("id") String id, @JsonProperty("value") T value, @JsonProperty("clazz") Class<T> clazz) {
       this.id = id;
       this.value = value;
       this.clazz = clazz;
   }
   public String getId() {
       return id;
   }
   public T getValue() {
       return value;
   }
   public Class<T> getClazz() {
       return clazz;
   }
}
public class ExampleConfiguration extends Configuration implements DependencyInjectionConfiguration {
   protected Class<?> getUserRepository() {
       return UserRepository.class;
   }
   @Override
   public List<Class<?>> getSingletons() {
       final List<Class<?>> result = new ArrayList();
       result.add(getUserRepository());
       result.add(UserResource.class);
       return result;
   }
   @Override
   public List<NamedProperty<? extends Object>> getNamedProperties() {
       final List<NamedProperty<? extends Object>> result = new ArrayList<>();
       result.add(new NamedProperty<>("dbUser", "dummy_db_user", String.class));
       return result;
   }
}

Then implement a bundle for DI:

public class DependencyInjectionBundle implements ConfiguredBundle<DependencyInjectionConfiguration> {
   @Override
   public void run(DependencyInjectionConfiguration configuration, Environment environment) throws Exception {
           environment
               .jersey()
               .register(
                   new AbstractBinder() {
                       @Override
                       protected void configure() {
                           for (Class<?> singletonClass : configuration.getSingletons()) {
                               bindAsContract(singletonClass).in(Singleton.class);
                           }
                           for (NamedProperty<? extends Object> namedProperty : configuration.getNamedProperties()) {
                               bind((Object) namedProperty.getValue()).to((Class<Object>) namedProperty.getClazz()).named(namedProperty.getId());
                           }
                       }
                   }
               );
   }
}

After that, create a new DependencyInjectionBundle, in your application's run method:

@Override
public void run(ExampleConfiguration config,
               Environment environment) {
   final DependencyInjectionBundle dependencyInjectionBundle = new DependencyInjectionBundle();
   dependencyInjectionBundle.run(configuration, environment);
}

This will help you to use CDI annotations to control your dependency injection:

@Singleton
public class UserResource {
   private final UserRepository userRepository;
   @Inject
   public UserResource(UserRepository userRepository) {
       this.userRepository = userRepository;
   }
}
@Singleton
public class UserRepository {
   private final String dbUser;
   @Inject
   public UserRepository(@Named("dbUser") String dbUser) {
       this.dbUser = dbUser;
   }
}

Then you can provide alternate configuration for testing purposes:

public class TestConfiguration extends ExampleConfiguration {
   @Override
   protected Class<?> getUserRepository() {
       return MockUserRepository.class;
   }
}
@DisplayName("User endpoint")
@ExtendWith(DropwizardExtensionsSupport.class)
public class UserControllerTests {
   public static final DropwizardAppExtension<TestConfiguration> app = new DropwizardAppExtension<>(ExampleApplication.class, new TestConfiguration());
}

Here @Singleton annotation is only effective for Dropwizard resources.

For custom classes, don’t forget to register them as shown in the above example with bindAsContract(singletonClass).in(Singleton.class).

Faqs

Frequently Asked Questions

What is Dropwizard? 

Dropwizard is an open-source framework of Java. It is used for the fast development of high-performance RESTful web services. 

What distinguishes the spring boot from the Dropwizard?

While Spring Boot uses the embeddable version of Tomcat by default but offers an alternative if you prefer Jetty or even RedHat's Undertow, on the other side, Dropwizard takes the convention over configuration approach a little further and is entirely built on Jetty.

Is Dropwizard an open source?

A high-performance, ops-friendly Java framework called Dropwizard can be used to create RESTful backends. Yammer developed it to power their JVM-based backend.

Does Dropwizard use Jetty?

Because HTTP is essential to build a web application, Dropwizard uses the Jetty HTTP framework to integrate an HTTP server into your project.

Name the libraries gathered in Dropwizard.

Dropwizard gathers many popular libraries. These are Jersey, Jackson, Jetty, JUnit, Guava, etc,. These libraries enable the creation of lightweight packages. Furthermore, it uses its own library called Metrics.

Conclusion 

This article contains the basic information on the Dropwizard Dependency Injection. In this article, we have discussed the Dropwizard Dependency Injection using HK2. The reader can carry out a thorough understanding of the topic by referring to the Official Documentation. For more information on Dropwizard Dependency Injection, Refer-

 

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass