问题描述
我试图让 micronaut (1.2.6) 使用我的代码来实例化 JacksonConfiguration
而不是默认机制.
I'm trying to get micronaut (1.2.6) to use my code to instantiate a JacksonConfiguration
instead of the default mechanism.
我有这个:
@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);
}
}
}
而且,当 ObjectMapper
工厂接收到 JacksonConfiguration
的实例时,我的另一个方法从未被调用.
and, while the ObjectMapper
factory receives and instance of JacksonConfiguration
, my other method is never called.
我尝试将 @Replaces(JacksonConfiguration.class)
添加到我的 jacksonConfiguration()
方法中,但这会导致 ObjectMapper
工厂方法成为使用 null
而不是 JacksonConfiguration
的实例调用(不知道为什么).
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).
我应该怎么做来替换默认的 JacksonConfiguraion
?
What should I do to replace the default JacksonConfiguraion
?
附注:我知道我可以忽略它并以任何我想要的方式实例化我的 ObjectMappers
(这就是我会做的直到我理解这个问题).这里的重点是更多地了解 micronaut 的工作原理,而不是为特定的实际问题找到解决方案/变通办法.
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.
推荐答案
使用您当前的代码,我希望抛出 NonUniqueBean 异常,因为会有多个 JacksonConfiguration bean.你应该将你的配置为 @Replaces(JacksonConfiguration.class)
.
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)
.
请注意,存在与替换配置属性 bean 相关的错误,该错误已在 1.3.0.M1 和最新的 1.2.8.BUILD-SNAPSHOT 中解决,因此您需要使用其中一个版本
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
这篇关于让 micronaut 使用我的“JacksonConfiguration"实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!