对spring个bean进行排序,将bean直接保存在一个集合中

Sort spring beans and save beans directly in a collection

我是 Spring 和 java 的新手。我正在尝试比较两个 Spring bean 属性 值并据此进行排序。

Main.java

public class Main {

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Circle circle = (Circle) context.getBean("circle");
    Rectangle rect = (Rectangle) context.getBean("rectangle");
    Square sq = (Square) context.getBean("square");
    System.out.println("Area of Circle: "+circle.calculateArea());
    System.out.println("Area of Rectangle: "+rect.calculateArea());
    System.out.println("Area of Square: "+sq.calculateArea());
}

}

现在我想根据区域对 spring beans/objects 进行排序并将对象保存在基于区域的集合中。谁能建议在这里使用什么?比较器?如果是这样,我们如何将它用于 spring 个 bean? spring 中是否有任何其他方法可以让生活更轻松?任何人都可以请建议或提供一个例子吗?或类似的话题?

使用多态性。

  1. 创建一个界面(可能叫做ThingsWithArea)。
  2. 在你的每个形状中实现这个接口 classes.
  3. 将豆子存放在 Collection 中(我喜欢 List)。
  4. 创建一个Comparator<ThingsWithArea>class。 其中以面积作为排序标准
  5. 使用比较器对列表进行排序。