To get objects from the registry or context we specify the type of the object we want. Ratpack will find the object(s) that match the given type. If we use the get method then the last object added to the registry with the given type is returned. To get multiple objects we use the getAll method. The methods returns an Iterable with the found objects where the last added objects are returned as first elements.

In the following example specification we have a Registry with some objects, of which two are of type User. Next we use the get and getAll methods to get the objects.

package com.mrhaki.ratpack

import ratpack.registry.Registry
import ratpack.registry.RegistrySpec
import spock.lang.Specification

class GetRegistrySpec extends Specification {

    Registry registry

    def setup() {
        // Setup registry with two objects of type User.
        registry = Registry.of { RegistrySpec registrySpec ->
            registrySpec.add(new User(username: 'mrhaki'))
            registrySpec.add(new User(username: 'hubert'))

    def "get object with given type and return the last one in the registry"() {
        // get method returns the last element in the
        // registry for the given User type.
        final User user = registry.get(User)

        user.username == 'hubert'

    def "get multiple objects with given type and return in last to first order"() {
        // get all objects of the given User type in the
        // registry and returns them in the order
        // last in first out.
        final Iterable<User> users = registry.getAll(User)

        users.toList().username == \['hubert', 'mrhaki'\]


class User {
    String username

Written with Ratpack 1.1.1.

Original blog post