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 4 ■ Using Beans Scopes<br />

public ServiceSearchEngine(){<br />

if(log.isDebugEnabled())<br />

log.debug("ServiceSearchEngine created: " + this);<br />

}<br />

public DocumentDAO getDocumentDAO() {<br />

return documentDAO;<br />

}<br />

public void setDocumentDAO(DocumentDAO documentDAO) {<br />

if(log.isDebugEnabled())<br />

log.debug("Document DAO set: " + documentDAO);<br />

}<br />

this.documentDAO = documentDAO;<br />

public List findByType(Type documentType) {<br />

List result = new ArrayList();<br />

for(Document doc : listAll()){<br />

if(doc.getType().getName().equals(documentType.getName()))<br />

result.add(doc);<br />

}<br />

return result;<br />

}<br />

public List listAll() {<br />

return Arrays.asList(documentDAO.getAll());<br />

}<br />

}<br />

In Listing 4-1 you have just added a Logger into your ServiceSearchEngine class using the SLF4J library.<br />

Also, you added a constructor for this class, and you have set the level of debug in the same constructor and in the<br />

setter method for your DocumentDAO implementation. With this, every time the container creates an instance, you<br />

should see ServiceSearchEngine create: with some object ID. When the container sets your DocumentDAO<br />

implementation, you should see the output Document DAO set: with some Object ID. For now you haven’t<br />

changed anything in the code; you only added the Logger just to illustrate what the <strong>Spring</strong> container is doing when it<br />

creates your beans.<br />

Now let’s see Listing 4-2, the class MyDocumentTest, because you need to modify it in order to see some of these<br />

bean scopes.<br />

Listing 4-2. MyDocumentTest.java<br />

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

import static org.junit.Assert.*;<br />

import java.util.List;<br />

import org.junit.Before;<br />

import org.junit.Test;<br />

import org.springframework.context.support.ClassPathXmlApplicationContext;<br />

46

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

Saved successfully!

Ooh no, something went wrong!