初识spring+maven

配置Maven环境

Maven – Download Apache Maven

下载完后解压到一个文件夹中

创建MAVEN_HOME环境变量为D:\MAVEN\apache-maven-3.9.6(这里根据你们自己的解压路径填写)

在用户环境变量和系统环境变量的PATH中新建一个值为:%MAVEN_HOME%\bin

win+r输入cmd,回车进入命令行,输入mvn -v显示下图说明配置完成

配置maven中本地仓库的路径

在D:\MAVEN\apache-maven-3.9.6这个路径下创建一个文件夹名为maven-repo作为本地仓库的位置如下图

再点开其中的conf文件夹下的setting.xml,用记事本打开

ps:如果是根据网课资料进行相关学习,这里最好不要配置镜像源(除非网课有额外说明),因为上课老师一般配置的pom.xml也未使用镜像源配置(而是直接使用官网给出的仓库地址),这会导致maven无法正确下载导入jar包

创建第一个maven+spring项目

//第一个类文件
public class hello {
    public void sayHello(){
        System.out.println("hello world");
    }
}

//第二个类文件
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testHello {

    @Test
    void test(){
        ApplicationContext text  = new ClassPathXmlApplicationContext("hello.xml");
        hello helloWorld = text.getBean("helloWorld", hello.class);
        helloWorld.sayHello();

    }
}
<!--
我的hello.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">

    <!--
    配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
    通过bean标签配置IOC容器所管理的bean
    属性:
        id:设置bean的唯一标识
        class:设置bean所对应类型的全类名
	-->
    <bean id="helloWorld" class="hello"></bean>

</beans>
<!--
项目的pom.xml文件
-->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.example</groupId>
    <artifactId>Spring</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <!--spring context依赖-->
        <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
        </dependency>
    </dependencies>
</project>

按照我这样写好pom.xml文件后,按顺序点击下图操作,如果网络正常,再加载结束后会在外部库中看到一大串的Maven:***,这样就解决了文件中无法解析的问题

然后运行代码,得到正确的类方法输出

至于spring,这里只是正确的导入了spring中有关的包,也算是为后续学习做一个好的铺垫

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值