Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!