0

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. I want to create a spring MVC application where that graph is loaded just once on startup as a 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. The Graph class then takes this list in its constructor. How can I create this single graph object as a 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?

user3809938
  • 1,114
  • 3
  • 16
  • 35

1 Answers1

0

Here you have created a bean with singleton scope in application context. And also you have wired as a constructor argument refering the list of Vertexes bean.

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

beans in spring by default are singletons.

Here you have created a list bean with id list and here u can define the value-type of vertex. 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?

Community
  • 1
  • 1
i3rd
  • 66
  • 8