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 分钟前
Java优化:双重for循环
java·开发语言
IT_陈寒6 分钟前
Element Plus 2.10.0 重磅发布!新增Splitter组件
前端·人工智能·后端
傍晚冰川23 分钟前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习
PingdiGuo_guo26 分钟前
C++智能指针的知识!
开发语言·c++
黄雪超26 分钟前
JVM——打开JVM后门的钥匙:反射机制
java·开发语言·jvm
有梦想的攻城狮34 分钟前
spring中的@RabbitListener注解详解
java·后端·spring·rabbitlistener
李斯维36 分钟前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork38 分钟前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
Java水解38 分钟前
MySQL DQL全面解析:从入门到精通
后端·mysql
Aurora_NeAr39 分钟前
Apache Spark详解
大数据·后端·spark