关于人工智能:91-Spring框架

3次阅读

共计 3775 个字符,预计需要花费 10 分钟才能阅读完成。

Spring 框架是一个开源的 Java 企业级利用开发框架,它的目标是简化企业应用开发,升高开发难度。Spring 框架蕴含了很多模块,例如:外围容器、数据拜访 / 集成、Web 开发、AOP(面向切面编程)、音讯、测试和整合等。在这一章节,咱们将具体介绍 Spring 框架的外围组件和性能,并通过实例来帮忙你更好地了解。

9.1.1. Spring 外围容器

外围容器是 Spring 框架的根底,它次要蕴含以下几个模块:

  • Spring Core: 提供了框架的根本组成部分,如依赖注入(DI)和管制反转(IoC)。
  • Spring Beans: 提供了 BeanFactory,用于管理应用程序中的对象(Bean)。
  • Spring Context: 建设在 Core 和 Beans 模块之上,提供了更高级的性能,如国际化、事件流传等。
  • Spring Expression Language (SpEL): 一种弱小的表达式语言,用于在运行时查问和操作对象图。

9.1.2. 依赖注入(DI)和管制反转(IoC)

依赖注入和管制反转是 Spring 框架的外围概念,它们有助于咱们实现解耦和模块化。

管制反转(IoC): 将对象之间的依赖关系从代码中分离出来,由容器(如 Spring 容器)来负责对象的创立和依赖关系的保护。

依赖注入(DI): 是实现 IoC 的一种形式,通过将依赖对象传递(注入)到须要它的对象中,从而实现对象之间的解耦。

9.1.2.1. 依赖注入示例

假如咱们有一个 TextEditor 类,它须要一个 SpellChecker 来查看拼写错误。不应用依赖注入的状况下,咱们的代码可能如下:

class SpellChecker {// ... SpellChecker 的实现 ...}

class TextEditor {
    private SpellChecker spellChecker;

    public TextEditor() {spellChecker = new SpellChecker();
    }

    // ... TextEditor 的实现 ...
}

这种状况下,TextEditor类间接依赖于 SpellChecker 类,这导致了紧耦合。咱们能够应用依赖注入来解决这个问题:

class SpellChecker {// ... SpellChecker 的实现 ...}

class TextEditor {
    private SpellChecker spellChecker;

    public TextEditor(SpellChecker spellChecker) {this.spellChecker = spellChecker;}

    // ... TextEditor 的实现 ...
}

当初,TextEditor类不再间接依赖于 SpellChecker 类,而是通过构造函数接管一个 SpellChecker 实例。这样,咱们能够在不批改 TextEditor 类的状况下,为其提供不同的 SpellChecker 实现。这就是依赖注入的根本思维。

9.1.3. Spring Bean

在 Spring 框架中,咱们将应用程序的对象称为 Bean。Bean 是由 Spring IoC 容器实例化、组装和治理的对象。咱们能够通过 XML 或者 Java 注解的形式来配置和治理 Bean。

9.1.3.1. 应用 XML 配置 Bean

以下是一个简略的 XML 配置文件,用于定义一个 SpellChecker 和一个 TextEditor 的 Bean:

<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="spellChecker" class="com.example.SpellChecker"></bean>

    <bean id="textEditor" class="com.example.TextEditor">
        <constructor-arg ref="spellChecker"></constructor-arg>
    </bean>

</beans>

在这个配置文件中,咱们定义了两个 Bean:spellCheckertextEditortextEditor Bean 通过<constructor-arg> 标签注入了 spellChecker Bean。在应用程序中,咱们能够应用ApplicationContext 来获取和应用这些 Bean:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        TextEditor textEditor = (TextEditor) context.getBean("textEditor");
        // ... 应用 textEditor 对象 ...
    }
}
9.1.3.2. 应用 Java 注解配置 Bean

除了应用 XML 来配置 Bean,咱们还能够应用 Java 注解的形式。以下是一个简略的例子:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public SpellChecker spellChecker() {return new SpellChecker();
    }

    @Bean
    public TextEditor textEditor() {return new TextEditor(spellChecker());
    }
}

class SpellChecker {// ... SpellChecker 的实现 ...}

class TextEditor {

    private SpellChecker spellChecker;

    @Autowired
    public TextEditor(SpellChecker spellChecker) {this.spellChecker = spellChecker;}

    // ... TextEditor 的实现 ...
}

在这个例子中,咱们应用了 @Configuration@Bean注解来定义 Bean。@Autowired注解用于主动注入 SpellChecker Bean 到TextEditor 类的构造函数中。在应用程序中,咱们能够应用 AnnotationConfigApplicationContext 来获取和应用这些 Bean:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        TextEditor textEditor = context.getBean(TextEditor.class);
        // ... 应用 textEditor 对象 ...
    }
}

9.1.4. 其余 Spring 性能和模块

Spring 框架还包含许多其余性能和模块,如数据拜访 / 集成(包含 JDBC 和 ORM 反对)、Web 开发(包含 Spring MVC 和 WebFlux)、AOP(面向切面编程)等。在理论的我的项目开发中,咱们通常会应用这些性能和模块来简化开发工作,进步开发效率。

总结

在本节中,咱们介绍了 Spring 框架的外围概念和性能,包含外围容器、依赖注入、管制反转、Bean 的配置和治理等。通过实例,咱们展现了如何应用 Spring 框架来实现解耦和模块化。在理论的我的项目开发中,你须要依据理论需要来抉择适合的性能和模块。心愿这一章节能帮忙你更好地了解和学习 Spring 框架。
举荐浏览:

https://mp.weixin.qq.com/s/dV2JzXfgjDdCmWRmE0glDA

https://mp.weixin.qq.com/s/an83QZOWXHqll3SGPYTL5g

正文完
 0