Idea 创建 Spring 项目(保姆级)

描述信息

最近卷起来,系统学习Spring;俗话说:万事开头难;创建一个Spring项目在网上找了好久没有找到好的方式;摸索了半天产出如下文档。

在 Idea 中新建项目

填写信息如下

生成项目目录结构

pom添加依赖

XML 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.31</version>
    </dependency>
</dependencies>

定义 applicationContext.xml 文件,并添加扫描路径

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
​
    <!-- 定义扫描路径 -->
    <context:component-scan base-package="cn.rollin.spring" />
​
</beans>

添加一个测试类型UserService

在测试类 Main 中进行测试

java 复制代码
public class Main {
    public static void main(String[] args) {
        // 扫描路径
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
​
        // 获取bean
        UserService userService = (UserService) context.getBean("userService");
​
        userService.print();
    }
}

可以看到 userService bean 已经初始化好了,并且执行了print 方法。

相关推荐
飞翔中文网2 分钟前
Java学习笔记之注解
java·笔记·学习
BossFriday4 分钟前
【手撸IM】SycllaDB 消息存储基础
java·分布式·中间件
霸道流氓气质5 分钟前
导入历史跟踪机制实战指南
java·linux·服务器
日取其半万世不竭7 分钟前
Uptime Kuma 应该放哪台机器?
java·docker·容器·https
消失的旧时光-19437 分钟前
Kotlin 协程设计思想(四):launch、async、withContext 到底有什么区别?
java·kotlin·async·launch·withcontext·deferred
夜白宋7 分钟前
【Redis深入】二、高性能
java·前端·redis
空圆小生8 分钟前
Vue3 + Spring Boot 全栈实战:从零搭建在线彩票模拟系统
java·spring boot·后端
devpotato10 分钟前
ArrayList 扩容机制:从源码细节到工程实践
java·list
运维瓦工13 分钟前
DevOps 生态介绍(八):docker &dockerfile 命令介绍及构建项目的第一个镜像
java·docker·devops
yurenpai(27届找实习中)16 分钟前
Spring AI 实战:从零实现 AI 对话的记忆与历史记录管理(附源码级解析)
java·spring·ai·prompt·word