Spring5 简易入门案例

9 篇文章 0 订阅
3 篇文章 0 订阅


目录

方式一: 手动创建spring项目 

1.下载Spring 5 的jar包

2. 创建项目

3. 导入 并设置spring jar包

4.编写User类

5.编写 spring配置文件 

6.编写测试类 (注意文件相对路径) 

7.执行结果:

第二种: 基于maven创建spring项目

1.创建maven项目

2.编写pom.xml文件 导入spring的核心依赖

3.编写 spring配置文件

4.创建主类测试

5.执行结果


环境 以及 包:

JDK 1.8.0_311

IDEA ultimate 2021.2.3

spring-5.3.15-dist

commons-logging-1.2-bin

(PS: maven是导入的, 不一定一样)


方式一: 手动创建spring项目 

1.下载Spring 5 的jar包

Spring的官网是

Spring | Homehttps://spring.io/

我们来到这个页面

https://spring.io/projects/spring-framework#learnhttps://spring.io/projects/spring-framework#learn

我们看到最新版本的稳定版是 5.3.15(GA表示稳定版)

进入这个网址下载

(有点慢)

JFrog (spring.io)https://repo.spring.io/ui/native/release/org/springframework/spring/往下划找到5.3.15

 

选择第一个下载

再进入这个网址 Apache Commons Logging - Download Apache Commons Logginghttps://commons.apache.org/proper/commons-logging/download_logging.cgi

下载这个

按照上述操作, 我们得到了两个压缩包

先解压了一会要用


2. 创建项目


3. 导入 并设置spring jar包

工程下创建 lib 目录,

lib 存放这几个 jar包 (去解压文件夹里找)

第一个文件夹

第二个 (注意看路径)

直接粘贴到 idea 的 lib路径下 

现在我们项目有这些东西

右键模块设置

 选择刚刚那几个 jar包


4.编写User类

新建User类

package com.company;

public class User {
    public void userTest() {
        System.out.println("Hello Spring !");
    }
}

5.编写 spring配置文件 

src下, 创建

id 类似于 前端 css 中的 id 

class 就是包的完整路径

<?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.xsd">
    <bean id="user" class="com.company.User"/>
</beans>


6.编写测试类 (注意文件相对路径) 

package com.company;

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

public class Main {

    public static void main(String[] args) {
	// write your code here
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        User user = context.getBean("user",User.class);
        System.out.println(user);
        user.userTest();
    }
}

7.执行结果:

 成功


第二种: 基于maven创建spring项目

最近学了maven的使用, 发现很方便, 不用手动导包了

1.创建maven项目

创建, 填写相关信息

 

2.编写pom.xml文件 导入spring的核心依赖

(免去下载jar包 手动导包的繁琐)

 

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.16</version>
        </dependency>
</dependencies>

参考下我们的项目结构

spring 的xml配置文件放在 resources目录下

好的, 依赖已经引入了, 现在我们

3.编写 spring配置文件

(类似上文)

<?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.xsd">

    <bean id="bean1" class="com.canyue.HelloSpring"/>

</beans>

4.创建主类测试

! 偷个懒, xml直接获取的是主类  !

(就不用@Test了)

package com.canyue;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author mobeiCanyue
 * Create:  2022-03-06 20:20
 * Describe:
 */
public class HelloSpring {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        HelloSpring bean1 = context.getBean("bean1", HelloSpring.class);
        System.out.println(bean1);
    }
}

再看下我们的项目结构

(有target是因为我运行过产生的文件)

 

5.执行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值