IDEA运用Maven项目搭建Spring

1.新建Maven项目

(1)File → New → Project → Maven → 按默认直接NEXT

在这里插入图片描述

(2)填写项目组GroupId 及项目ArtifactId然后NEXT

在这里插入图片描述

(3)Project name项目名称IDEA会自动识别先前填写ArtifactId,修改好要存放的路径接着FINISH,空的Maven项目就建立完成了

在这里插入图片描述

(4)如果需要添加web模块

在这里插入图片描述
在这里插入图片描述

2.打开pom.xml文件添加Spring依赖

	<!--添加属性,用于统一定义spring版本,以下spring依赖的版本version使用${spring.version}引用-->
    <properties>
        <spring.version>5.2.12.RELEASE</spring.version>
    </properties>

    <dependencies>
    
        <!--spring核心依赖-->
        <!--spring-core、spring-beans、spring-context-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring dao依赖(提供JDBC事务支持)-->
        <!--spring-jdbc、spring-tx-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring web依赖-->
        <!--spring-web、spring-webmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring test依赖-->
        <!--spring-test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
    </dependencies>

第一次下载依赖包可能会比较久,可以使用阿里镜像
配置完成后在External Libraies包中应该有先前添加的依赖包文件
在这里插入图片描述

3.配置spring

在resources资源目录下创建spring.xml配置文件,当pom文件依赖注入成功时会有Spring Config选项

在这里插入图片描述

4.测试spring

(1)创建User测试对象,目录为src.main.java.com.cx.bean.User
package com.cx.bean;

public class User {
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

(2)配置sping.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.xsd">

    <bean id="user" class="com.cx.bean.User">
        <property name="id" value="1"/>
        <property name="username" value="张三"/>
        <property name="password" value="111"/>
    </bean>

</beans>
(3)编写测试代码
package com.cx.test;

import com.cx.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void userTest() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = (User)applicationContext.getBean("user");
        System.out.println(user);
    }
}

此处需要添加junit依赖

(4)测试结果

在这里插入图片描述
即在Maven项目搭建spring配置成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ww空ww

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值