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

相关推荐
你的人类朋友6 小时前
说说签名与验签
后端
databook6 小时前
Manim实现脉冲闪烁特效
后端·python·动效
RainbowSea7 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea7 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
canonical_entropy10 小时前
AI时代,我们还需要低代码吗?—— 一场关于模型、演化与软件未来的深度问答
后端·低代码·aigc
颜如玉10 小时前
HikariCP:Dead code elimination优化
后端·性能优化·源码
考虑考虑11 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613511 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
bobz96512 小时前
virtio vs vfio
后端
浮游本尊12 小时前
Java学习第22天 - 云原生与容器化
java