建立一個簡單的Spring應用

環境已經安裝完成,接下來建立一個簡單的Spring應用。java

建立Spring應用步驟:spring

  1. 建立一個maven項目
  2. 添加spring庫依賴
  3. 建立Bean類
  4. 添加Bean的xml裝配文件
  5. 建立主類
  6. 運行應用程序

1. 建立一個maven項目

打開Eclipse,若是尚未搭建開發環境,可參照Spring開發環境搭建(Eclipse) ,選擇菜單:File > New > Maven Project,彈出對話框,以下圖操做apache

image

點擊Next,彈出對話框,以下圖操做編程

image

點擊Finish,完成項目建立,項目結構以下:app

image

項目目錄說明:框架

  • src – 源碼目錄
    • main – 代碼
      • java – Java代碼目錄
      • resources – 配置文件等資源目錄
    • test – 測試代碼
  • target – 編譯後的輸出目錄

項目根目錄下的pom.xml文件就是maven的依賴包配置文件。maven

2. 添加spring庫依賴

要把用到的spring模塊添加到項目中來。修改pom.xml,引入以下模塊:測試

  • spring-core
  • spring-beans
  • spring-context

完整的pom.xml文件內容:this

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.qikegu.demo</groupId>
	<artifactId>spring-helloworld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>

	</dependencies>

</project>

接下來將添加代碼,會添加以下文件:spa

  • Customer.java - bean類
  • applicationContext.xml - bean配置文件
  • Hello.java - 主類

最終的項目結構以下圖所示:

image

後面將詳細說明。

3. 建立Bean類

添加Customer Bean類。項目根目錄右鍵彈出菜單,選擇:New -> File, 指定目錄.../src/main/java/com/qikegu/demo,添加Customer.java文件。

Customer.java代碼:

package com.qikegu.demo;

public class Customer { 

    String name; 
    
    public String getName() { 
        return name; 
    }
    
    public void setName(String name) { 
        this.name = name; 
    }
    
    public void displayInfo() { 
        System.out.println("Hello: "+ name); 
    } 
}

這是一個簡單的bean類,包含一個屬性及其getter和setter方法,另外displayInfo()方法會打印客戶名稱。

4. 添加Bean的xml裝配文件

在resources目錄下,添加Bean的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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="customerBean" class="com.qikegu.demo.Customer"> 
		<property name="name" value="奇客谷"></property> 
	</bean>
</beans>
  • <bean>標記爲指定的類定義bean。
  • <property>標記是bean的一個子元素,用於設置Customer類的屬性,設置的屬性值將由IoC容器賦值給Customer類實例。

5. 建立主類

添加主類文件Hello.java,內容以下:

package com.qikegu.demo;

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

public class Hello { 
    public static void main(String[] args) { 
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Customer customerBean = (Customer) context.getBean("customerBean"); 
        customerBean.displayInfo(); 
        
        ((ClassPathXmlApplicationContext) context).close();
    } 
}

6. 運行應用程序

右鍵單擊Hello.java,彈出菜單,選擇Run As > Java Application,輸出:

Hello: 奇客谷

系列教程

  1. Spring 框架介紹
  2. Spring 框架模塊
  3. Spring開發環境搭建(Eclipse)
  4. 建立一個簡單的Spring應用
  5. Spring 控制反轉容器(Inversion of Control – IOC)
  6. 理解依賴注入(DI – Dependency Injection)
  7. Bean XML 配置(1)- 經過XML配置加載Bean
  8. Bean XML 配置(2)- Bean做用域與生命週期回調方法配置
  9. Bean XML 配置(3)- 依賴注入配置
  10. Bean XML 配置(4)- 自動裝配
  11. Bean 註解(Annotation)配置(1)- 經過註解加載Bean
  12. Bean 註解(Annotation)配置(2)- Bean做用域與生命週期回調方法配置
  13. Bean 註解(Annotation)配置(3)- 依賴注入配置
  14. Bean Java配置
  15. Spring 面向切面編程(AOP)
  16. Spring 事件(1)- 內置事件
  17. Spring 事件(2)- 自定義事件
相關文章
相關標籤/搜索