Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In our earlier article, we’ve shown how building scalable applications using Ratpack looks like.

In this tutorial, we will discuss further on how to use Google Guice with Ratpack as dependency management engine.

2. Why Google Guice?

Google Guice is an open source software framework for the Java platform released by Google under the Apache License.

It’s extremely lightweight dependency management module that is easy to configure. Moreover, it only allows constructor level dependency injection for the sake of usability.

More details about Guice can be found here.

3. Using Guice With Ratpack

3.1. Maven Dependency

Ratpack has first-class support for Guice dependency. Therefore, we don’t have to manually add any external dependency for Guice; it already comes pre-built with Ratpack. More details on Ratpack‘s Guice support can be found here.

Hence, we just need to add following core Ratpack dependency in the pom.xml:

<dependency>
    <groupId>io.ratpack</groupId>
    <artifactId>ratpack-core</artifactId>
    <version>1.4.5</version>
</dependency>

You can check the latest version on Maven Central.

3.2. Building Service Modules

Once done with the Maven configuration, we’ll build a service and make good use of some simple dependency injection in our example here.

Let’s create one service interface and one service class:

public interface DataPumpService {
    String generate();
}

This is the service interface which will act as the injector. Now, we have to build the service class which will implement it and will define the service method generate():

public class DataPumpServiceImpl implements DataPumpService {

    @Override
    public String generate() {
        return UUID.randomUUID().toString();
    }

}

An important point to note here is that since we are using Ratpack’s Guice module, we don’t need to use Guice‘s @ImplementedBy or @Inject annotation to manually inject the service class.

3.3. Dependency Management

There are two ways to perform dependency management with Google Guice.

The first one is to use Guice‘s AbstractModule and other on is to use Guice’s instance binding mechanism method:

public class DependencyModule extends AbstractModule {

    @Override
    public void configure() {
        bind(DataPumpService.class).to(DataPumpServiceImpl.class)
          .in(Scopes.SINGLETON);
    }

}

A few of points to note here:

  • by extending AbstractModule we are overriding default configure() method
  • we are mapping DataPumpServiceImpl class with the DataPumpService interface which is the service layer built earlier
  • we have also injected the dependency as Singleton manner.

3.4. Integration With the Existing Application

Since the dependency management configuration is ready, let’s now integrate it:

public class Application {

    public static void main(String[] args) throws Exception {

      RatpackServer
          .start(server -> server.registry(Guice
            .registry(bindings -> bindings.module(DependencyModule.class)))
            .handlers(chain -> chain.get("randomString", ctx -> {
                DataPumpService dataPumpService = ctx.get(DataPumpService.class);
                ctx.render(dataPumpService.generate().length());
            })));
    }
}

Here, with the registry() – we’ve bound the DependencyModule class which extends AbstractModule. Ratpack’s Guice module will internally do the rest of the needful and inject the service in the application Context.

Since it’s available in the application-context, we can now fetch the service instance from anywhere in the application. Here, we have fetched the DataPumpService instance from the current context and mapped the /randomString URL with the service’s generate() method.

As a result, whenever the /randomString URL is hit, it will return random string fragments.

3.5. Runtime Instance Binding

As said earlier, we will now use Guice’s instance binding mechanism to do the dependency management at runtime. It’s almost the same like previous technique apart from using Guice’s bindInstance() method instead of AbstractModule to inject the dependency:

public class Application {

    public static void main(String[] args) throws Exception {

      RatpackServer.start(server -> server
        .registry(Guice.registry(bindings -> bindings
        .bindInstance(DataPumpService.class, new DataPumpServiceImpl())))
        .handlers(chain -> chain.get("randomString", ctx -> {
            DataPumpService dataPumpService = ctx.get(DataPumpService.class);
            ctx.render(dataPumpService.generate());
        })));
    }
}

Here, by using bindInstance(), we’re performing instance binding, i.e. injecting DataPumpService interface to DataPumpServiceImpl class.

In this way, we can inject the service instance to the application-context as we did in the earlier example.

Although we can use any of the two techniques for dependency management, it’s always better to use AbstractModule since it’ll completely separate the dependency management module from the application code. This way the code will be a lot cleaner and easier to maintain in the future.

3.6. Factory Binding

There’s also one more way for dependency management called factory binding. It’s not directly related to Guice’s implementation but this can work in parallel with Guice as well.

A factory class decouples the client from the implementation. A simple factory uses static methods to get and set mock implementations for interfaces.

We can use already created service classes to enable factory bindings. We just need to create one factory class just like DependencyModule (which extends Guice’s AbstractModule class) and bind the instances via static methods:

public class ServiceFactory {

    private static DataPumpService instance;

    public static void setInstance(DataPumpService dataPumpService) {
        instance = dataPumpService;
    }

    public static DataPumpService getInstance() {
        if (instance == null) {
            return new DataPumpServiceImpl();
        }
        return instance;
    }
}

Here, we’re statically injecting the service interface in the factory class. Hence, at a time only one instance of that interface would be available to this factory class. Then, we have created normal getter/setter methods to set and fetch the service instance.

Point to note here is that in the getter method we have made one explicit check to make sure only a single instance of the service is present or not; if it’s null then only we have created the instance of the implementational service class and returned the same.

Thereafter, we can use this factory instance in the application chain:

.get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))

4. Testing

We will use Ratpack‘s MainClassApplicationUnderTest to test our application with the help of Ratpack‘s internal JUnit testing framework. We have to add the necessary dependency (ratpack-test) for it.

Point to note here is since the URL content is dynamic, we can’t predict it while writing the test case. Hence, we would match the content length of the /randomString URL endpoint in the test case:

@RunWith(JUnit4.class)
public class ApplicationTest {

    MainClassApplicationUnderTest appUnderTest
      = new MainClassApplicationUnderTest(Application.class);

    @Test
    public void givenStaticUrl_getDynamicText() {
        assertEquals(21, appUnderTest.getHttpClient()
          .getText("/randomString").length());
    }
	
    @After
    public void shutdown() {
        appUnderTest.close();
    }
}

5. Conclusion

In this quick article, we’ve shown how to use Google Guice with Ratpack.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW