spring bean显式单例范围声明

时间:2015-03-30 16:16:23

标签: spring

在下面的spring bean声明中,我想知道为什么我们需要显式的scope="singleton"。这不是多余的吗?

<bean class="com.foo.blah.JerseyJacksonConfigFactory" factory-method="getProvider" scope="singleton"/>
public class JerseyJacksonConfigFactory {
    public static ClientConfig getConfig() {
        JacksonJsonProvider jsonProvider = getProvider();
        DefaultClientConfig config = new DefaultClientConfig();
        config.getSingletons().add(jsonProvider);
        return config;
    }

    public static JacksonJsonProvider getProvider() {
        ObjectMapper mapper = getObjectMapper();
        JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
        jsonProvider.setMapper(mapper);
        return jsonProvider;
    }

    public static ObjectMapper getObjectMapper() {
        return new ObjectMapper()
                    .configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                    .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
                    .setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
    }
}

1 个答案:

答案 0 :(得分:0)

实际上,没有必要,因为singleton是默认范围,即使使用静态工厂方法也是如此。我想作者使用它来更好地了解bean的定义。