最近在翻译Spring Framework Documentation 5.1.8.RELEASE. 觉得还是可以系统的将Spring5的知识点系统的再整理一下,于是有了这个Spring5参考指南系列,教程会一直更新,翻译也会同步进行,敬请期待。

为什么使用Spring5

Spring经过这么多年的发展,已经成为既定的企业级J2EE标准,其最大的优点即是轻量级和其最核心的IOC容器。 Spring最新的版本已经到了5.1.8,Spring5提供了很多新的特性,个人认为最主要的有3点:

  • 使用JDK8的新特性

    最引人注意的是Spring5使用了JDK8中的lambda表达式,让代码变得更加简洁。

  • 响应式编程支持

    响应式编程是Spring5中最主要的特性之一,响应式编程提供了另一种编程风格,专注于构建对事件做出响应的应用程序。 Spring5 包含响应流和 Reactor。

  • 响应式web框架

    Spring5提供了一个最新的响应式Web框架,Spring WebFlux,在编程理念和使用习惯方面和之前的传统Web框架都有了很大的区别。

当然,我们要拥抱新技术新变化,那么快来学习Spring5吧。

什么是IOC容器

IOC也称为依赖注入(DI)。它是指对象仅通过构造函数参数、工厂方法的参数或从工厂方法构造或返回对象实例后,通过在其上设置的属性来定义其依赖项(即与之一起工作的其他对象)的过程。

简单点说就是通过配置的参数来构造对象,然后通过配置的属性来实例化其依赖对象。一切都是通过配置来完成的,而不是使用通常的Java new方法来创建对象,也不需要手动去查找或者实例化其依赖对象,一切的一切都是通过Spring IOC容器来实现的。

IOC容器的两个主要包是:org.spring framework.beans和org.springframework.context包。

如果想使用IOC容器,下面两个依赖是必须的:

    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.8.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>

IOC容器中有两个非常重要的类:BeanFactory和ApplicationContext。

ApplicationContext是BeanFactory的子类,BeanFactory提供了对Bean的操作接口,ApplicationContext则是表示容器本身,提供了bean操作之外的如AOP接入,事件处理,消息资源接入和应用程序上下文等非常有用的特性。

org.springframework.context.ApplicationContext接口代表着SpringIOC容器,它负责实例化、配置和组装bean。容器通过读取配置元数据获取关于要实例化、配置和组装的对象的指令。配置元数据以XML、Java注释或Java代码来表示。它定义了组成应用程序的对象以及这些对象之间的丰富依赖关系。

如果你是创建一个单体应用,那么Spring提供了两个非常有用的ApplicationContext实现,ClassPathXMLApplicationContext或FileSystemXMLApplicationContext。

ClassPathXMLApplicationContext是从类路径去加载要装载的配置,FileSystemXMLApplicationContext是从文件路径去装载。

你只需要在配置中,定义你需要使用的业务对象(POJO),在创建和初始化ApplicationContext之后,您就拥有了一个完全配置且可执行的系统或应用程序.

配置元数据

配置配置,Spring的本质就是通过配置来展示和构建业务对象,通常来说,我们可以使用XML文件来配置,当然现在我们也可以使用Java注解和Java代码来实现。

Spring配置由容器必须管理的至少一个或多个bean定义组成。基于XML的配置元数据通常使用来表示。Java配置通常在@Configuration中使用@bean注解的方法。

下面是一个基本的基于XML的定义 daos.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
https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountDao"
class="com.flydean.daos.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean> <bean id="itemDao" class="com.flydean.daos.JpaItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean> <!-- more bean definitions for data access objects go here --> </beans>

其中id是bean的唯一标记,class是bean的类路径。

实例化容器

Spring容器有很多种实例化方法,比如上面讲的单体应用的两个类:

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

上面已经列出了daos.xml , 这里我们再列出services.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
https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="petStore" class="com.flydean.services.PetStoreService">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
<constructor-arg ref="accountDao"/>
</bean> <!-- more bean definitions for services go here --> </beans>

service.xml 里面主要定义了对在定义在daos.xml中的bean的引用。这里的引用方式是通过ref. ref引用了daos.xml里面bean的id。

XML嵌套

除了上面例子中在创建ApplicationContext的时候,加载多个xml文件,其实我们也可以在xml中通过import来引入其他的xml文件。

    <import resource="daos.xml"/>

resource配置的是要引入的xml的路径,可以使用相对路径和绝对路径。不建议使用相对路径“…”来引用父目录中的文件。这样做会创建对当前应用程序外部文件的依赖关系。直接使用绝对路径又会影响不同部署环境下文件路径的位置。所以比较好的方法是在运行时根据JVM系统属性解析的“$…”占位符。

groovy bean定义DSL

除了xml定义,Spring也可以使用groovy bean来配置。

下面是daos.groovy的定义:

import com.flydean.daos.JpaAccountDao;
import com.flydean.daos.JpaItemDao; beans{ accountDao(JpaAccountDao){ } itemDao(JpaItemDao){ }
}

很简单,就是定义了2个bean。

下面是services.groovy的定义:

import com.flydean.services.PetStoreService

beans {

    petStore(PetStoreService, accountDao){

        accountDao=accountDao
itemDao=itemDao
}
}

使用容器

ApplicationContext是高级工厂的接口,它能够维护不同bean的注册及其依赖。通过使用方法T getBean(String name, Class requiredType),获取到bean的实例。 ApplicationContext允许您读取bean定义并访问它们,如下例所示:

        // create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml"); // retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class); // use configured instance
List<String> userList = service.getUsernameList();

上面讲到了groovy bean配置, 下面是怎么使用groovy bean:

        // create and configure beans with groovy
//daos.groovy 必须写在services.groovy前面,否则会报bean找不到的错误
ApplicationContext context = new GenericGroovyApplicationContext("daos.groovy","services.groovy"); // retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class); // use configured instance
List<String> userList = service.getUsernameList();

使用groovy的时候请注意, daos.groovy 必须写在services.groovy前面,否则会报bean找不到的错误。

还可以使用XmlBeanDefinitionReader和GenericApplicationContext结合的方式:

        GenericApplicationContext context = new GenericApplicationContext();
//reader with xml
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");

你也可以使用GroovyBeanDefinitionReader来加载Groovy文件,如下所示:

GenericApplicationContext context = new GenericApplicationContext();
new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");

本教程的源代码可以参照:spring5-core-workshop 中的container模块。

更多作者的博客请查看flydean的博客http://www.flydean.com

Spring5参考指南:IOC容器的更多相关文章

  1. Spring5参考指南:容器扩展

    文章目录 BeanPostProcessor自定义bean BeanFactoryPostProcessor自定义配置元数据 使用FactoryBean自定义实例化逻辑 Spring提供了一系列的接口 ...

  2. Spring5参考指南:Bean的生命周期管理

    文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...

  3. Spring5参考指南:Bean作用域

    文章目录 Bean作用域简介 Singleton作用域 Prototype作用域 Singleton Beans 中依赖 Prototype-bean web 作用域 Request scope Se ...

  4. Spring5参考指南:依赖注入

    文章目录 依赖注入 依赖注入的配置详解 depends-on lazy-init 自动装载 方法注入 依赖注入 依赖注入就是在Spring创建Bean的时候,去实例化该Bean构造函数所需的参数,或者 ...

  5. spring5源码 -- IOC容器设计理念和核心注解的作用

    一. spring源码整体脉络介绍及源码编译 二. Spring IOC的源码深入学习 2.1 ioc容器的加载过程(重要) 2.2 bean的生命周期源码深度剖析 2.3 循环依赖总结讲解 2.4 ...

  6. Spring5参考指南:基于注解的容器配置

    文章目录 @Required @Autowired @primary @Qualifier 泛型 @Resource @PostConstruct和@PreDestroy Spring的容器配置可以有 ...

  7. Spring5参考指南:Bean的创建

    文章目录 Spring容器中的Bean Bean的命名 Bean的实例化 Spring容器中的Bean Bean在Spring中就是一个业务组件,我们通过创建各种Bean来完成最终的业务逻辑功能. 在 ...

  8. Spring5参考指南:SpringAOP简介

    文章目录 AOP的概念 Spring AOP简介 Spring AOP通知类型 写过程序的都知道OOP即面向对象编程. 从最开始的面向过程编程,到后面的面向对象编程,程序的编写方式发生了重大的变化,面 ...

  9. Spring5参考指南: BeanWrapper和PropertyEditor

    文章目录 BeanWrapper PropertyEditor BeanWrapper 通常来说一个Bean包含一个默认的无参构造函数,和属性的get,set方法. org.springframewo ...

随机推荐

  1. 【Redis】集群教程(Windows)

    概述 Redis集群数据分片 Redis集群节点通讯 环境准备 搭建Redis集群 测试Redis集群 概述 Redis Cluster provides a way to run a Redis i ...

  2. 三层架构之UI层

    之前已经发表了BLL,DAL,MODEL,三个层的源码 继续UI层: 先简单实现用户的登录及注册 高级操作可按照上一篇文章进行源码完善 如图所示↑ UI层目录文件 Reg.aspx 进行注册操作  & ...

  3. [一、Jmeter5安装及环境配置]

    前言:Jmeter基于Jave底层开发,需要配置Java运行时环境 第一步:首先从Jmeter的官网下载Jmeter,Oracle官网下载Jave; Apache JMeter 5.2.1(需要Jav ...

  4. CentOS-7.6 下搭建 NIS 服务器

    ##服务端配置: ####Server: 192.168.0.178(CentOS 7.6) # systemctl stop firewalld # systemctl disable firewa ...

  5. eolinker测试增强

    地址:https://www.eolinker.com Chrome: https://chrome.google.com/webstore/detail/eolinker/mdbgchaihbacj ...

  6. PTA | 1056 组合数的和 (15分)

    给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...

  7. 在scratch中怎样编写抓蝴蝶游戏

    打开scratch2.0软件,进入工作界面,将语言切换为简体中文:将默认的演员猫删除掉:在新建背景中选择“从背景库中选择背景”: 选择户外,来点一个背景图flower bed,然源后点下面的确定: 背 ...

  8. java web数据库的增删改查详细

    本次课上实验是完成数据库的增删改查. 包括增加用户信息.删除用户信息.多条件查找用户信息.修改用户信息(主要是复选框单选框等的相关操作.) 下面下看一下各个界面的样子. 总页面:显示全部页面:增加页面 ...

  9. PHP如何配置session存储在redis

    当网站用户量增多的时候,正常的session存取就会出现有点慢的问题,如果提高速度呢. 我们可以使用redis去保存session的会话信息. PHP的会话默认是以文件的形式存在的,可以配置到NoSQ ...

  10. sql 案例

    select now();#获取当前系统时间 select now() from dual;#与Oracle兼容 show character set;#产看当前数据库支持的字符集 create da ...