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 6<br />

Using Resource Files<br />

So far, you have been using all your data as bean definitions. Of course, there are better ways to accomplish the same.<br />

In this chapter, you are going to employ a useful feature from the <strong>Spring</strong> <strong>Framework</strong>, using external files as a data<br />

resource for your application.<br />

The resource files will not only help to load the data you are using, but they also can be useful when you want to<br />

separate some configuration that may depend on the environment you are working on. They can also be useful if<br />

you have an application that you want to be shown in a different language base on the user’s browser locale.<br />

Using Resources<br />

You are going to start working on your classes and see how you can use these resource files. These files will just<br />

contain some data that will be displayed when you run the application. First, you are going to add a small feature to<br />

your application, a simple menu for displaying some options so you can have some interaction with it. The menu will<br />

be as simple as showing the following options.<br />

1. Show all documents.<br />

2. Show all document’s types.<br />

3. Search by type.<br />

4. Quit.<br />

Let’s create a text file under the src/main/resources/META-INF/data folder. This will be your menu, as shown<br />

in Listing 6-1.<br />

Listing 6-1. src/main/resources/META-INF/data/menu.txt<br />

Welcome to My Documents<br />

1. Show all Documents<br />

2. Show all Document's Types<br />

3. Search by Type<br />

4. Quit<br />

As you can see, Listing 6-1 is just simple plain text that describes your menu, nothing else. Next, you are going to<br />

modify your test so you can see how your menu can be loaded and print it out into the console. See Listing 6-2.<br />

61

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

Saved successfully!

Ooh no, something went wrong!