Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 6 ■ Using Resource Files Listing 6-8. MyDocumentsWithResourceLoaderInjectionTest.java package com.apress.isf.spring.test; import static org.junit.Assert.assertNotNull; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.apress.isf.spring.views.ResourceLoaderMenu; public class MyDocumentsWithResourceLoaderInjectionTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsWithResourceLoaderInjectionTest.class); private ClassPathXmlApplicationContext context; @Before public void setup(){ context = new ClassPathXmlApplicationContext("META-INF/spring/ mydocuments-resourceloader-injection-context.xml"); } } @Test public void testMenu() { log.debug("Calling the Menu as Resourceloader Injection:"); ResourceLoaderMenu menu = context.getBean(ResourceLoaderMenu.class); assertNotNull(menu); menu.printMenu("classpath:META-INF/data/menu.txt"); } you should have the same result.: 2014-04-23 12:20:43,127 DEBUG [main] Calling the Menu as Resource Injection: Welcome to My Documents 1. Show all Documents 2. Show all Document's Types 3. Search by Type 4. Quit Using Property Files The Spring Framework can help you read properties files that can contain sensitive data such as username, passwords, URL connections, etc. This feature allows you to separate this sensitive data from your XML configuration files, so it will be easy to deploy applications using the correct properties. A simple use case will have different properties files for different environments such as TEST, QA, and Production. 67
Chapter 6 ■ Using Resource Files You are going to add a new interface, a Login class that can help you to authenticate a user providing an e-mail and password and based on the environment you are working on. Let’s start by reviewing Listing 6-9 and Listing 6-10. First, you will create your properties files and see how you can use them. Listing 6-9 shows some Development environment values. Listing 6-9. env_dev.properties user.email=test@mydocuments.com user.password=test123 Listing 6-10 shows some QA environment values. Listing 6-10. env_qa.properties user.email=qa@mydocuments.com user.password=3$aqw1 Let’s continue creating your login interface and its implementation. See Listings 6-11 and 6-12. Listing 6-11 shows a simple interface with only one method that accepts an e-mail and a password. The idea is to authorize a user if these values are correct. Listing 6-11. Login.java package com.apress.isf.java.service; public interface Login { public boolean isAuthorized(String email, String pass); } Listing 6-12. LoginService.java package com.apress.isf.spring.service; import com.apress.isf.java.service.Login; public class LoginService implements Login { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } 68
- 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 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69: 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
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
Chapter 6 ■ Using Resource Files<br />
You are going to add a new interface, a Login class that can help you to authenticate a user providing an<br />
e-mail and password and based on the environment you are working on. Let’s start by reviewing Listing 6-9 and<br />
Listing 6-10. First, you will create your properties files and see how you can use them. Listing 6-9 shows some<br />
Development environment values.<br />
Listing 6-9. env_dev.properties<br />
user.email=test@mydocuments.com<br />
user.password=test123<br />
Listing 6-10 shows some QA environment values.<br />
Listing 6-10. env_qa.properties<br />
user.email=qa@mydocuments.com<br />
user.password=3$aqw1<br />
Let’s continue creating your login interface and its implementation. See Listings 6-11 and 6-12. Listing 6-11<br />
shows a simple interface with only one method that accepts an e-mail and a password. The idea is to authorize a user<br />
if these values are correct.<br />
Listing 6-11. Login.java<br />
package com.apress.isf.java.service;<br />
public interface Login {<br />
public boolean isAuthorized(String email, String pass);<br />
}<br />
Listing 6-12. LoginService.java<br />
package com.apress.isf.spring.service;<br />
import com.apress.isf.java.service.Login;<br />
public class LoginService implements Login {<br />
private String username;<br />
private String password;<br />
public String getUsername() {<br />
return username;<br />
}<br />
public void setUsername(String username) {<br />
this.username = username;<br />
}<br />
public String getPassword() {<br />
return password;<br />
}<br />
68