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:spellChecker
和textEditor
。textEditor
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;@Configurationpublic 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