1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

2-3. Setting Up a Spring Project

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 29



CHAPTER 2 ■ INTRODUCTION TO SPRING



public void hello() {

System.out.println("Hello! " + message);

}

}

To configure a bean of type HelloWorld with a message in the Spring IoC container, you

create the following bean configuration file. According to this book’s convention, let’s create it

in the root of the classpath with the name beans.xml.


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">











Using Beans from the Spring IoC Container

You can write the following Main class to instantiate the Spring IoC container with the bean

configuration file beans.xml, located in the root of the classpath. Then you can get the

helloWorld bean from it for your use.

package com.apress.springrecipes.hello;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("beans.xml");

HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

helloWorld.hello();

}

}

If everything is fine, you should see the following output in your console, preceded by several lines of Spring’s logging messages:

Hello! How are you?



29



9799ch02.qxd



30



5/5/08



4:42 PM



Page 30



CHAPTER 2 ■ INTRODUCTION TO SPRING



2-4. Installing Spring IDE

Problem

You would like to install an IDE that can assist you in developing Spring applications.



Solution

If you are an Eclipse user, you can install Spring’s official Eclipse plug-ins, called Spring IDE

(http://springide.org/). IntelliJ IDEA has similar Spring support features, but they are not as

powerful or updated as Spring IDE. In this book, I will focus on Spring IDE only. There are

three ways you can install Spring IDE:

• Add Spring IDE’s update site (http://springide.org/updatesite/) in Eclipse’s update

manager and install it online.

• Download Spring IDE’s archived update site and install it in Eclipse’s update manager

as a local site.

• Download Spring IDE’s archive and extract it into Eclipse’s installation directory.

Of these, I strongly recommend installing Spring IDE from its update site if your machine

has an Internet connection available. Failing that, you should install it from an archived local

site. These methods can help you to check if all Spring IDE’s dependent plug-ins are installed

already.



How It Works

From Eclipse’s Help menu, choose Software Updates ➤ Find and Install. Eclipse will then ask

you whether you would like to update currently installed features or install new features. For

the first time installing Spring IDE, choose “Search for new features to install.”

Next, you will see a list of existing update sites. If you can’t see Spring IDE’s update site

here, add it by selecting Add Remote Site, and enter the name and URL of this site, as shown in

Figure 2-3.



9799ch02.qxd



5/5/08



4:42 PM



Page 31



CHAPTER 2 ■ INTRODUCTION TO SPRING



Figure 2-3. Adding an update site for Spring IDE

After selecting the mirror site for your update, you should see a list of Spring IDE’s features and their dependencies, as shown in Figure 2-4.



Figure 2-4. Selecting Spring IDE features to install



31



9799ch02.qxd



32



5/5/08



4:42 PM



Page 32



CHAPTER 2 ■ INTRODUCTION TO SPRING



Spring IDE 2.0.2 integrates with two other Eclipse plug-ins: Eclipse Mylyn and AJDT

(AspectJ Development Tools). Mylyn is a task-focused UI that can integrate multiple development tasks and reduce information overload. AJDT is an Eclipse plug-in focused on AspectJ

development. Spring IDE integrates with AJDT to support Spring 2.0 AOP development. You

can optionally install Spring IDE’s integration features with these plug-ins.

Note that if you are using Eclipse 3.3 or later, you should not check the Dependencies category under the Spring IDE feature, which is intended for Eclipse 3.2 only. After that, you can

easily complete the installation by following the instructions step by step.



2-5. Using Spring IDE’s Bean-Supporting Features

Problem

You would like to use Spring IDE’s bean-supporting features to help you develop Spring applications.



Solution

Spring IDE provides rich bean-supporting features that can increase your productivity in

developing Spring applications. By using Spring IDE, you can view your beans in both explorer

and graph mode. Moreover, Spring IDE can assist you in writing the contents of your bean

configuration file and also validating its correctness.



How It Works

Creating a Spring Project

To create a Spring project in Eclipse with Spring IDE installed, from the File menu choose

New ➤ Project, and select Spring Project from the Spring category, as shown in Figure 2-5.

If you would like to turn an existing Java project into a Spring project, select your project,

right-click it, and choose Spring Tools ➤ Add Spring Project Nature to enable Spring IDE support for this project. A Spring project’s icon has a small “S” in the upper-right corner.

Creating a Spring Bean Configuration File

To create a bean configuration file, from the File menu, choose New ➤ Other, and select

Spring Bean Definition from the Spring category, as shown in Figure 2-6.



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



Xem Thêm
Tải bản đầy đủ (.pdf) (753 trang)

×