私はいくつかの順序に依存物事アップボックスにファクトリメソッドを持っている:スプリングインジェクションしたい機能は、可変長を取ります。代わりにリストを取得するオーバーロードを提供する必要がありますか?
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>
春が配列に変換し、その後、ArrayListに作成されていますか? 。重大なパフォーマンス上の利点のような過負荷を提供することにあります:
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;
}
(This questionは私がに興味があるものではありません私は可変引数のparamに春のXMLリストを注入することができます知っている。私はちょうどそれ以外の場合は行うには、パフォーマンス上の理由があるかどうかを知りたい)
Springは、価値のあることについて、私のテストで、varargsを取るリストを取るバージョンを選択します。 私の個人的な傾向は、より洗練された、より混乱しにくいAPI(すなわち、ただ1つの機能)を好むことであり、プロファイリングがそれを問題として識別するならば、パフォーマンスについて心配することである。 – jkade
そのスタイルの問題。 2つ目は、直後に破棄される不変のコピーを作成することをはっきりと回避します。 – GhostCat