Eclipse建立Spring工程

來源:互聯網
上載者:User

Eclipse建立Spring工程

一、建Spring工程: 開啟eclipse,建立一個工程File->New->Project
選擇Web->Dynamic Web Project
點擊next,然後出現如下圖,Project name隨便取吧,為了後文方便,這裡叫kjsb
點擊Finish後,在這個FuckSpring項目上右鍵,new->Folder 建一個檔案夾,名字叫做 lib
現在到解壓後的Spring檔案夾裡面去找個檔案spring-framework-2.5.6-with-dependencies\spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar
和 spring-framework-2.5.6\dist\spring.jar
把commons-logging.jar和spring.jar複製到剛剛建好的那個lib檔案夾裡面(雖然我才看一天的書,但是我覺得複製進去而不直接引用,是因為相對路徑和絕對路徑的問題,方便以後發布)
在eclipse裡面添加引用。在FuckSpring工程上右鍵,選擇Properties,出現如圖:


左邊選擇Java Build Path,中間選擇Libraries ,右邊選擇Add Library。然後在彈出新的框裡面選擇user libaries->new,名字隨便取,比如 LibrariesForFuckSpring,然後確定。在如下畫面,選中LibrariesForFuckSpring,點擊Add JARs



把lib檔案夾下面的commons-logging.jar和spring.jar引用進來。弄好後是這樣的:




二、建立spring設定檔:

在Java Resources裡的src上右鍵,new->other->XML->XML File 建立檔案名稱叫hello.xml
代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="myBean" class="com.hisoft.test.HelloTest">
        <property name="hello">
            <value>Hello World!!!</value>
        </property>
    </bean>
</beans>

在再src上new一個class他的Package名字叫:com.hisoft.test ,name叫做:SpringTest
SpringTest.java的代碼如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


public class SpringTest {
private String hello;


public String getHello() {
return hello;
}


public void setHello(String hello) {
this.hello = hello;
}


public void show() {
System.out.println("--message--" + getHello());
}


public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/hello.xml");


SpringTest fs = (SpringTest) ctx.getBean("myBean");
fs.show();
}


}

最後運行SpringTest.java可以在控制台看到
資訊: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@77c118ae: display name [org.springframework.context.support.FileSystemXmlApplicationContext@77c118ae]; startup date [Sat Nov 15 21:42:44 CST 2014]; root of context hierarchy
十一月 15, 2014 9:42:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from file [F:\H5WS\kjsb\src\hello.xml]
十一月 15, 2014 9:42:44 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
資訊: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@77c118ae]: org.springframework.beans.factory.support.DefaultListableBeanFactory@7305830a
十一月 15, 2014 9:42:44 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7305830a: defining beans [myBean]; root of factory hierarchy
--message--Hello World!!!

現在Eclipse建立Spring項目已經整合完畢。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.