深入理解Spring中的依赖注入原理

简介: 深入理解Spring中的依赖注入原理

深入理解Spring中的依赖注入原理

今天我们来深入探讨一下Spring框架中的依赖注入(Dependency Injection, DI)原理。依赖注入是Spring框架的核心功能之一,它通过控制反转(Inversion of Control, IoC)容器来实现对象之间的解耦,提高代码的可维护性和可测试性。

1. 什么是依赖注入

依赖注入是一种设计模式,用来实现对象之间的依赖关系。传统的面向对象编程中,类与类之间的依赖关系是通过硬编码来实现的,而依赖注入则将这种依赖关系交给外部容器来处理,从而实现了解耦。

2. 依赖注入的基本方式

在Spring中,依赖注入主要有以下几种方式:

  1. 构造函数注入
  2. Setter方法注入
  3. 字段注入

2.1 构造函数注入

构造函数注入是通过构造函数来传递依赖对象的方式。这种方式在对象创建时就明确了依赖关系,具有较高的安全性和不可变性。

package cn.juwatech.service;
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
    public String greet() {
        return "Hello, World!";
    }
}
package cn.juwatech.controller;
import cn.juwatech.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
    private final GreetingService greetingService;
    @Autowired
    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }
    public void printGreeting() {
        System.out.println(greetingService.greet());
    }
}

2.2 Setter方法注入

Setter方法注入是通过Setter方法来传递依赖对象的方式。这种方式适合需要在对象创建后才设置依赖关系的场景。

package cn.juwatech.controller;
import cn.juwatech.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
    private GreetingService greetingService;
    @Autowired
    public void setGreetingService(GreetingService greetingService) {
        this.greetingService = greetingService;
    }
    public void printGreeting() {
        System.out.println(greetingService.greet());
    }
}

2.3 字段注入

字段注入是直接在类的字段上通过注解的方式来传递依赖对象。这种方式简单直接,但不推荐使用,因为它破坏了类的封装性。

package cn.juwatech.controller;
import cn.juwatech.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
    @Autowired
    private GreetingService greetingService;
    public void printGreeting() {
        System.out.println(greetingService.greet());
    }
}

3. Spring IoC容器的工作原理

Spring IoC容器通过读取配置文件或注解扫描来识别需要管理的Bean,并根据Bean的定义创建和初始化它们。容器在创建Bean时会自动处理Bean之间的依赖关系。

3.1 Bean的定义

在Spring中,Bean的定义可以通过XML配置文件、注解和Java配置类来实现。

3.1.1 XML配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="greetingService" class="cn.juwatech.service.GreetingService"/>
    <bean id="greetingController" class="cn.juwatech.controller.GreetingController">
        <constructor-arg ref="greetingService"/>
    </bean>
</beans>

3.1.2 注解

package cn.juwatech.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "cn.juwatech")
public class AppConfig {
}

3.1.3 Java配置类

package cn.juwatech.config;
import cn.juwatech.service.GreetingService;
import cn.juwatech.controller.GreetingController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
    @Bean
    public GreetingService greetingService() {
        return new GreetingService();
    }
    @Bean
    public GreetingController greetingController() {
        return new GreetingController(greetingService());
    }
}

4. Bean的生命周期

Spring IoC容器不仅管理Bean的创建和依赖注入,还管理Bean的整个生命周期,包括初始化和销毁。

4.1 初始化

Bean的初始化可以通过实现InitializingBean接口或使用@PostConstruct注解来实现。

package cn.juwatech.service;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class GreetingService implements InitializingBean {
    @PostConstruct
    public void init() {
        System.out.println("GreetingService initialized");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("GreetingService after properties set");
    }
    public String greet() {
        return "Hello, World!";
    }
}

4.2 销毁

Bean的销毁可以通过实现DisposableBean接口或使用@PreDestroy注解来实现。

package cn.juwatech.service;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Service;
import javax.annotation.PreDestroy;
@Service
public class GreetingService implements DisposableBean {
    @PreDestroy
    public void destroy() {
        System.out.println("GreetingService destroyed");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("GreetingService after destroy");
    }
    public String greet() {
        return "Hello, World!";
    }
}

5. 总结

Spring的依赖注入机制通过IoC容器来管理Bean的创建和依赖关系,从而实现了对象之间的解耦。通过构造函数注入、Setter方法注入和字段注入,我们可以灵活地配置Bean的依赖关系。了解Spring IoC容器的工作原理和Bean的生命周期,可以帮助我们更好地使用Spring框架来开发高质量的应用程序。

相关文章
|
1月前
|
缓存 Java 开发者
【Spring】原理:Bean的作用域与生命周期
本文将围绕 Spring Bean 的作用域与生命周期展开深度剖析,系统梳理作用域的类型与应用场景、生命周期的关键阶段与扩展点,并结合实际案例揭示其底层实现原理,为开发者提供从理论到实践的完整指导。
|
1月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
|
6天前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
93 2
|
2月前
|
Java 关系型数据库 数据库
深度剖析【Spring】事务:万字详解,彻底掌握传播机制与事务原理
在Java开发中,Spring框架通过事务管理机制,帮我们轻松实现了这种“承诺”。它不仅封装了底层复杂的事务控制逻辑(比如手动开启、提交、回滚事务),还提供了灵活的配置方式,让开发者能专注于业务逻辑,而不用纠结于事务细节。
|
6月前
|
存储 人工智能 自然语言处理
RAG 调优指南:Spring AI Alibaba 模块化 RAG 原理与使用
通过遵循以上最佳实践,可以构建一个高效、可靠的 RAG 系统,为用户提供准确和专业的回答。这些实践涵盖了从文档处理到系统配置的各个方面,能够帮助开发者构建更好的 RAG 应用。
2894 114
|
3月前
|
缓存 安全 Java
Spring 框架核心原理与实践解析
本文详解 Spring 框架核心知识,包括 IOC(容器管理对象)与 DI(容器注入依赖),以及通过注解(如 @Service、@Autowired)声明 Bean 和注入依赖的方式。阐述了 Bean 的线程安全(默认单例可能有安全问题,需业务避免共享状态或设为 prototype)、作用域(@Scope 注解,常用 singleton、prototype 等)及完整生命周期(实例化、依赖注入、初始化、销毁等步骤)。 解析了循环依赖的解决机制(三级缓存)、AOP 的概念(公共逻辑抽为切面)、底层动态代理(JDK 与 Cglib 的区别)及项目应用(如日志记录)。介绍了事务的实现(基于 AOP
129 0
|
3月前
|
监控 架构师 NoSQL
spring 状态机 的使用 + 原理 + 源码学习 (图解+秒懂+史上最全)
spring 状态机 的使用 + 原理 + 源码学习 (图解+秒懂+史上最全)
|
5月前
|
前端开发 Java 数据库连接
Spring核心原理剖析与解说
每个部分都是将一种巨大并且复杂的技术理念传达为更易于使用的接口,而这就是Spring的价值所在,它能让你专注于开发你的应用,而不必从头开始设计每一部分。
179 32
|
5月前
|
Java 开发者 Spring
Spring框架 - 深度揭秘Spring框架的基础架构与工作原理
所以,当你进入这个Spring的世界,看似一片混乱,但细看之下,你会发现这里有个牢固的结构支撑,一切皆有可能。不论你要建设的是一座宏大的城堡,还是个小巧的花园,只要你的工具箱里有Spring,你就能轻松搞定。
217 9
|
6月前
|
安全 前端开发 Java
Spring Boot 项目中触发 Circular View Path 错误的原理与解决方案
在Spring Boot开发中,**Circular View Path**错误常因视图解析与Controller路径重名引发。当视图名称(如`login`)与请求路径相同,Spring MVC无法区分,导致无限循环调用。解决方法包括:1) 明确指定视图路径,避免重名;2) 将视图文件移至子目录;3) 确保Spring Security配置与Controller路径一致。通过合理设定视图和路径,可有效避免该问题,确保系统稳定运行。
409 0