Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 15 and 16: Chapter 1 ■ Your First Spring App
- Page 17 and 18: Chapter 1 ■ Your First Spring App
- Page 19 and 20: Chapter 2 ■ Working with Classes
- Page 21 and 22: Chapter 2 ■ Working with Classes
- Page 23 and 24: Chapter 2 ■ Working with Classes
- Page 25 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
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