Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 3 ■ Applying Different Configurations But wait! Bean? What does “bean” mean? In the Java world, this is a concept that has being around since the Java language was created, so the Spring Framework team followed the same naming convention. A Java bean must have some conventions like method naming (set/get/is), construction, and behavior, so it can be reusable and it can interact with other beans and other classes. Later, in the Java community, the Java bean was transformed to the well-known POJO: Plain Old Java Object. The Spring Framework takes advantage of these conventions to know, create, inject, interact, and even destroy all the declared classes on its container. A bean is declared as a tag in the XML file and can contain the attributes described in Table 3-1. Table 3-1. Bean Tag Attributes Attribute id class scope init-method factory-method destroy-method lazy-init Description An identifier for the bean. Only one unique ID can be defined. Points to a concrete class, given the full java package. Tells the Spring container how it will create the bean; by default if this scope property is not set, the bean will be a singleton instance. Other scopes are prototype (an instance is created every time the bean is required), request (a single instance is created in each HTTP web request), and session (a bean is created and lives during the HTTP session). This is the name of the method that will be called after a bean is created. It’s useful when you want to set a state after your object is created. This is the name of the method that will be used to create the bean. In other words, you need to provide the method that will create the instance of the object, and this method should have parameters. This is the name of the method that will be called after you dispose of the bean. This can be set to true if you want the container to create your bean when it’s being called or used by you (when you called the getBean method) or maybe later from another instance class that requires your object. The Spring Framework has different ways to add information about your classes and their dependencies, and how they interact with each other. All of this will be covered throughout the book by adding some features to your Spring application, My Documents. Listing 3-2 shows your SearchEngine implementation from the previous chapter: MySearchEngine looks like a lot of code, and there is a lot of data hard-coded that has been added to the class. So what happens if you need to add more types or more methods? You need to edit and recompile it again and again with any new changes. Too much work! Listing 3-2. MySearchEngine.java package com.apress.isf.java.service; import java.util.ArrayList; 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; 26
Chapter 3 ■ Applying Different Configurations public class MySearchEngine implements SearchEngine { @Override public List findByType(Type documentType) { List result = new ArrayList(); for(Document document : storage()){ if(document.getType().getName().equals(documentType.getName())) result.add(document); } return result; } @Override public List listAll() { return storage(); } private List storage(){ List result = new ArrayList(); Type type = new Type(); type.setName("PDF"); type.setDesc("Portable Document Format"); type.setExtension(".pdf"); Document document = new Document(); document.setName("Book Template"); document.setType(type); document.setLocation("/Users/felipeg/Documents/Random/Book Template.pdf"); result.add(document); document = new Document(); document.setName("Sample Contract"); document.setType(type); document.setLocation("/Users/felipeg/Documents/Contracts/Sample Contract.pdf"); result.add(document); type = new Type(); type.setName("NOTE"); type.setDesc("Text Notes"); type.setExtension(".txt"); document = new Document(); document.setName("Clustering with RabbitMQ"); document.setType(type); document.setLocation("/Users/felipeg/Documents/Random/Clustering with RabbitMQ.txt"); result.add(document); 27
- 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: Chapter 3 Applying Different Config
- 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 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- 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
Chapter 3 ■ Applying Different Configurations<br />
public class MySearchEngine implements SearchEngine {<br />
@Override<br />
public List findByType(Type documentType) {<br />
List result = new ArrayList();<br />
for(Document document : storage()){<br />
if(document.getType().getName().equals(documentType.getName()))<br />
result.add(document);<br />
}<br />
return result;<br />
}<br />
@Override<br />
public List listAll() {<br />
return storage();<br />
}<br />
private List storage(){<br />
List result = new ArrayList();<br />
Type type = new Type();<br />
type.setName("PDF");<br />
type.setDesc("Portable Document Format");<br />
type.setExtension(".pdf");<br />
Document document = new Document();<br />
document.setName("Book Template");<br />
document.setType(type);<br />
document.setLocation("/Users/felipeg/Documents/Random/Book Template.pdf");<br />
result.add(document);<br />
document = new Document();<br />
document.setName("Sample Contract");<br />
document.setType(type);<br />
document.setLocation("/Users/felipeg/Documents/Contracts/Sample Contract.pdf");<br />
result.add(document);<br />
type = new Type();<br />
type.setName("NOTE");<br />
type.setDesc("Text Notes");<br />
type.setExtension(".txt");<br />
document = new Document();<br />
document.setName("Clustering with RabbitMQ");<br />
document.setType(type);<br />
document.setLocation("/Users/felipeg/Documents/Random/Clustering with RabbitMQ.txt");<br />
result.add(document);<br />
27