這篇文章主要講解了“MyEclipse如何開發Spring”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“MyEclipse如何開發Spring”吧!
MyEclipse開發Spring入門的操作是什么呢?那么開始我們的講解吧:
1. 新建普通 Java 項目 MySpringTest. 這個過程無需贅述了, 建議建項目的時候將 src 目錄和 bin(或者classes)目錄分開, 另外提示你切換透視圖的時候一定要切換過去到 Java 透視圖, 此時默認會在 Package Explorer 中選中剛才已經建好的 Java Project, 但是背景為灰色.
2. 首先單擊一下左邊的 Package Explorer 中新建的 MySpringTest 項目來使其高亮選中, 接著點擊菜單項 MyEclipse -> Add Spring Capabilities..., 接著會彈出對話框 Add Spring Capabilities 提示你設置當前項目的 Spring 屬性.
對話框的***頁可以選擇全部的 Spring 框架, 這是最保險的做法, 不過我們的例子只需要選中Spring 2.0 Core Libraries 就可以了. 點擊 "Next" 繼續.
第二頁是 Add Spring bean configuration file. 保持默認值不變就可以了. ***點擊 Finish.
3. Spring 的開發沒法自動生成 Bean, 這里大家只好手工來寫了, 也很簡單. 分別復制下面的三個代碼, 然后在 MyEclipse src 目錄上點擊右鍵后選擇菜單項 Paste 就可以生成 Java 類文件了.
public interface Action { public String execute(String str); } public class UpperAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return (getMessage() + str).toUpperCase(); } } import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAction { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); Action bean = (Action) ctx.getBean("theAction"); System.out.println(bean.execute("Rod")); } }
4. 雙擊左側在第2步生成的 applicationContext.xml, 然后選擇菜單項 Window -> Show View -> Other..., 在彈出的對話框中選擇 MyEclipse Enterprise Workbench 節點下的 Spring Beans 子節點打開視圖 Spring Beans. 此視圖講出現在主界面的右下側.
5. 展開此視圖中的 MySpringTest 父節點, 并選中 src/applicationContext.xml 子節點, 在此節點上點擊右鍵并選擇彈出菜單項中的 New Bean 來打開 Create a new Spring bean 對話框, 并按照下圖輸入對應的內容.
Bean Id: [theAction]
Bean class: [UpperAction]
接下來請單擊一下 Tab 面板 Properties 并點擊其中的 Add... 按鈕, 在接下來彈出的 Property Wizard 對話框中按照下圖輸入/選擇內容:
Name: [message]
Spring type: [value]
Type: [java.lang.String]
Value:[Hello_]
***點擊兩次 Finish 按鈕關閉所有向導對話框. 然后點擊菜單 File -> Save. 此時可以看到 applicationContext.xml 的內容如下所示:
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> lazy-init="default" autowire="default" dependency-check="default"> Hello_
然后雙擊 Package Explorer 下 MySpringTest/src/TestAction.java 打開源代碼, 然后點擊菜單 Run -> Run As -> 1. Java Application, 如果沒有錯誤的話將會出現如下的輸入, 您的***個 Hello Spring 運行成功了:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. HELLO_ROD
接著您就可以對著參考書繼續創建類, 修改 applicationContext.xml 做更多的練習了.
開發整合 Hibernate 的關鍵操作點截圖:
1. 在數據庫瀏覽器中選擇反向工程菜單;
2. 對話框的選項說明
感謝各位的閱讀,以上就是“MyEclipse如何開發Spring”的內容了,經過本文的學習后,相信大家對MyEclipse如何開發Spring這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。