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

相关推荐
float_六七几秒前
SQL中=与IS的区别:关键用法解析
java·数据库·sql
rit8432499几秒前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
Tony_yitao1 小时前
9.华为OD机试真题 - 最长的顺子 - 2024E卷 Java
java·华为od·algorithm
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 非凡物流公司电商物流管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse
菠菠萝宝3 小时前
【Java手搓RAGFlow】-3- 用户认证与权限管理
java·开发语言·人工智能·llm·openai·qwen·rag
print(未来)5 小时前
C++ 与 C# 的性能比较:选择合适的语言进行高效开发
java·开发语言
王者之座7 小时前
java+maven配置yguard的一次实验
java·spring boot·maven
q***61508 小时前
eclipse配置Spring
java·spring·eclipse
Miraitowa_cheems9 小时前
LeetCode算法日记 - Day 106: 两个字符串的最小ASCII删除和
java·数据结构·算法·leetcode·深度优先