Spring学习(二):Hello Spring你的第一个Spring应用

本文详细介绍如何使用JDK 1.8、Maven及IDEA搭建Spring基础环境,并通过具体实例演示如何创建并运行一个简单的Spring应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 开发环境说明

  • jdk 1.8
  • maven
  • IDEA

2. 搭建Spring的基础环境

2.1 新建一个Maven项目

新建maven项目

这里写图片描述

设置项目信息

这里写图片描述

2.2 引入Spring依赖包

pom.xml 设置如下,我们主要引入了Spring的核心依赖包和单元测试的相关包:

<?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>HelloSpring</groupId>
    <artifactId>HelloSpring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.10.RELEASE</spring.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>

        <!-- 核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>

</project>

至此,Spring的基本核心环境已经搭建完成

3. 构建第一个Spring程序

3.1 项目整体结构如下

这里写图片描述

3.2 Task 接口
package blog.spring.hello;

public interface Task {
    void doIt();
}
3.3 WorkTask实现Task接口
package blog.spring.hello;

import org.springframework.stereotype.Component;

@Component
public class WorkTask implements Task {
    public void doIt() {
        System.out.println("工作");
    }
}
3.4 Person类
package blog.spring.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Person {

    private Task task;

    @Autowired
    public Person(Task task) {
        this.task = task;
    }

    public void doTask(){
        task.doIt();
    }
}
3.5 配置类MyConfig
package blog.spring.hello.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "blog.spring.hello")
public class MyConfig {
}
3.6 测试类App
package blog.spring.hello;

import blog.spring.hello.config.MyConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyConfig.class)
public class App {

    @Autowired
    Person person;

    @Test
    public void test(){
        person.doTask();
    }
}

4. 测试

不用太过关心这些注解和配置类如何工作,将鼠标点击到App中的test方法下,右键对该方法进行单元测试:
这里写图片描述

点击运行,如果进度条为绿色,且输出如下则表示你的第一个Spring应用运行成功:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值