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

相关推荐
比奇堡鱼贩5 分钟前
python第五次作业
开发语言·前端·python
半兽先生21 分钟前
使用 retire.js 自动检测前端 JavaScript 库漏洞
开发语言·前端·javascript
前路不黑暗@44 分钟前
Java项目:Java脚手架项目的地图服务(十)
java·数据库·spring boot·笔记·学习·spring cloud·maven
014-code1 小时前
Redisson 常用技巧
java·redis
java干货1 小时前
明明删了数据,磁盘却满了?
java
之歆1 小时前
HA 高可用集群指南
java·开发语言
lsx2024062 小时前
电子商务网站主机:选择与维护指南
开发语言
样例过了就是过了2 小时前
LeetCode热题100 最大子数组和
数据结构·算法·leetcode
wangluoqi2 小时前
c++ 逆元 小总结
开发语言·c++
BackCatK Chen2 小时前
第十五章 吃透C语言结构与数据形式:struct/union/typedef全解析
c语言·开发语言·数据结构·typedef·结构体·函数指针·联合体