Spring学习-02课XML配置使用-02maven管理依赖关系

317 阅读1分钟

加入Spring框架

1、Intellij 使用Maven方式创建项目

2、在MVN仓库搜索 spring

3、点击Spring Context 版本里面的maven信息 复制Maven的文字添加到 pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.3</version>
    </dependency>

</dependencies>

4、点击编辑器右侧 Maven 刷新(Reload All Maven Project ) 添加响应jar包

使用Spring

1、创建一个类

2、在resources中 创建springconfing.xml

3、将类以bean的形式添加到springconfig.xml中

4、创建使用

public class IoCTest {
    @Test
    public void test01(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");

        User bean = ioc.getBean(User.class);
        System.out.println(bean);


    }
}

Maven 项目目录结构

image.png `

`

总结

1、只添加spring-context 就把整个jar下载过来的原因是因为下面的依赖关系

image.png

2、Test中类 test方法 加入@Test 注解 根据智能提示 加入 junit 便于测试功能

3、pom.xml中 scope

image.png

4、maven与 spring 版本问题 ,降版本解决。

java: 无法访问org.springframework.context.ApplicationContext 错误的类文件: /D:/repo/org/springframework/spring-context/6.0.3/spring-context-6.0.3.jar!/org/springframework/context/ApplicationContext.class 类文件具有错误的版本 61.0, 应为 52.0 请删除该文件或确保该文件位于正确的类路径子目录中。