Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 3 ■ Applying Different Configurations type = new Type(); type.setName("WEB"); type.setDesc("Web Link"); type.setExtension(".url"); document = new Document(); document.setName("Pro Spring Security Book"); document.setType(type); document.setLocation("http://www.apress.com/9781430248187"); result.add(document); } } return result; So, let’s create a new implementation for your SearchEngine interface and use some of the setter-based dependency injections to add some types. Listing 3-3 is an example of your new SearchEngineService class. Listing 3-3. SearchEngineService.java package com.apress.isf.spring.service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; import com.apress.isf.java.service.SearchEngine; import com.apress.isf.spring.data.DocumentDAO; public class SearchEngineService implements SearchEngine { 28 private DocumentDAO documentDAO; public DocumentDAO getDocumentDAO() { return documentDAO; } public void setDocumentDAO(DocumentDAO documentDAO) { this.documentDAO = documentDAO; } public List findByType(Type documentType) { List result = new ArrayList(); for(Document doc : listAll()){ if(doc.getType().getName().equals(documentType.getName())) result.add(doc); } return result; }
Chapter 3 ■ Applying Different Configurations } public List listAll() { return Arrays.asList(documentDAO.getAll()); } Let’s compare Listing 3-2 and Listing 3-3. In Listing 3-3, you got rid of the hard-coded code, you got rid of the storage method, and you are including a new attribute, documentDAO, which will be injected through its setter method, but you need to help the Spring container to know about this new attribute and the class that will contain the data. Next, let’s create the new classes, DocumentDAO and DocumentRepository. See Listing 3-4 and Listing 3-5. Listing 3-4 shows the DocumentDAO, which is the class that holds all the information about your documents that, for now, will be in memory. Of course, the DocumentRepository (see Listing 3-5) is your implementation of it. Listing 3-4. DocumentDAO.java package com.apress.isf.spring.data; import com.apress.isf.java.model.Document; public interface DocumentDAO { public Document[] getAll(); } Listing 3-5 shows your DocumentRepository with four Document properties with their own setters and getters. And yes, you are going to inject four documents through their setters. Listing 3-5. DocumentRepository.java package com.apress.isf.spring.data; import com.apress.isf.java.model.Document; public class DocumentRepository implements DocumentDAO { private Document doc1; private Document doc2; private Document doc3; private Document doc4; public Document getDoc1() { return doc1; } public void setDoc1(Document doc1) { this.doc1 = doc1; } public Document getDoc2() { return doc2; } public void setDoc2(Document doc2) { this.doc2 = doc2; } 29
- Page 1 and 2: THE EXPERT’S VOICE ® IN SPRING
- Page 3 and 4: Contents at a Glance About the Auth
- Page 5 and 6: Introduction This book is an introd
- Page 7 and 8: Part I Spring Framework Basics The
- Page 9 and 10: Chapter 1 ■ Your First Spring App
- Page 11 and 12: Chapter 1 ■ Your First Spring App
- Page 13 and 14: Chapter 1 ■ Your First Spring App
- Page 15 and 16: Chapter 1 ■ Your First Spring App
- Page 17 and 18: Chapter 1 ■ Your First Spring App
- Page 19 and 20: Chapter 2 ■ Working with Classes
- Page 21 and 22: Chapter 2 ■ Working with Classes
- Page 23 and 24: Chapter 2 ■ Working with Classes
- Page 25 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
Chapter 3 ■ Applying Different Configurations<br />
}<br />
public List listAll() {<br />
return Arrays.asList(documentDAO.getAll());<br />
}<br />
Let’s compare Listing 3-2 and Listing 3-3. In Listing 3-3, you got rid of the hard-coded code, you got rid of the<br />
storage method, and you are including a new attribute, documentDAO, which will be injected through its setter method,<br />
but you need to help the <strong>Spring</strong> container to know about this new attribute and the class that will contain the data.<br />
Next, let’s create the new classes, DocumentDAO and DocumentRepository. See Listing 3-4 and Listing 3-5. Listing 3-4<br />
shows the DocumentDAO, which is the class that holds all the information about your documents that, for now, will be<br />
in memory. Of course, the DocumentRepository (see Listing 3-5) is your implementation of it.<br />
Listing 3-4. DocumentDAO.java<br />
package com.apress.isf.spring.data;<br />
import com.apress.isf.java.model.Document;<br />
public interface DocumentDAO {<br />
public Document[] getAll();<br />
}<br />
Listing 3-5 shows your DocumentRepository with four Document properties with their own setters and getters.<br />
And yes, you are going to inject four documents through their setters.<br />
Listing 3-5. DocumentRepository.java<br />
package com.apress.isf.spring.data;<br />
import com.apress.isf.java.model.Document;<br />
public class DocumentRepository implements DocumentDAO {<br />
private Document doc1;<br />
private Document doc2;<br />
private Document doc3;<br />
private Document doc4;<br />
public Document getDoc1() {<br />
return doc1;<br />
}<br />
public void setDoc1(Document doc1) {<br />
this.doc1 = doc1;<br />
}<br />
public Document getDoc2() {<br />
return doc2;<br />
}<br />
public void setDoc2(Document doc2) {<br />
this.doc2 = doc2;<br />
}<br />
29