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 方法。

相关推荐
代码AI弗森2 小时前
一文理清楚“算力申请 / 成本测算 / 并发评估”
java·服务器·数据库
Old Uncle Tom2 小时前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
小小小米粒2 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
摇滚侠3 小时前
expdp 查看帮助
java·数据库·oracle
:1213 小时前
java基础
java·开发语言
曹牧4 小时前
Spring:@RequestMapping注解,匹配的顺序与上下文无关
java·后端·spring
daixin88484 小时前
cursor无法正常使用gpt5.5等模型解决方案
java·redis·cursor
韦禾水5 小时前
记录一次项目部署到tomcat的异常
java·tomcat
曦月合一5 小时前
树莓派安装jdk、tomcat、vnc、谷歌浏览器开机自启等环境配置
java·tomcat·树莓派
此剑之势丶愈斩愈烈6 小时前
openssl 自建证书
java