25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8<br />

Give Advice to Your <strong>Spring</strong><br />

Application<br />

In this chapter, you will review part of your logic and refactor some of the classes to make the application more<br />

functional. In the previous chapters, I introduced some <strong>Spring</strong> concepts for your application, like dependency<br />

injection, how to use collections, how to load external resources, and how to use Profiles and testing. You did a little<br />

testing with them, but you need to actually see something real, right? So in this chapter you are going to give advice to<br />

your My Documents app.<br />

Advice to My Documents<br />

So what is this “advice” thingy anyway? Remember your SearchEngineService.java class from previous chapters?<br />

You just added logs everywhere in order to find out what was going on in every method (if you were passing the<br />

correct parameter or not, and if the result was what you expected). Now imagine for a moment that this class needs<br />

some new logic added to it, something like a security checkpoint for every method written where only users with<br />

a username and password are allowed to execute certain logic, or maybe audit checks for some methods, or some<br />

particular business logic you have, like having a counter to see how many times a user uses a particular method call.<br />

This new logic would become a nightmare—too much code to add for something that should be simple! You would<br />

end up having too many calls to do security or audit checks, and the same code would be all over the place. Let’s look<br />

at Listing 8-1, which is your SearchEngineService class.<br />

Listing 8-1. SearchEngineService.java<br />

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

import java.util.ArrayList;<br />

import java.util.Arrays;<br />

import java.util.List;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

89

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

Saved successfully!

Ooh no, something went wrong!