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

相关推荐
勤奋的知更鸟6 分钟前
Java编程之组合模式
java·开发语言·设计模式·组合模式
千|寻6 分钟前
【画江湖】langchain4j - Java1.8下spring boot集成ollama调用本地大模型之问道系列(第一问)
java·spring boot·后端·langchain
爱编程的喵20 分钟前
深入理解JavaScript原型机制:从Java到JS的面向对象编程之路
java·前端·javascript
on the way 12331 分钟前
行为型设计模式之Mediator(中介者)
java·设计模式·中介者模式
保持学习ing33 分钟前
Spring注解开发
java·深度学习·spring·框架
techzhi34 分钟前
SeaweedFS S3 Spring Boot Starter
java·spring boot·后端
异常君1 小时前
Spring 中的 FactoryBean 与 BeanFactory:核心概念深度解析
java·spring·面试
weixin_461259411 小时前
[C]C语言日志系统宏技巧解析
java·服务器·c语言
cacyiol_Z1 小时前
在SpringBoot中使用AWS SDK实现邮箱验证码服务
java·spring boot·spring
竹言笙熙1 小时前
Polarctf2025夏季赛 web java ez_check
java·学习·web安全