Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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();
- 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 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: 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: Chapter 4 ■ Using Beans Scopes Th
- 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
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
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();