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 ■ Using Resource Files<br />

public void setPassword(String password) {<br />

this.password = password;<br />

}<br />

public boolean isAuthorized(String email, String pass){<br />

if(username.equals(email) && password.equals(pass))<br />

return true;<br />

return false;<br />

}<br />

}<br />

As you can see in Listing 6-12, this login and its implementation are trivial for the example you want to do, just do<br />

a basic authorization logic by passing an e-mail and password to the isAuthorized method and returning true if the<br />

both parameters are equal to the injected properties. For now that’s all you need.<br />

Next, let’s add your support class and its bean definition to your XML file, the org.springframework.beans.<br />

factory.config.PropertyPlaceholderConfigurer class. See Listing 6-13 for your XML configuration file.<br />

Listing 6-13. mydocuments-login-context.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

In Listing 6-13, you are using the PropertyPlaceholderConfigurer. This class helps to externalize properties<br />

values and keep them separate from your XML configuration files. These files follow the Java properties convention<br />

and they are useful for customized environment-centric applications by adding values such as password, database<br />

URLs, etc. During runtime, the PropertyPlaceholderConfigurer will take the metadata from the properties file and<br />

replace it where a placeholder is specified. The placeholders have the form of a ${property} format.<br />

In Listing 6-13, the property username will have the value test@mydocuments.com because it was replaced by<br />

using the placeholder ${user.email} matching the property file (see Listing 6-9).<br />

Now, let’s create your unit test for this change (see Listing 6-14).<br />

69

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

Saved successfully!

Ooh no, something went wrong!