Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!