有没有更好的方法来配置Bean?

发布于 2024-11-19 08:09:05 字数 990 浏览 7 评论 0原文

下面的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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

゛时过境迁 2024-11-26 08:09:05

XML 是一种非常糟糕的方法。是的,您可以做到这一点,但是编写一个FactoryBean来配置您的ObjectMapper要容易得多:

public class MyObjectMapperFactoryBean extends AbstractFactoryBean<ObjectMapper> {
   public Class<ObjectMapper> getObjectType() {
      return ObjectMapper.class;
   }

   public ObjectMapper createInstance() {
      // create and return ObjectMapper 
   }
}

然后在您的XML中:

<bean id="jacksonObjectMapper" class="x.y.MyObjectMapperFactoryBean" />

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 your ObjectMapper:

public class MyObjectMapperFactoryBean extends AbstractFactoryBean<ObjectMapper> {
   public Class<ObjectMapper> getObjectType() {
      return ObjectMapper.class;
   }

   public ObjectMapper createInstance() {
      // create and return ObjectMapper 
   }
}

and then in your XML:

<bean id="jacksonObjectMapper" class="x.y.MyObjectMapperFactoryBean" />
月亮是我掰弯的 2024-11-26 08:09:05

仍然不完全理想,但更干净一些:

<bean id="objectMapperBuilder1" class="org.codehaus.jackson.map.ObjectMapper"/>
<bean id="objectMapperBuilder2" factory-bean="objectMapperBuilder1" factory-method="setSerializationInclusion">
    <constructor-arg value="NON_NULL"/>
</bean>
<bean id="jsonWriter" factory-bean="objectMapperBuilder2" factory-method="writerWithDefaultPrettyPrinter" />
<!-- etc, etc -->

一个缺点是内存中会有不必要的 bean 实例。 (我正在使用这种方法,并且我会一直使用它,直到 Spring 决定处理这些)。这里和 Spring 论坛上有很多帖子要求使用流畅的设置器(例如 Jackson 使用的构建器模式)的支持,但在那之前您必须选择较小的邪恶。

Still not totally ideal, but a little cleaner:

<bean id="objectMapperBuilder1" class="org.codehaus.jackson.map.ObjectMapper"/>
<bean id="objectMapperBuilder2" factory-bean="objectMapperBuilder1" factory-method="setSerializationInclusion">
    <constructor-arg value="NON_NULL"/>
</bean>
<bean id="jsonWriter" factory-bean="objectMapperBuilder2" factory-method="writerWithDefaultPrettyPrinter" />
<!-- etc, etc -->

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.

黎歌 2024-11-26 08:09:05

我同意 @skaffman 使用 FactoryBean 代替不可避免的复杂 Spring XML bean 配置来配置 Jackson ObjectMapper 的一般方法。 Spring 3.2+ 现在提供了这样一个开箱即用的 FactoryBean。请参阅 JacksonObjectMapperFactoryBean< /a> / Jackson2ObjectMapperFactoryBean 。下面是一个通过 Spring FactoryBean 配置 ObjectMapper 的 Spring XML 示例 -

<bean id="jacksonObjectMapper" factory-bean="&jacksonObjectMapperFactoryBean" factory-method="getObject"/>  
<bean id="jacksonObjectMapperFactoryBean" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean"> 
  <property name="featuresToDisable">
    <array>
      <util:constant static-field="org.codehaus.jackson.map.SerializationConfig$Feature.WRITE_NULL_PROPERTIES"/>
    </array>
  </property>
</bean> 

(注意需要在 '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 -

<bean id="jacksonObjectMapper" factory-bean="&jacksonObjectMapperFactoryBean" factory-method="getObject"/>  
<bean id="jacksonObjectMapperFactoryBean" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean"> 
  <property name="featuresToDisable">
    <array>
      <util:constant static-field="org.codehaus.jackson.map.SerializationConfig$Feature.WRITE_NULL_PROPERTIES"/>
    </array>
  </property>
</bean> 

(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文