Spring工程搭建

创建项目

1.使用IDEA创建Maven工程

2.IDEA配置Maven

搭建配置Spring

引入依赖

maven仓库查询网址:MavenRepository

spring基础包:

1.spring-core
2.spring-beans;
3.spring-context;
4.spring-expression

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.13.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.13.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.13.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>5.2.13.RELEASE</version>
    </dependency>
</dependencies>

刷新maven等待它自动下载
libraries中有了所有导入的包表示依赖引入完成
在这里插入图片描述

核心配置文件

我们需要创建一个配置文件,文件目录如下:\src\main\resources\applicationContext.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        https://www.springframework.org/schema/util/spring-util.xsd
       ">

    <!-- bean definitions here -->

</beans>

把他复制到我们的配置文件后左上角会提示“Application context not configured for this file”,点击“Configure application context”,点击OK
在这里插入图片描述

编写代码测试

接口类

新建接口类
在这里插入图片描述
接口类内容如下:

package services;

public interface UserService {
    public void saveUser();
}

实现类

新建实现类
在这里插入图片描述
实现接口并使用快捷键 ALT+回车键 添加接口方法实现
在这里插入图片描述
实现方法内容如下:

package services.impl;

import services.UserService;

public class UserServiceImpl implements UserService {
    public void saveUser() {
        System.out.println("service的save方法执行了");
    }
}

补充配置文件

<!-- 此标签的作用是利用反射机制将UserServiceImpl类的实例交给Spring容器 -->
<bean id="userService" class="services.impl.UserServiceImpl"/>

测试类

新建测试类
在这里插入图片描述
编写测试类main方法

public class DemoTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserService service = (UserService) context.getBean("userService");
        service.saveUser();
    }
}

测试结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值