有没有更好的方法来配置Bean?
下面的Spring DI xml可以改进吗? xml 下面是配置目标 bean 的编程方法。
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="setSerializationInclusion" />
<property name="arguments">
<list>
<value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
</list>
</property>
</bean>
ObjectMapper 映射器 = 新 对象映射器(); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
Can the following Spring DI xml be improved? Below the xml is the programmatic approach of configuring the target bean.
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="setSerializationInclusion" />
<property name="arguments">
<list>
<value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
</list>
</property>
</bean>
ObjectMapper mapper = new
ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 是一种非常糟糕的方法。是的,您可以做到这一点,但是编写一个
FactoryBean
来配置您的ObjectMapper
要容易得多:然后在您的XML中:
XML is a really bad way of doing this. Yes, you can do this, but it's much easier to write a
FactoryBean
which configures yourObjectMapper
:and then in your XML:
仍然不完全理想,但更干净一些:
一个缺点是内存中会有不必要的 bean 实例。 (我正在使用这种方法,并且我会一直使用它,直到 Spring 决定处理这些)。这里和 Spring 论坛上有很多帖子要求使用流畅的设置器(例如 Jackson 使用的构建器模式)的支持,但在那之前您必须选择较小的邪恶。
Still not totally ideal, but a little cleaner:
One downside is that you'll have unnecessary bean instances in memory. (I'm using this method, and I'll live with it until Spring decides to handle these). There are many threads here and on the Spring forums asking for support with fluent setters like the builder pattern used by Jackson, but until then you have to choose the lesser evil for you.
我同意 @skaffman 使用 FactoryBean 代替不可避免的复杂 Spring XML bean 配置来配置 Jackson ObjectMapper 的一般方法。 Spring 3.2+ 现在提供了这样一个开箱即用的 FactoryBean。请参阅 JacksonObjectMapperFactoryBean< /a> / Jackson2ObjectMapperFactoryBean 。下面是一个通过 Spring FactoryBean 配置 ObjectMapper 的 Spring XML 示例 -
(注意需要在 'factory-bean' 属性中使用 & 来指示 Spring 使用 FactoryBean 本身的工厂方法,而不是 bean它创造)。
I agree with @skaffman's general approach of using a FactoryBean in place of the unavoidably convoluted Spring XML bean configuration to configure a Jackson ObjectMapper. Spring 3.2+ now provides such a FactoryBean out of the box. See JacksonObjectMapperFactoryBean / Jackson2ObjectMapperFactoryBean. Here's an example of the Spring XML to configure the ObjectMapper via the Spring FactoryBean -
(Note the need to use & in the 'factory-bean' attribute to instruct Spring to use the factory method on the FactoryBean itself, rather than the bean it creates).