Java Reference
In-Depth Information
Then you create an interface for the Data Access Object (DAO), which is responsible for accessing
data from the database. The getSequence() method loads a Sequence object from the table by its ID,
while the getNextValue() method retrieves the next value of a particular database sequence.
package com.apress.springenterpriserecipes.sequence;
public interface SequenceDao {
public Sequence getSequence(String sequenceId);
public int getNextValue(String sequenceId);
}
In a production application, you should implement this DAO interface using a data access
technology such as JDBC or object/relational mapping. But for testing purposes, let's use maps to
store the sequence instances and values.
package com.apress.springenterpriserecipes.sequence;
...
public class SequenceDaoImpl implements SequenceDao {
private Map<String, Sequence> sequences;
private Map<String, Integer> values;
public SequenceDaoImpl() {
sequences = new HashMap<String, Sequence>();
sequences.put("IT", new Sequence("IT", "30", "A"));
values = new HashMap<String, Integer>();
values.put("IT", 100000);
}
public Sequence getSequence(String sequenceId) {
return sequences.get(sequenceId);
}
public synchronized int getNextValue(String sequenceId) {
int value = values.get(sequenceId);
values.put(sequenceId, value + 1);
return value;
}
}
You also need a service object, acting as a façade, to provide the sequence generation service.
Internally, this service object will interact with the DAO to handle the sequence generation requests.
So it requires a reference to the DAO.
package com.apress.springenterpriserecipes.sequence;
public class SequenceService {
private SequenceDao sequenceDao;
Search WWH ::




Custom Search