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

@Bean<br />

public Type webType(){<br />

return getTypeFromMap("web");<br />

}<br />

@Bean<br />

@Scope("prototype")<br />

public SearchEngine engine(){<br />

ServiceSearchEngine engine = new ServiceSearchEngine();<br />

engine.setDocumentDAO(documentDAO());<br />

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

log.debug("SearchEngine created: " + engine);<br />

}<br />

return engine;<br />

public MyDocumentsContext(){<br />

}<br />

//...Omitted calls<br />

//... Omitted methods<br />

}<br />

Running the unit test, you should get the same results. You can find each of these tests in Chapter 4 of the book’s<br />

companion source code.<br />

Using Bean Scopes in the New GroovyBeanDefinitionReader<br />

Context<br />

Using the new Groovy integration will be as easy as setting the word scope in the mydocuments.groovy file<br />

(see Listing 4-6).<br />

Listing 4-6. mydocuments.groovy<br />

import com.apress.isf.spring.service.ServiceSearchEngine<br />

import com.apress.isf.spring.data.DocumentRepository<br />

import com.apress.isf.java.model.Document<br />

beans {<br />

engine(SearchEngineService){ bean -><br />

bean.scope = "prototype"<br />

documentDAO = ref("documentDAO")<br />

}<br />

51

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

Saved successfully!

Ooh no, something went wrong!