2016-02-16 37 views
0

當我在解決兩點之間的Dijkstra算法時,我必須在運行它時再次創建圖形對象。我想創建一個Spring MVC應用程序,其中該圖形在啓動時僅作爲一個bean加載一次。Spring MVC:Dijkstra算法如何在啓動時創建一個圖形bean

目前這些都是我的課是這樣的:

public class Graph { 
private final List<Vertex> vertexes; 

public Graph(List<Vertex> vertexes) { 
       this.vertexes = vertexes; 


public List<Vertex> getVertexes() { 
       return vertexes; 
}  
} 

頂點類:

public class Vertex implements Comparable<Vertex> { 

final private Integer id; 
final private String name; 
public List<Edge> adjacencies; 
public double minDistance = Double.POSITIVE_INFINITY; 
public Vertex previous; 

public Vertex(Integer id, String name) { 
      this.id = id; 
      this.name = name; 
      adjacencies = new LinkedList<Edge>(); 
     } 

public Integer getId() { 
     return id; 
} 

public String getName() { 
     return name; 
} 

@Override 
public String toString() { 
    return id+name; 
} 

public int compareTo(Vertex other) { 
    return Double.compare(minDistance, other.minDistance); 
} 

}

邊緣階層:

public class Edge { 
private final String id; 
private final Vertex destination; 
private final double weight; 

public Edge(String id, Vertex destination, double weight) { 
     this.id = id; 
     this.destination = destination; 
     this.weight = weight; 
} 

public String getId() { 
     return id; 
} 


public Vertex getDestination() { 
     return destination; 
} 

public double getWeight() { 
     return weight; 
} 

} 

在我的主方法,我填充頂點列表包含274個頂點元素。 Graph類然後在其構造函數中使用此列表。我如何創建這個單一的圖形對象作爲一個bean?這是我得到的。

<bean id="graph" class="com.fdm.model.Graph" > 
<constructor-arg ref="list"/> 
</bean> 

<util:list id="list" list-class="java.util.ArrayList" /> 

但我不確定如何進一步處理。上面的列表不是類型頂點?

+0

定義'Graph'靜態這份名單的範圍是單 ? –

回答

0

在這裏,您已經在應用程序上下文中創建了一個具有單例作用域的bean。 而且你還有線作爲構造參數引用Vertexes bean的列表。

<bean id="graph" class="com.fdm.model.Graph" > 
<constructor-arg ref="list"/> 
</bean> 

默認情況下,spring中的bean是singletons。

在這裏你已經創建了一個id列表的列表bean,在這裏你可以定義頂點的值類型。此外,在這種情況下,如果要定義類型

<util:list id="list" value-type="com.springapp.mvc.Vertex" list-class="java.util.ArrayList" /> 

您還可以看到下面的例子 How to define a List bean in Spring?

相關問題