自学内容网 自学内容网

Spring 核心概念

引言

在上一篇文章中,我们介绍了 Spring 框架的基本概念,并搭建了一个简单的 Spring 应用。本文将深入探讨 Spring 框架的核心概念,包括 IoC 容器、依赖注入(DI)、Bean 的生命周期、配置元数据等。通过这些概念的学习,你将能够更好地理解和使用 Spring 框架。

1. IoC 容器
1.1 什么是 IoC?

控制反转(Inversion of Control,IoC)是一种设计模式,用于降低代码间的耦合度。在传统的编程模式中,对象的创建和依赖关系的管理通常是由对象自身来完成的。而在 IoC 模式中,这些职责被交给了外部容器来管理。

IoC 的主要优点包括:

  • 降低耦合度:对象不再需要直接创建其依赖的对象,而是通过容器来注入这些依赖。
  • 提高可测试性:通过依赖注入,可以更容易地在测试环境中模拟依赖对象。
  • 提高灵活性:可以通过配置文件或注解来改变对象的行为,而不需要修改代码。
1.2 Spring IoC 容器

Spring 框架提供了两个主要的 IoC 容器:

  • BeanFactory:这是 Spring IoC 容器的最基础实现,提供了基本的依赖注入功能。BeanFactory 接口是 Spring IoC 容器的核心接口。
  • ApplicationContext:这是 BeanFactory 的扩展,提供了更多的企业级功能,如 AOP、事件传播、国际化等。ApplicationContext 接口是大多数 Spring 应用中使用的 IoC 容器。
1.2.1 BeanFactory 示例
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BeanFactoryExample {
    public static void main(String[] args) {
        // 加载配置文件
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

        // 获取 Bean
        HelloWorld helloWorld = (HelloWorld) factory.getBean("helloWorld");

        // 调用方法
        helloWorld.getMessage();
    }
}
1.2.2 ApplicationContext 示例
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextExample {
    public static void main(String[] args) {
        // 加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取 Bean
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

        // 调用方法
        helloWorld.getMessage();
    }
}
2. 依赖注入(DI)
2.1 什么是依赖注入?

依赖注入(Dependency Injection,DI)是 IoC 的一种具体实现方式。通过依赖注入,对象的依赖关系不再由对象自己创建和管理,而是由外部容器在对象创建时注入。

依赖注入有三种主要的方式:

  • 构造器注入:通过构造器传递依赖对象。
  • 设值注入:通过 setter 方法传递依赖对象。
  • 字段注入:通过字段直接注入依赖对象。
2.1.1 构造器注入示例
package com.example;

public class HelloWorld {
    private final String message;

    public HelloWorld(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Message : " + message);
    }
}
<bean id="helloWorld" class="com.example.HelloWorld">
    <constructor-arg value="Hello, Spring!" />
</bean>
2.1.2 设值注入示例
package com.example;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Message : " + message);
    }
}
<bean id="helloWorld" class="com.example.HelloWorld">
    <property name="message" value="Hello, Spring!" />
</bean>
2.1.3 字段注入示例
package com.example;

import org.springframework.beans.factory.annotation.Autowired;

public class HelloWorld {
    @Autowired
    private String message;

    public void getMessage() {
        System.out.println("Message : " + message);
    }
}
<bean id="helloWorld" class="com.example.HelloWorld" />
3. Bean 的生命周期
3.1 Bean 的生命周期阶段

Spring 容器管理 Bean 的生命周期,主要包括以下几个阶段:

  1. 实例化:Spring 容器通过反射创建 Bean 实例。
  2. 属性赋值:Spring 容器为 Bean 的属性赋值。
  3. 初始化:调用初始化方法,如 @PostConstruct 注解的方法或 init-method 属性指定的方法。
  4. 使用:Bean 被应用使用。
  5. 销毁:当容器关闭时,调用销毁方法,如 @PreDestroy 注解的方法或 destroy-method 属性指定的方法。
3.1.1 初始化和销毁方法示例
package com.example;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Message : " + message);
    }

    @PostConstruct
    public void init() {
        System.out.println("Bean is initializing...");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean is destroying...");
    }
}
<bean id="helloWorld" class="com.example.HelloWorld" init-method="init" destroy-method="destroy">
    <property name="message" value="Hello, Spring!" />
</bean>
4. 配置元数据
4.1 XML 配置

Spring 最初使用 XML 文件来配置 Bean 和依赖关系。虽然现在更推荐使用注解配置,但了解 XML 配置仍然是很有帮助的。

4.1.1 XML 配置示例
<?xml version="1.0" encoding="UTF-8"?>
<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="helloWorld" class="com.example.HelloWorld" init-method="init" destroy-method="destroy">
        <property name="message" value="Hello, Spring!" />
    </bean>

</beans>
4.2 注解配置

随着 Java 5 的推出,Spring 引入了注解配置,使得配置更加简洁和灵活。

4.2.1 注解配置示例
package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloWorld helloWorld() {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setMessage("Hello, Spring!");
        return helloWorld;
    }
}
package com.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnotationConfigExample {
    public static void main(String[] args) {
        // 加载配置类
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // 获取 Bean
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        // 调用方法
        helloWorld.getMessage();

        // 关闭上下文
        context.close();
    }
}
5. 总结

通过本文,我们深入探讨了 Spring 框架的核心概念,包括 IoC 容器、依赖注入(DI)、Bean 的生命周期和配置元数据。理解这些概念对于掌握 Spring 框架至关重要。希望本文对你有所帮助,欢迎继续关注后续文章!

6. 扩展阅读

如果你有任何疑问或建议,欢迎在评论区留言交流!


原文地址:https://blog.csdn.net/pjx987/article/details/142751866

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!