Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.49 MB, 753 trang )
9799ch02.qxd
5/5/08
4:42 PM
Page 33
CHAPTER 2 ■ INTRODUCTION TO SPRING
Figure 2-5. Creating a Spring project
Figure 2-6. Creating a Spring bean configuration file
33
9799ch02.qxd
34
5/5/08
4:42 PM
Page 34
CHAPTER 2 ■ INTRODUCTION TO SPRING
After choosing the location and name of this bean configuration file, you are asked to
select which XSD namespaces to include in this file, as shown in Figure 2-7.
Figure 2-7. Selecting the XSD namespaces to include in a bean configuration file
If you would like to specify an existing XML file as a Spring bean configuration file, you
can right-click your project and choose Properties. From Beans Support, located in the Spring
category, you can add your XML files, as shown in Figure 2-8.
Figure 2-8. Specifying bean configuration files in a Spring project
9799ch02.qxd
5/5/08
4:42 PM
Page 35
CHAPTER 2 ■ INTRODUCTION TO SPRING
Viewing Spring Beans in Spring Explorer
To better illustrate Spring IDE’s exploring and graphic features, let’s create the following
Holiday class. For simplicity’s sake, I’m omitting the getter and setter methods of this class.
You can easily generate them by choosing Source ➤ Generate Getters and Setters.
package com.apress.springrecipes.hello;
public class Holiday {
private int month;
private int day;
private String greeting;
// Getters and Setters
...
}
Then modify your HelloWorld class to add a holidays property of the java.util.List type
and also its setter method.
package com.apress.springrecipes.hello;
import java.util.List;
public class HelloWorld {
...
private List
public void setHolidays(List
this.holidays = holidays;
}
}
In the bean configuration file, you declare a christmas and a newYear bean whose type is
Holiday. Then you set the holidays property of the helloWorld bean to contain references to
these two beans.
35
9799ch02.qxd
36
5/5/08
4:42 PM
Page 36
CHAPTER 2 ■ INTRODUCTION TO SPRING
To view your beans in Spring Explorer, right-click your bean configuration file and choose
Show In ➤ Spring Explorer, and then you can explore them, as shown in Figure 2-9.
Figure 2-9. Exploring Spring beans in Spring Explorer
In Spring Explorer, you can double-click an element, and your cursor will jump to its corresponding declaration in the bean configuration file. So, you can begin editing this element
very conveniently.
Viewing Spring Beans in Spring Beans Graph
You can right-click a bean element (or its root element) in Spring Explorer and then choose
Open Graph to view your beans in a graph, as shown in Figure 2-10.
9799ch02.qxd
5/5/08
4:42 PM
Page 37
CHAPTER 2 ■ INTRODUCTION TO SPRING
Figure 2-10. Viewing Spring beans in Spring Beans Graph
Using Content Assist in Bean Configuration Files
Spring IDE supports using Eclipse’s content assist feature to edit class names, property names,
and bean name references. The default key combination to activate content assist in Eclipse is
Ctrl+Space. For example, you can use content assist to complete a property name as shown in
Figure 2-11.
Figure 2-11. Using content assist in bean configuration files
Validating Bean Configuration Files
Each time you save your bean configuration file, Spring IDE automatically validates its correctness against bean classes, property names, bean name references, and so on. If there’s any
error in the file, Spring IDE will highlight this error, as shown in Figure 2-12.
37
9799ch02.qxd
38
5/5/08
4:42 PM
Page 38
CHAPTER 2 ■ INTRODUCTION TO SPRING
Figure 2-12. Validating bean configuration files with Spring IDE
Searching Spring Beans
Spring IDE supports searching beans according to certain criteria. From the Search menu,
choose Beans, and then you can search your beans, as shown in Figure 2-13.
Figure 2-13. Searching Spring Beans
9799ch02.qxd
5/5/08
4:42 PM
Page 39
CHAPTER 2 ■ INTRODUCTION TO SPRING
2-6. Summary
In this chapter, you have learned the Spring framework’s overall architecture and major
functions, as well as the general purpose of each subproject. The installation of the Spring
framework is very simple. You just download it and extract it into a directory. You saw an
overview of the contents in each subdirectory of Spring’s installation directory. You also created a very simple Spring project, which demonstrated how a typical Spring project is set up.
This project also defined some project conventions that I will use throughout this book.
The Spring team has created an Eclipse plug-in called Spring IDE to simplify Spring application development. In this chapter, you installed this IDE and learned how to use its basic
bean-supporting features.
In the next chapter, you will learn about the basic bean configuration in the Spring IoC
container.
39
9799ch02.qxd
5/5/08
4:42 PM
Page 40
9799ch03.qxd
5/5/08
4:50 PM
CHAPTER
Page 41
3
Bean Configuration in Spring
I
n this chapter, you will learn basic component configuration in the Spring IoC container. As
the heart of the Spring framework, the IoC container is designed to be highly adaptive and
configurable. It provides a set of facilities to make your component configuration as simple as
possible. So, you can easily configure your components to run in the Spring IoC container.
In Spring, components are also called “beans.” Note that this is a different concept from
the JavaBeans specification defined by Sun. The beans declared in the Spring IoC container
are not necessarily required to be JavaBeans. They can be any POJOs (Plain Old Java Objects).
The term POJO means an ordinary Java object without any specific requirements, such as to
implement a specific interface or to extend a specific base class. This term is used to distinguish lightweight Java components from heavyweight components in other complex
component models (e.g., EJB components in versions prior to 3.0).
Upon finishing this chapter, you will be able to build a complete Java application using
the Spring IoC container. Besides, if you review your old Java applications, you may find that
you can significantly simplify and improve them by using the Spring IoC container.
3-1. Configuring Beans in the Spring IoC Container
Problem
Spring offers a powerful IoC container to manage the beans that make up an application.
To utilize the container services, you have to configure your beans to run in the Spring IoC
container.
Solution
You can configure your beans in the Spring IoC container through XML files, properties files,
or even APIs. This book will concentrate on the XML-based configuration because of its simplicity and maturity. If you are interested in the other methods, you can consult the Spring
documentation to find out more about bean configuration.
Spring allows you to configure your beans in one or more bean configuration files. For a
simple application, you can simply centralize your beans in a single configuration file. But for
a large application with a lot of beans, you should separate them in multiple configuration
files according to their functionalities.
41
9799ch03.qxd
42
5/5/08
4:50 PM
Page 42
CHAPTER 3 ■ BEAN CONFIGURATION IN SPRING
How It Works
Suppose you are going to develop an application for generating sequence numbers. In this
application, there may be many series of sequence numbers to generate for different purposes. Each one of them will have its own prefix, suffix, and initial value. So, you have to create
and maintain multiple generator instances in your application.
Creating the Bean Class
In accordance with the requirements, you create the SequenceGenerator class that has three
properties, prefix, suffix, and initial, that can be injected via setter methods or a constructor. The private field counter is for storing the current numeric value of this generator. Each
time you call the getSequence() method on a generator instance, you will get the last sequence
number with the prefix and suffix joined. You declare this method as synchronized to make it
thread-safe.
package com.apress.springrecipes.sequence;
public class SequenceGenerator {
private
private
private
private
String prefix;
String suffix;
int initial;
int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setInitial(int initial) {
this.initial = initial;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);