Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!