Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 3 ■ Applying Different Configurations } public Document getDoc3() { return doc3; } public void setDoc3(Document doc3) { this.doc3 = doc3; } public Document getDoc4() { return doc4; } public void setDoc4(Document doc4) { this.doc4 = doc4; } public Document[] getAll() { return new Document[] { doc1, doc2, doc3, doc4 }; } Well, now this looks a little better. You are separating the way you are extracting the data, but how are you doing this? Well, if you recall, you were using some kind of storage method to retrieve the information, right? (See Listing 3-2.) Then you decided to reimplement your SearchEngine interface by adding a new way to get the data, and the solution was to create an interface that will be injected independent of its implementation, making your class more robust and easy to maintain. But let’s see how you are going to modify the XML so the Spring container knows about all of these new modifications. Listing 3-6 is an example of your new modified mydocuments-context.xml configuration file. This configuration holds all the information about your new DocumentDAO class implementation (DocumentRepository) and how it’s being injected in your SearchEngine implementation. Listing 3-6. mydocuments-context.xml 30

Chapter 3 ■ Applying Different Configurations Analyzing Listing 3-6, you can see that you are using some references like the ref attribute to assign a value; there’s a new declaration of the ServiceSearchEngine; you are setting a property, documentDAO; and you are referencing the value to another bean with id "documentDAO". 31

Chapter 3 ■ Applying Different Configurations<br />

}<br />

public Document getDoc3() {<br />

return doc3;<br />

}<br />

public void setDoc3(Document doc3) {<br />

this.doc3 = doc3;<br />

}<br />

public Document getDoc4() {<br />

return doc4;<br />

}<br />

public void setDoc4(Document doc4) {<br />

this.doc4 = doc4;<br />

}<br />

public Document[] getAll() {<br />

return new Document[] { doc1, doc2, doc3, doc4 };<br />

}<br />

Well, now this looks a little better. You are separating the way you are extracting the data, but how are you<br />

doing this? Well, if you recall, you were using some kind of storage method to retrieve the information, right?<br />

(See Listing 3-2.) Then you decided to reimplement your SearchEngine interface by adding a new way to get the data,<br />

and the solution was to create an interface that will be injected independent of its implementation, making your class<br />

more robust and easy to maintain. But let’s see how you are going to modify the XML so the <strong>Spring</strong> container knows<br />

about all of these new modifications. Listing 3-6 is an example of your new modified mydocuments-context.xml<br />

configuration file. This configuration holds all the information about your new DocumentDAO class implementation<br />

(DocumentRepository) and how it’s being injected in your SearchEngine implementation.<br />

Listing 3-6. mydocuments-context.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

30

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

Saved successfully!

Ooh no, something went wrong!