1. 程式人生 > 其它 >11.12 Spring開發環境以及第一個Spring程式

11.12 Spring開發環境以及第一個Spring程式

11.12 Spring開發環境以及第一個Spring程式

Spring開發環境搭建

Maven專案的porm.xml匯入Spring依賴

關鍵依賴:

Spring專案是基於Bean的程式設計,所以:

<dependencies>
<!-- 測試相關依賴 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>

<!-- 核心基礎依賴 -->
<!--Spring核心容器Core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<!--Spring上下文依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<!--Spring的Bean依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<!--Spring的M層-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<!--日誌相關-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</dependencies>

依賴小結:

Spring相關:

  • 核心Core依賴

  • 上下文Context依賴

  • 物件Bean依賴

  • 表現層Expression依賴


例項搭建Spring專案

  • 使用maven建立Spring專案

  • 匯入相關依賴

  • 建立Spring配置檔案

  • 建立Java


使用maven建立Spring專案

File--->project--->org.apache.maven下的webapp

匯入相關依賴:

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

<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.junkingboy</groupId>
<artifactId>SpringStudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>SpringStudy Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<!-- 測試相關依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>

<!-- Spring核心依賴 -->
<!--Spring底層Core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<!--Spring上下文依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<!--Spring的Bean物件依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<!--Spring的M層依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<!--日誌相關依賴-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<finalName>SpringStudy</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<
plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement
>
</build>
</project>

src下建立Spring的配置檔案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.junkingboy.HelloWorld">
<!-- 定義bean類的屬性和值 -->
<property name="message" value="Hello World" />
</bean>
</beans>

建立Beans.xml檔案中的bean類--->嚴格按照Spring檔案下的路徑去建立:

package com.junkingboy;

/**
* @description:Spring的測試類
* @data: 2021/11/15 11:11
* @author: Lucifer
*/
public class HelloWorld {
private String message;

/* 提供get和set方法 */
public String getMessage() {
return message;
}

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

作用:

  • 給不同的Bean分配唯一的ID,並給相應的Bean屬性賦值

建立執行測試類:

MainApp.java

package com.junkingboy;

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

/**
* @description:獲取Spring當中的Bean.xml配置檔案下的類和屬性資訊
* @data: 2021/11/15 11:20
* @author: Lucifer
*/
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
System.out.println(obj.getMessage());
}
}

整體呼叫過程:

  • 面向介面程式設計,使用ClasspathXmlApplicationContext類載入Spring的配置檔案Beans.xml

  • 使用ApplicationContext介面接收該物件

  • 在通過介面接收的物件呼叫getBean()方法獲取Bean物件

  • 呼叫Bean物件當中的方法

It's a lonely road!!!