Spring5参考指南:IOC容器
最近在翻译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容器的更多相关文章
- Spring5参考指南:容器扩展
文章目录 BeanPostProcessor自定义bean BeanFactoryPostProcessor自定义配置元数据 使用FactoryBean自定义实例化逻辑 Spring提供了一系列的接口 ...
- Spring5参考指南:Bean的生命周期管理
文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...
- Spring5参考指南:Bean作用域
文章目录 Bean作用域简介 Singleton作用域 Prototype作用域 Singleton Beans 中依赖 Prototype-bean web 作用域 Request scope Se ...
- Spring5参考指南:依赖注入
文章目录 依赖注入 依赖注入的配置详解 depends-on lazy-init 自动装载 方法注入 依赖注入 依赖注入就是在Spring创建Bean的时候,去实例化该Bean构造函数所需的参数,或者 ...
- spring5源码 -- IOC容器设计理念和核心注解的作用
一. spring源码整体脉络介绍及源码编译 二. Spring IOC的源码深入学习 2.1 ioc容器的加载过程(重要) 2.2 bean的生命周期源码深度剖析 2.3 循环依赖总结讲解 2.4 ...
- Spring5参考指南:基于注解的容器配置
文章目录 @Required @Autowired @primary @Qualifier 泛型 @Resource @PostConstruct和@PreDestroy Spring的容器配置可以有 ...
- Spring5参考指南:Bean的创建
文章目录 Spring容器中的Bean Bean的命名 Bean的实例化 Spring容器中的Bean Bean在Spring中就是一个业务组件,我们通过创建各种Bean来完成最终的业务逻辑功能. 在 ...
- Spring5参考指南:SpringAOP简介
文章目录 AOP的概念 Spring AOP简介 Spring AOP通知类型 写过程序的都知道OOP即面向对象编程. 从最开始的面向过程编程,到后面的面向对象编程,程序的编写方式发生了重大的变化,面 ...
- Spring5参考指南: BeanWrapper和PropertyEditor
文章目录 BeanWrapper PropertyEditor BeanWrapper 通常来说一个Bean包含一个默认的无参构造函数,和属性的get,set方法. org.springframewo ...
随机推荐
- 实验十三 MySQL多用户事务管理
实验十三 MySQL多用户事务管理 一. 实验内容: 1. 事务机制的使用 2. 锁机制的使用 二. 实验项目:员工管理数据库 用于企业管理的员工管理数据库,数据库名为YGGL中,YGGL数据库中 ...
- C - Can you solve this equation? HDU - 2199(二分水题)
Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100; ...
- VAuditDemo-任意文件读取
任意文件读取是属于文件操作漏洞的一种. 一般任意文件读取漏洞可以读取配置信息.甚至系统重要文件. 严重的话,就可能导致SSRF,进而漫游内网. 文件操作漏洞 任意文件删除--删除lock 任意文件复制 ...
- CentOS 7 Docker安装
1. uname -a 查询机器信息,确保CPU为64位,且Linux内核在3.10版本以上 2. 更新yum包: yum update 3. 在 /etc/yum.repos.d下创建 docker ...
- 安装elasticsearch-head(源码安装方式)
gitHub 地址 https://github.com/mobz/elasticsearch-head 克隆到本地 进行npm 安装运行 git clone git://github.com/mob ...
- x86汇编利用int 16h中断实现伪多线程输入
x86汇编利用int 16h中断实现伪多线程输入 我们都知道,如果想让一个程序,同时又干这个,又干那个,最好的办法就是多线程.这个在高级语言里面已经用烂了. 但是,DOS是只有单线程的.我如果想让程序 ...
- MTK Android Driver :Camera
MTK Android Driver :camera 1.相关代码位置:mediatek\config\XXXX(红色字为具体的项目名) 文件:ProjectConfig.mk CUSTOM_KERN ...
- wireshark抓包实战(七),数据流追踪
方法一 选中一个包,然后右键选择 "追踪流" ==> "xx流" 方法二 选中某个数据包后,点击 "分析" ===> " ...
- Array(数组)对象-->push() 方法
1.定义和用法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法: array.push(item1, item2, ..., itemX) 参数:item1, item2, ...
- 数据结构和算法(Golang实现)(3)简单入门Golang-流程控制语句
流程控制语句 计算机编程语言中,流程控制语句很重要,可以让机器知道什么时候做什么事,做几次.主要有条件和循环语句. Golang只有一种循环:for,只有一种判断:if,还有一种特殊的switch条件 ...