Spring Hello World 實(shí)例

2022-05-21 15:17 更新

讓我們使用 Spring 框架開始實(shí)際的編程。在你開始使用 Spring 框架編寫第一個例子之前,你必須確保已經(jīng)正確地設(shè)置了 Spring 環(huán)境,正如在 Spring——環(huán)境設(shè)置 教程中如所說的。假設(shè)你有了解一些有關(guān) Eclipse IDE 工作的知識。

因此,讓我們繼續(xù)編寫一個簡單的 Spring 應(yīng)用程序,它將根據(jù)在 Spring Beans 配置文件中配置的信息輸出 “Hello World!” 或其他信息。

第 1 步:創(chuàng)建 Java 項(xiàng)目

第一步是使用 Eclipse IDE 創(chuàng)建一個簡單的 Java 項(xiàng)目。按照選項(xiàng) File -> New -> Project,最后從向?qū)Я斜碇羞x擇 Java Project 向?qū)А,F(xiàn)在,使用向?qū)Т翱趯⒛愕捻?xiàng)目命名為 HelloSpring,如下所示:

Spring Hello World 實(shí)例

一旦你的項(xiàng)目創(chuàng)建成功后,將在 Project Explorer 看到下面的內(nèi)容:

Spring Hello World 實(shí)例

第 2 步:添加必需的庫

第二步讓我們添加 Spring 框架和通用的日志 API 庫到我們的項(xiàng)目中。為了做到這個,在你的項(xiàng)目名稱 HelloSpring 上單擊右鍵,然后在快捷菜單上按照下面可用的選項(xiàng):Build Path -> Configure Build Path 顯示 Java 構(gòu)建路徑窗口,如下所示:

Spring Hello World 實(shí)例

現(xiàn)在,在 Libraries 標(biāo)簽中使用可用的 Add External JARs 按鈕,添加從 Spring 框架和通用日志安裝目錄下面的核心 JAR 文件:

commons-logging-1.1.1

spring-aop-4.1.6.RELEASE

spring-aspects-4.1.6.RELEASE

spring-beans-4.1.6.RELEASE

spring-context-4.1.6.RELEASE

spring-context-support-4.1.6.RELEASE

spring-core-4.1.6.RELEASE

spring-expression-4.1.6.RELEASE

spring-instrument-4.1.6.RELEASE

spring-instrument-tomcat-4.1.6.RELEASE

spring-jdbc-4.1.6.RELEASE

spring-jms-4.1.6.RELEASE

spring-messaging-4.1.6.RELEASE

spring-orm-4.1.6.RELEASE

spring-oxm-4.1.6.RELEASE

spring-test-4.1.6.RELEASE

spring-tx-4.1.6.RELEASE

spring-web-4.1.6.RELEASE

spring-webmvc-4.1.6.RELEASE

spring-webmvc-portlet-4.1.6.RELEASE

spring-websocket-4.1.6.RELEASE

第 3 步:創(chuàng)建源文件

現(xiàn)在讓我們在 HelloSpring 項(xiàng)目下創(chuàng)建實(shí)際的源文件。首先,我們需要創(chuàng)建一個名為 com.tutorialspoint 的包。在 package explore 區(qū)域中的 src 上點(diǎn)擊右鍵,并按照選項(xiàng):New -> Package。

接下來,我們在包 com.tutorialspoint 下創(chuàng)建 HelloWorld.javaMainApp.java 文件。

Spring Hello World 實(shí)例

這里是 HelloWorld.java 文件的內(nèi)容:

package com.tutorialspoint;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

下面是第二個文件 MainApp.java 的內(nèi)容:

package com.tutorialspoint;
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();
   }
}

關(guān)于主要程序有以下兩個要點(diǎn)需要注意:

  • 第一步是我們使用框架 API ClassPathXmlApplicationContext() 來創(chuàng)建應(yīng)用程序的上下文。這個 API 加載 beans 的配置文件并最終基于所提供的 API,它處理創(chuàng)建并初始化所有的對象,即在配置文件中提到的 beans。

  • 第二步是使用已創(chuàng)建的上下文的 getBean() 方法來獲得所需的 bean。這個方法使用 bean 的 ID 返回一個最終可以轉(zhuǎn)換為實(shí)際對象的通用對象。一旦有了對象,你就可以使用這個對象調(diào)用任何類的方法。

第 4 步:創(chuàng)建 bean 的配置文件

你需要創(chuàng)建一個 Bean 的配置文件,該文件是一個 XML 文件,并且作為粘合 bean 的粘合劑即類。這個文件需要在 src 目錄下創(chuàng)建,如下圖所示:

Spring Hello World 實(shí)例

通常開發(fā)人員保存該文件的名稱為 Beans.xml 文件,當(dāng)然你也可以設(shè)置成任何你喜歡的名稱。但是你必須確保這個文件在 CLASSPATH 中是可用的,并在主應(yīng)用程序中使用相同的名稱,而在 MainApp.java 文件中創(chuàng)建應(yīng)用程序的上下文。

Beans.xml 用于給不同的 bean 分配唯一的 ID,并且控制不同值的對象的創(chuàng)建,而不會影響 Spring 的任何源文件。例如,使用下面的文件,你可以為 “message” 變量傳遞任何值,因此你就可以輸出信息的不同值,而不會影響的 HelloWorld.java和MainApp.java 文件。讓我們來看看它是如何工作的:

<?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.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

當(dāng) Spring 應(yīng)用程序被加載到內(nèi)存中時,框架利用了上面的配置文件來創(chuàng)建所有已經(jīng)定義的 beans,并且按照標(biāo)簽的定義為它們分配一個唯一的 ID。你可以使用標(biāo)簽來傳遞在創(chuàng)建對象時使用不同變量的值。

第 5 步:運(yùn)行程序

一旦你完成了創(chuàng)建源代碼和 bean 的配置文件后,就可以準(zhǔn)備編譯和運(yùn)行你的程序了。為了做到這個,請保持 MainApp.Java 文件標(biāo)簽是有效的,并且在 Eclipse IDE 中使用可用的 Run 選項(xiàng),或使用 Ctrl + F11 編譯并運(yùn)行你的應(yīng)用程序 MainApp。如果你的應(yīng)用程序一切都正常,將在 Eclipse IDE 控制臺打印以下信息:

Your Message : Hello World!

恭喜你,你已經(jīng)成功地創(chuàng)建了你的第一個 Spring 應(yīng)用程序。通過更改 “message” 屬性的值并且保持兩個源文件不變,你可以看到上述 Spring 應(yīng)用程序的靈活性。下一步,我們開始在接下來的幾個章節(jié)中做一些更有趣的事情。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號