我要Spring注入的函数需要varargs。我应该提供一个带有列表的重载吗? [英] A function I want to Spring-inject takes varargs. Should I provide an overload that takes a list instead?

查看:79
本文介绍了我要Spring注入的函数需要varargs。我应该提供一个带有列表的重载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工厂方法来整理一些与订单有关的东西:

I have a factory method to box up some order-dependent Things:

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class ThingBoxProcessor {
    public static ThingBox forInputThings(Thing... thingsToProcess) {
       ThingBox thingBox = new ThingBox();
       for (Thing thing : Lists.reverse(ImmutableList.copyOf(thingsToProcess))) {
          thingBox.store(thing);
       }
       return thingBox;
    }
}

我这样注入:

<bean name="Things" class="ThingBoxProcessor" factory-method="forInputThings">
   <constructor-arg>
      <list>
         <ref bean="ThingTwo" />
         <ref bean="ThingOne" />
         <!-- imagine a couple dozen other things that do different order-sensitive things -->
      </list>
   </constructor-arg>
</bean>

Spring是否正在创建ArrayList,然后将其转换为数组?提供重载是否有任何显着的性能优势,例如:

Is Spring creating an ArrayList, then converting it to an array? Is there any significant performance advantage to offering an overload such as:

    public static ThingBox forInputThings(Thing... thingsToProcess) {
       return forInputThings(ImmutableList.copyOf(thingsToProcess));
    }

    public static ThingBox forInputThings(List<Thing> thingsToProcess) {
       ThingBox thingBox = new ThingBox();
       for (Thing thing : Lists.reverse(thingsToProcess)) {
          thingBox.store(thing);
       }
       return thingBox;
    }

这个问题不是我感兴趣的。我知道我可以将春季xml列表注入varargs参数中;我只想知道是否有性能原因需要这样做。)

(This question isn't what I'm interested in. I know I can inject a spring xml list into a varargs param; I just want to know if there are performance reasons to do otherwise.)

推荐答案

我不会在任何时间解决此问题,因为:

I would not dedicate any time to this problem because:

假定您正在处理范围为Singleton的Spring bean,该操作正在进行

Assuming that you are dealing with singleton scoped Spring beans, this operation is going to happen once.

加上:启动Spring上下文至少需要几秒钟,因为它需要读取配置文件,加载类等。与之相比,在任何合理大小的数组和列表之间来回转换的代价(在某种意义上说,您可能不会在Spring中放入超过50?的参数是合理的),可能会增加大约0,000001 %

Plus: starting up a Spring context will take at least a few seconds since it needs to read config files, load classes, etc. Compared to this the penalty for converting back and forth between arrays and lists of any reasonable size (reasonable in a sense that you would probably not put more than 50? of your parameters into Spring), will likely add around 0,000001% to your boot-speed.

因此,从可靠性,可维护性或类似的角度来看,考虑应该选择哪个选项是有意义的,但是性能-明智的做法实际上与它无关。

So from reliability, maintainability or some similar point of view it makes sense to think about which option you should choose, but performance-wise it is really as irrelevant as it gets.

这篇关于我要Spring注入的函数需要varargs。我应该提供一个带有列表的重载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆