Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 6 ■ Using Resource Files then you should have the following output: 2014-02-16 10:12:20,220 DEBUG [main] About to read the Resource file: menu.txt Welcome to My Documents 1. Show all Documents 2. Show all Document's Types 3. Search by Type 4. Quit ■ ■Tip The best part of the Resource class provided by the Spring Framework is that it can locate the resources in the classpath, an external URL, and on the file system: classpath: , URL: http:///, file: /unix/path/resourcefile or file: c:\\windows\\path\\resourcefile. Which one to choose? Well, it will depend on your requirements. Perhaps it is necessary to request access through a remote server for special settings on your beans definitions such as passwords or any other credentials, so the url:http resource will be the right one. But wait, loading a resource file in your unit test (see Listing 6-2)? Well, there are different ways that you can accomplish the same, but of course you need to use this in one of your classes, right? So let’s use Spring’s dependency injection feature, as shown in Listings 6-3, 6-4, and 6-5. Listing 6-3 shows the usage of the org.springframework. core.io.Resource class, the menuFile as a property with its setters and getters. Listing 6-3. Menu.java package com.apress.isf.spring.views; import java.io.IOException; import java.io.InputStream; import java.util.Scanner; import static java.lang.System.out; import org.springframework.core.io.Resource; public class Menu { private Resource menuFile = null; public Resource getMenuFile() { return menuFile; } public void setMenuFile(Resource menuFile) { this.menuFile = menuFile; } public void printMenu(){ try{ InputStream stream = getMenuFile().getInputStream(); Scanner scanner = new Scanner(stream); 63

Chapter 6 ■ Using Resource Files } } while (scanner.hasNext()) { out.println(scanner.nextLine()); } scanner.close(); stream.close(); }catch(IOException e){ e.printStackTrace(); } Next, Listing 6-4 shows your XML configuration. And the important part to remember is the property of your “menu” bean that points to your menu.txt file, located under src/resources/META-INF/data folder. Listing 6-4. mydocuments-resource-injection-context.xml Listing 6-5 shows the unit test that will load the mydocuments-resource-injection-context.xml configuration file (see Listing 6-4). Listing 6-5. MyDocumentsWithResourceInjectionTest.java package com.apress.isf.spring.test; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertNotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.apress.isf.spring.views.Menu; public class MyDocumentsWithResourceInjectionTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsWithResourceInjectionTest.class); private ClassPathXmlApplicationContext context; @Before public void setup(){ 64

Chapter 6 ■ Using Resource Files<br />

then you should have the following output:<br />

2014-02-16 10:12:20,220 DEBUG [main] About to read the Resource file: 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 />

■ ■Tip The best part of the Resource class provided by the <strong>Spring</strong> <strong>Framework</strong> is that it can locate the<br />

resources in the classpath, an external URL, and on the file system: classpath: , URL:<br />

http:///, file: /unix/path/resourcefile or file: c:\\windows\\path\\resourcefile. Which<br />

one to choose? Well, it will depend on your requirements. Perhaps it is necessary to request access through a remote<br />

server for special settings on your beans definitions such as passwords or any other credentials, so the url:http<br />

resource will be the right one.<br />

But wait, loading a resource file in your unit test (see Listing 6-2)? Well, there are different ways that you can<br />

accomplish the same, but of course you need to use this in one of your classes, right? So let’s use <strong>Spring</strong>’s dependency<br />

injection feature, as shown in Listings 6-3, 6-4, and 6-5. Listing 6-3 shows the usage of the org.springframework.<br />

core.io.Resource class, the menuFile as a property with its setters and getters.<br />

Listing 6-3. Menu.java<br />

package com.apress.isf.spring.views;<br />

import java.io.IOException;<br />

import java.io.InputStream;<br />

import java.util.Scanner;<br />

import static java.lang.System.out;<br />

import org.springframework.core.io.Resource;<br />

public class Menu {<br />

private Resource menuFile = null;<br />

public Resource getMenuFile() {<br />

return menuFile;<br />

}<br />

public void setMenuFile(Resource menuFile) {<br />

this.menuFile = menuFile;<br />

}<br />

public void printMenu(){<br />

try{<br />

InputStream stream = getMenuFile().getInputStream();<br />

Scanner scanner = new Scanner(stream);<br />

63

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

Saved successfully!

Ooh no, something went wrong!