Get micronaut to use my instance of `JacksonConfiguration`

最后都变了- 提交于 2020-01-16 17:58:50

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/59160012/get-micronaut-to-use-my-instance-of-jacksonconfiguration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!