25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ Working with Collections and Custom Types<br />

public void setTypes(Map types) {<br />

this.types = types;<br />

}<br />

public Type findById(String id){<br />

if(log.isDebugEnabled())<br />

log.debug("Start Params: " + id);<br />

Type type = types.get(id);<br />

}<br />

if(log.isDebugEnabled())<br />

log.debug("End Params: " + type);<br />

return type;<br />

}<br />

public Type[] getAll() {<br />

return types.values().toArray(new Type[types.values().size()]);<br />

}<br />

In the TypeDataRepository class (see Listing 5-3) you also removed the different fields about the type. You also<br />

added a Map that can help you to have in memory at least some of the types you need, like a note, a web URL, a PDF<br />

and maybe some Office documents. Furthermore, you implemented the new method findById that the TypeDataDAO<br />

(see Listing 5-2) declares; this method will return the type by just giving the Id (using the get method from the Map<br />

collection). Then you added some logs to see what method you are calling and what parameters (if any) you are<br />

passing. For now it is important to know what is going on; this practice will help to debug your class in case you need<br />

to do so. Of course, there are better ways to debug your class, like using an IDE (integrated development environment)<br />

such as Eclipse or IntelliJ, but for now, let’s do only logs.<br />

Collections in Your XML Configuration<br />

Next, let’s see your XML configuration file, as shown in Listing 5-4.<br />

Listing 5-4. mydocuments-context.xml<br />

<br />

<br />

<br />

<br />

<br />

55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!