Eclipse搭建Spring開發環境-


這篇文章簡單介紹下如何利用Eclipse搭建Spring開發環境。

一、軟件准備

1. Eclipse, 下載地址:http://www.eclipse.org,可下載3.6版本

2. SpringIde, 有兩種安裝方法,官網:http://www.springsource.org/node/489

3. Spring Framework: 下載地址:http://www.springsource.org/download (這里使用的是2.5.5,最新為3.0.5)

二、軟件安裝

1. 安裝Eclipse,直接解壓到某個目錄即可,比如我的:E:\SpringDev\eclipse。(注意:使用Eclipse需要安裝Java)

2.安裝SpringIDE,這里介紹兩種方法,一種在線更新:Help->intall new software,更新地址為:http://springide.org/updatesite

第二種方法,下載離線包:http://u.115.com/file/f97474c557,或者備份下載。下載之后把它解壓到Eclipse安裝目錄下即可。

3. 將下載的spring-framework-2.5.5-with-dependencies.zip解壓。將其中的spring.jar(dist 目錄中)、commons-logging.jar(lib\jakarta-commons目錄)、log4j-1.2.15.jar(lib \log4j目錄)這三個文件復制到的”D:\java\Spring\lib” 目錄中,然后在Eclipse中建立一個“Springlib”庫,將那三個文件添加進“Springlib”庫中。關於如何添加用戶庫參考:http://www.cnblogs.com/maplye/archive/2006/06/19/429404.html

這樣就完成了基本配置。接下來我們新建一個例子。該例子屬於《Spring In Action》書中的第一個例子

三、Spring示例

1. 新建Spring Project,取名為HelloWorld。建好之后我們首先先導入用戶庫,導入方法參考這里。這時目錄結果如下圖:

2. 新建interface: GreetingService:

1 package info.leyond.test;
2
3 public interface GreetingService {
4 public void sayGreeting();
5 }

3. 實現該接口:

 1 package info.leyond.test;
2
3 public class GreetingServiceImpl implements GreetingService {
4 private String greeting;
5
6 public GreetingServiceImpl() {}
7
8 public GreetingServiceImpl(String greeting) {
9 this.greeting = greeting;
10 }
11
12 public void sayGreeting() {
13 System.out.println(greeting);
14 }
15
16 public void setGreeting(String greeting) {
17 this.greeting = greeting;
18 }
19 }

4.測試程序:

 1 package info.leyond.test;
2
3 import org.springframework.beans.factory.BeanFactory;
4 import org.springframework.beans.factory.xml.XmlBeanFactory;
5 import org.springframework.core.io.ClassPathResource;
6
7 public class HelloApp {
8 public static void main(String[] args) throws Exception {
9 BeanFactory factory =
10 new XmlBeanFactory(new ClassPathResource("./info/leyond/test/hello.xml"));
11
12 GreetingService greetingService =
13 (GreetingService) factory.getBean("greetingService");
14
15 greetingService.sayGreeting();
16 }
17 }

5. 注意上面的hello.xml,配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7 <bean id="greetingService"
8 class="info.leyond.test.GreetingServiceImpl">
9 <property name="greeting" value="Buenos Dias!" />
10 </bean>
11 </beans>

6. 文件已經准備妥當。此刻看看項目中項目名稱旁邊是否有個S標記。如果沒有,右擊HelloWorld,在彈出菜單中選擇“Add Spring Project Nature”即可。

7.右鍵HelloWorld,選擇properties,然后Spring->bean support->Config Files,如下圖配置:

之后就可以看到hello.xml,以及GreetingServiceImpl.java都掛上了S.

Buenos Dias!

9. 例子代碼下載:http://www.box.net/shared/q7b2xzvxrl

參考資料:

1. http://blog.csdn.net/srx/archive/2005/12/31/567455.aspx

2.http://blog.csdn.net/jawsy/archive/2006/01/06/571934.aspx

3. http://blog.csdn.net/javamxj/archive/2005/06/26/403413.aspx

4.http://www.cnblogs.com/maplye/archive/2006/06/19/429404.html

 

出自:http://www.codecho.com/set-up-spring-dev-environment-using-eclipse/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM