Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 4 ■ Using Beans Scopes Table 4-1. Bean Scopes Scope singleton prototype request session globalSession Definition The Spring container returns a single instance. This is the default value. The Spring container returns a new instance every time it is requested. The Spring container returns a new instance on each HTTP request; this is used in a web context. The Spring container returns a new instance on each HTTP session; this is used in a web context. The Spring container returns a single instance per global HTTP session; this is used in a web context. In the next sections, you will see different ways to use these bean scopes. You need to be very careful to choose the right scope because that could impact your application. Using the @Scope Annotation You need to remember that the Spring Framework has different ways to configure its container: XML, annotations, Java configuration classes, and the new GroovyBeanDefinitionReader. If you are going to use either the annotated beans or the Java bean configuration to get the instance of your beans, then it is necessary to use the @Scope annotation. Listing 4-4 shows the modified code. Listing 4-4. AnnotatedSearchEngine.java package com.apress.isf.spring.annotated.service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; 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; import com.apress.isf.spring.service.ServiceSearchEngine; @Service("engine") @Scope("prototype") public class AnnotatedSearchEngine implements SearchEngine { private static final Logger log = LoggerFactory.getLogger(ServiceSearchEngine.class); 49

Chapter 4 ■ Using Beans Scopes @Autowired private DocumentDAO documentDAO; public AnnotatedSearchEngine(){ if(log.isDebugEnabled()) log.debug("AnnotatedSearchEngine created: " + this); } 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; } } public List listAll() { return Arrays.asList(documentDAO.getAll()); } Listing 4-4 shows the @Scope annotation that is placed over your class with a “prototype” value. Also, you can see the use of the Logger. Listing 4-5 shows a Java configuration class and the use of the @Scope annotation. All these configurations are similar whether you use XML, annotation, or the Java configuration class. Listing 4-5. MyDocumentsContext.java package com.apress.isf.spring.config; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; 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; import com.apress.isf.spring.data.DocumentRepository; import com.apress.isf.spring.service.ServiceSearchEngine; @Configuration public class MyDocumentsContext { private static final Logger log = LoggerFactory.getLogger(MyDocumentsContext.class); 50 private Map documents = new HashMap(); private Map types = new HashMap();

Chapter 4 ■ Using Beans Scopes<br />

@Autowired<br />

private DocumentDAO documentDAO;<br />

public AnnotatedSearchEngine(){<br />

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

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

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

}<br />

public List listAll() {<br />

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

}<br />

Listing 4-4 shows the @Scope annotation that is placed over your class with a “prototype” value. Also, you can see<br />

the use of the Logger.<br />

Listing 4-5 shows a Java configuration class and the use of the @Scope annotation. All these configurations are<br />

similar whether you use XML, annotation, or the Java configuration class.<br />

Listing 4-5. MyDocumentsContext.java<br />

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

import java.util.HashMap;<br />

import java.util.Map;<br />

import org.springframework.context.annotation.Bean;<br />

import org.springframework.context.annotation.Configuration;<br />

import org.springframework.context.annotation.Scope;<br />

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

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

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

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

@Configuration<br />

public class MyDocumentsContext {<br />

private static final Logger log =<br />

LoggerFactory.getLogger(MyDocumentsContext.class);<br />

50<br />

private Map documents = new HashMap();<br />

private Map types = new HashMap();

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

Saved successfully!

Ooh no, something went wrong!