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 3 ■ Applying Different Configurations<br />

But wait! Bean? What does “bean” mean? In the Java world, this is a concept that has being around since the<br />

Java language was created, so the <strong>Spring</strong> <strong>Framework</strong> team followed the same naming convention. A Java bean must<br />

have some conventions like method naming (set/get/is), construction, and behavior, so it can be reusable and it<br />

can interact with other beans and other classes. Later, in the Java community, the Java bean was transformed to the<br />

well-known POJO: Plain Old Java Object.<br />

The <strong>Spring</strong> <strong>Framework</strong> takes advantage of these conventions to know, create, inject, interact, and even destroy all<br />

the declared classes on its container.<br />

A bean is declared as a tag in the XML file and can contain the attributes described in Table 3-1.<br />

Table 3-1. Bean Tag Attributes<br />

Attribute<br />

id<br />

class<br />

scope<br />

init-method<br />

factory-method<br />

destroy-method<br />

lazy-init<br />

Description<br />

An identifier for the bean. Only one unique ID can be defined.<br />

Points to a concrete class, given the full java package.<br />

Tells the <strong>Spring</strong> container how it will create the bean; by default if this scope property is not<br />

set, the bean will be a singleton instance. Other scopes are prototype (an instance is created<br />

every time the bean is required), request (a single instance is created in each HTTP web<br />

request), and session (a bean is created and lives during the HTTP session).<br />

This is the name of the method that will be called after a bean is created. It’s useful when you<br />

want to set a state after your object is created.<br />

This is the name of the method that will be used to create the bean. In other words, you need<br />

to provide the method that will create the instance of the object, and this method should have<br />

parameters.<br />

This is the name of the method that will be called after you dispose of the bean.<br />

This can be set to true if you want the container to create your bean when it’s being called<br />

or used by you (when you called the getBean method) or maybe later from another instance<br />

class that requires your object.<br />

The <strong>Spring</strong> <strong>Framework</strong> has different ways to add information about your classes and their dependencies, and<br />

how they interact with each other. All of this will be covered throughout the book by adding some features to your<br />

<strong>Spring</strong> application, My Documents.<br />

Listing 3-2 shows your SearchEngine implementation from the previous chapter: MySearchEngine looks like a lot<br />

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

types or more methods? You need to edit and recompile it again and again with any new changes. Too much work!<br />

Listing 3-2. MySearchEngine.java<br />

package com.apress.isf.java.service;<br />

import java.util.ArrayList;<br />

import java.util.List;<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 />

26

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

Saved successfully!

Ooh no, something went wrong!