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 4<br />

Using Beans Scopes<br />

So far you have an application called My Documents that for now is exposing a service that returns either all the<br />

documents available or it can return a list of documents by specific type. We were talking about the SearchEngine<br />

class implementation and the way you had configured it. Based on your XML configuration, you have set its scope to<br />

be a Singleton class, meaning that every time you require it by invoking the context.getBean method, you always<br />

get the same instance. But what happens if you require different instances, such as another service that is required to<br />

handle a fresh instance of your SearchEngine implementation to use its search methods.<br />

In this chapter, I will talk about how the <strong>Spring</strong> <strong>Framework</strong> instantiates the classes and its dependencies in the<br />

My Documents application, by using your existing code and classes such as the SearchEngine implementation. You<br />

will see what scopes you can use and the benefits of each one.<br />

Scopes<br />

Right now your existing code doesn’t print out any information about what is going on, so let’s start by putting some<br />

logs (recording activity) into your classes so you know what is happening when the <strong>Spring</strong> container creates and uses<br />

them as shown in Listing 4-1.<br />

Listing 4-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 />

public class SearchEngineService implements SearchEngine {<br />

private static final Logger log =<br />

LoggerFactory.getLogger(ServiceSearchEngine.class);<br />

private DocumentDAO documentDAO;<br />

45

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

Saved successfully!

Ooh no, something went wrong!