[英]Spring MVC: Dijkstra's algorithm how to create a bean for graph on startup
At the moment whenever I am solving Dijkstra's algorithm between two points, I have to create the graph object again when I run it. 现在,每当我在两点之间求解Dijkstra的算法时,我都必须在运行它时再次创建图对象。 I want to create a spring MVC application where that graph is loaded just once on startup as a bean.
我想创建一个Spring MVC应用程序,该图在启动时作为Bean仅加载一次。
At the moment these are what my classes look like: 目前,这些是我的班级样子:
public class Graph {
private final List<Vertex> vertexes;
public Graph(List<Vertex> vertexes) {
this.vertexes = vertexes;
public List<Vertex> getVertexes() {
return vertexes;
}
}
Vertex Class: 顶点类别:
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);
}
} }
Edge Class: 边缘等级:
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;
}
}
In my main method, I populate the Vertexes list with 274 Vertex elements. 在我的主要方法中,我用274个顶点元素填充“顶点”列表。 The Graph class then takes this list in its constructor.
然后,Graph类将此列表放入其构造函数中。 How can I create this single graph object as a bean?
如何将单个图形对象创建为Bean? This is a far as I got.
这是我所了解的。
<bean id="graph" class="com.fdm.model.Graph" >
<constructor-arg ref="list"/>
</bean>
<util:list id="list" list-class="java.util.ArrayList" />
But I am unsure how to proceed further. 但是我不确定如何进一步进行。 The list above is not of type vertex?
上面的列表不是顶点类型吗?
Here you have created a bean with singleton scope in application context. 在这里,您已经在应用程序上下文中创建了具有单例作用域的bean。 And also you have wired as a constructor argument refering the list of Vertexes bean.
同样,您已将引用Vertexes bean列表的构造函数作为连线。
<bean id="graph" class="com.fdm.model.Graph" >
<constructor-arg ref="list"/>
</bean>
beans in spring by default are singletons. 春季默认情况下,bean是单例。
Here you have created a list bean with id list and here u can define the value-type of vertex. 在这里,您创建了一个具有id list的列表bean,在这里您可以定义顶点的值类型。 Also in this case the scope of this list is singleton If you want to define the type
同样在这种情况下,此列表的范围为单例如果要定义类型
<util:list id="list" value-type="com.springapp.mvc.Vertex" list-class="java.util.ArrayList" />
You can also see the following examples How to define a List bean in Spring? 您还可以看到以下示例如何在Spring中定义List Bean?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.