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 配置,减少配置文件冗余,提高开发效率。

相关推荐
zzzll11116 分钟前
Spring Boot 实现数据脱敏:自定义注解 + Jackson 序列化器
java·spring boot·后端
一个初入编程的小白1 小时前
数据结构:二叉树链式结构的实现
数据结构
2501_941875284 小时前
从配置中心到动态管理的互联网工程语法演进与多语言实践分享
开发语言·python
hfywmsj4 小时前
广州餐饮铺位招租决策模型:多因子选址系统设计
开发语言·人工智能·python·广州餐饮铺位招租
vx-Biye_Design5 小时前
SSM伴侣动物伴护星小程序06330-计算机课程设计、毕业设计
spring boot·后端·elasticsearch·小程序·架构·课程设计·idea
努力的lpp6 小时前
php反序列化一(大白话版)
开发语言·web安全·php·ctf·反序列化
2分钟速写快排6 小时前
什么是 RAG?如何用 RAG 实现一个用户记忆?
前端·后端·ai编程
菜鸟~noob2336 小时前
【电子战】第03篇:CFAR(恒虚警)处理【含matlab代码】
开发语言·matlab
面向Google编程7 小时前
Apache Iceberg Variant 类型:v3 半结构化数据不再「二选一」
大数据·后端
lhldsg7 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析