Day 15:Spring 框架基础

目标

理解 Spring 核心概念,学习依赖注入(DI)和控制反转(IoC)的基本实现方式,并能够配置一个简单的 Spring 项目。

1. 什么是 Spring?

Spring 是一个轻量级的开源框架,旨在解决企业级应用开发中的复杂性。它提供了一种简单、灵活的方式来管理对象之间的依赖关系,促进模块化和可测试性。

2. Spring 的核心模块
  1. Spring IoC(Inversion of Control)容器

    IoC 容器是 Spring 的核心,负责管理对象的创建、配置和生命周期。

    • Bean:由 IoC 容器管理的对象。
    • 依赖注入(DI):通过容器将对象的依赖关系注入到类中。
  2. Spring AOP(Aspect-Oriented Programming)

    提供面向切面编程功能,用于实现横切关注点(如日志、事务管理)。

3. DI 的实现方式
  1. 基于 XML 的配置

    在 Spring 的早期版本中,XML 是主要的配置方式,通过 <bean> 标签定义对象及其依赖关系。

  2. 基于注解的配置(推荐方式)

    • @Component:将类标记为 Spring 管理的组件。
    • @Autowired:自动注入依赖对象。
    • @Configuration 和 @Bean:定义配置类和 Bean。

实践操作:配置一个简单的 Spring 项目

1. 项目结构
复制代码
src
├── com.example
│   ├── service
│   │   ├── GreetingService.java
│   │   └── GreetingServiceImpl.java
│   ├── controller
│   │   └── GreetingController.java
│   └── AppConfig.java
└── Main.java
2. 定义服务类和实现类
复制代码
package com.example.service;

public interface GreetingService {
    String sayHello(String name);
}

package com.example.service;

import org.springframework.stereotype.Service;

@Service  // 标记为 Spring 管理的服务类
public class GreetingServiceImpl implements GreetingService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}
3. 定义控制器类
复制代码
package com.example.controller;

import com.example.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller  // 标记为 Spring 管理的控制器类
public class GreetingController {
    private final GreetingService greetingService;

    @Autowired  // 自动注入 GreetingService 实现类
    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public void greet(String name) {
        System.out.println(greetingService.sayHello(name));
    }
}
4. 配置 Spring 容器
复制代码
package com.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration  // 标记为配置类
@ComponentScan("com.example")  // 扫描指定包及其子包中的组件
public class AppConfig {
}
5. 启动容器并调用方法
复制代码
package com.example;

import com.example.controller.GreetingController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        // 创建 Spring 容器
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // 获取 GreetingController Bean
        GreetingController controller = context.getBean(GreetingController.class);

        // 调用方法
        controller.greet("World");
    }
}

总结

  1. Spring IoC 容器

    • IoC 容器通过配置文件或注解管理对象的创建和依赖关系。
    • 依赖注入(DI)是 Spring 的核心机制。
  2. 基于注解的配置

    • 使用 @Component 标记组件类。
    • 使用 @Autowired 自动注入依赖。
  3. 实际应用场景

    • 模块化开发:将服务、控制器等分离,降低耦合度。
    • 可测试性:通过注入依赖轻松替换实现类或模拟对象。

使用场景

  1. 构建微服务

    使用 Spring Boot 快速构建 RESTful 服务,管理依赖关系并实现模块化开发。

  2. 企业级应用

    在复杂项目中,Spring 提供统一的依赖管理方式,简化开发流程,提高代码可维护性。

  3. 注解驱动开发

    使用注解替代传统 XML 配置,减少配置文件冗余,提高开发效率。

相关推荐
hqxstudying5 分钟前
J2EE模式---服务层模式
java·数据库·后端·spring·oracle·java-ee
GM_82810 分钟前
【最新最完整】SpringAI-1.0.0开发MCP Server,搭建MCP Client 实战笔记(进阶+详细+完整代码)
java·后端·ai编程·springai·mcp
都叫我大帅哥11 分钟前
Java DelayQueue:时间管理大师的终极武器
java
秋千码途19 分钟前
小架构step系列27:Hibernate提供的validator
java·spring·架构·hibernate
都叫我大帅哥20 分钟前
TOGAF迁移规划阶段全解密:从菜鸟到达人的通关秘籍
java
探索java21 分钟前
深入理解 Spring 中的 XmlBeanFactory 原理及实践
java·spring·xmlbeanfactory
奋进的孤狼25 分钟前
【Spring AI】阿里云DashScope灵积模型
人工智能·spring·阿里云·ai·云计算
程序员爱钓鱼29 分钟前
Go语言实战案例-滑动窗口最大值
后端·google·go
Vertira31 分钟前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
Victor3561 小时前
MySQL(163) 如何理解MySQL的隔离级别?
后端