giorgiga
giorgiga

Reputation: 1778

Get micronaut to use my instance of `JacksonConfiguration`

I'm trying to get micronaut (1.2.6) to use my code to instantiate a JacksonConfiguration instead of the default mechanism.

I have this:

@Factory
public class MyFactory {

    @Singleton
    public JacksonConfiguration jacksonConfiguration() {
        JacksonConfiguration cfg = new JacksonConfiguration();
        System.out.println("jacksonConfiguration() - hashcode is " + System.identityHashCode(cfg));
        return cfg;
    }

    @Factory
    public static class MyObjectMapperFactory extends ObjectMapperFactory {
        @Override
        @Singleton @Replaces(ObjectMapper.class)
        public ObjectMapper objectMapper(@Nullable JacksonConfiguration jacksonConfiguration, @Nullable JsonFactory jsonFactory) {
            System.out.println("objectMapper()         - hashcode is " + System.identityHashCode(jacksonConfiguration));
            return super.objectMapper(jacksonConfiguration, jsonFactory);
        }
    }

}

and, while the ObjectMapper factory receives and instance of JacksonConfiguration, my other method is never called.

I tried adding @Replaces(JacksonConfiguration.class) to my jacksonConfiguration() method, but that causes the ObjectMapper factory method to be called with null instead of an instance of JacksonConfiguration (no idea why).

What should I do to replace the default JacksonConfiguraion?

PS: I know I can just ignore it and instantiate my ObjectMappers any way I want (that's what I'll do until I understand this issue). The point here is more understanding how micronaut works than finding a solution/workaround to a specific practical problem.

Upvotes: 0

Views: 1182

Answers (1)

James Kleeh
James Kleeh

Reputation: 12228

With your current code I would expect a NonUniqueBean exception to be thrown because there would be multiple JacksonConfiguration beans. You should configure yours to @Replaces(JacksonConfiguration.class).

Note there was a bug related to replacing configuration properties beans that was resolved in 1.3.0.M1 and the latest 1.2.8.BUILD-SNAPSHOT so you will need to use one of those versions

Upvotes: 1

Related Questions