1. 程式人生 > >Eclipse IDE下的Spring框架使用簡單實例

Eclipse IDE下的Spring框架使用簡單實例

onf 在線 add jdk安裝 .get lns xsd system eve

Eclipse IDE下的Spring框架使用簡單實例


1 準備
Java jdk安裝。

Eclipse軟件安裝。
根據系統安裝32/64版本,選擇Eclipse IDE for Java Developers 進行在線安裝。

Spring框架下載。 

Commons_Logging包下載

2 配置工作
在項目欄右鍵,Build Path->Config Build Path 選擇Librarys -> Add External Jars導入Spring 與Commons_Logging的jar包

3 示例程序
在src文件夾下創建com.manongjc包,在包中分別創建HelloWorld類與MainApp類,代碼如下:

//HelloWorld類
package com.manongjc;

public class HelloWorld {
private String message;

public void setMessage(String message){
this.message = message;
}

public void getMessage(){
System.out.println("Your Message : " + message);
}
}

//MainApp類
package com.manongjc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");

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

obj.getMessage();
}
}

在src文件夾下創建beans.xml文件,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">

<bean id="helloWorld" class="com.manongjc.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>

選中MainApp,點擊運行,可以在Console裏看到輸出的日誌。

Eclipse IDE下的Spring框架使用簡單實例