Spring 5:以函数式方式注册 Bean
http://www.baeldung.com/spring-5-functional-beans
作者:Loredana Crusoveanu
译者:http://oopsguy.com
1、概述
Spring 5 支持在应用程序上下文中以函数式方式注册 bean。
简单地说,您可以通过在 GenericApplicationContext 类中定义的一个新 registerBean() 方法重载来完成。
让我们来为此功能列举一些例子。
2、Maven 依赖
建立 Spring 5 项目的最快方式是将 spring-boot-start-parent 依赖添加到 pom.xml 中来使用 Spring Boot:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath />
</parent>
我们的示例需要到 spring-boot-starter-web 和 spring-boot-starter-test,且在 JUnit 测试中需要使用到 WebApplicationContext:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
当然,使用函数式的方式来注册一个 bean,Spring Boot 并不是必需的。 我们也可以直接添加 spring-core 依赖 [1]:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RC2</version>
</dependency>
由于 Maven Central 仓库中尚未存在这些版本 [1],我们需要将 Spring Snapshot Repository 添加到 pom.xml 文件中:
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
3、以函数式形式注册 Bean
registerBean() API 可以接收两种类型的函数式接口作为参数:
- 用于创建对象的 Supplier 参数
- 一个 BeanDefinitionCustomizer vararg(可变参数),可用于提供一个或多个 lambda 表达式来自定义 BeanDefinition;此接口有一个 custom() 方法
首先,我们创建一个非常简单的类,使用它来创建 bean:
public class MyService {
public int getRandomNumber() {
return new Random().nextInt(10);
}
}
我们再添加一个 **@SpringBootApplication** 类,可以使用它来运行 JUnit 测试:
@SpringBootApplication
public class Spring5Application {
public static void main(String[] args) {
SpringApplication.run(Spring5Application.class, args);
}
}
接下来,我们可以使用 **@SpringBootTest** 注解设置我们的测试类来创建一个 GenericWebApplicationContext 实例:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class BeanRegistrationTest {
@Autowired
private GenericWebApplicationContext context;
//...
}
我们在示例中使用了 GenericWebApplicationContext 类型,但任何类型的 ApplicationContext 都可以以相同的方式来注册一个 bean。
让我们看看如何使用 lambda 表达式注册一个 bean 以创建实例:
context.registerBean(MyService.class, () -> new MyService());
我们来验证一下可不可以检索到该 bean 并使用它:
MyService myService = (MyService) context.getBean("com.baeldung.functional.MyService");
assertTrue(myService.getRandomNumber() < 10);
在该例子中我们可以看到,如果没有明确定义 bean 的名称,那么它将根据小写的类名来确定。上述方法也可以与一个显式的 bean 名称一起使用:
context.registerBean("mySecondService", MyService.class, () -> new MyService());
接下来,让我们来看看如何通过添加一个 lambda 表达式来自定义注册一个 bean:
context.registerBean("myCallbackService", MyService.class,
() -> new MyService(), bd -> bd.setAutowireCandidate(false));
这个参数是一个函数式回调,我们可以使用它来设置 bean 属性,如 autowire-candidate 标志或 primary 标志。
4、结论
在本教程中,我们了解了如何以函数式方式来注册一个 bean。
该示例的源代码可以在 GitHub 上找到。
原文示例代码
https://github.com/eugenp/tutorials/tree/master/spring-5
译者注
- [1] 原作者在编写该文时 Spring 5 还没有正式版本。就在不久前,Spring 5 已经发布了,您现在可以直接使用最新的 Spring 5 发行版本。
Spring 5:以函数式方式注册 Bean的更多相关文章
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- spring使用注解的方式创建bean ,将组件加入容器中
第一种使用@Bean的方式 1.创建一个bean package com.springbean; public class Person { private String name; private ...
- spring之通过注解方式配置Bean(一)
(1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...
- Spring使用ioc注解方式配置bean
context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...
- Spring初学之注解方式配置bean
直接看代码: UserController.java package spring.beans.annotation.controller; import org.springframework.be ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
- spring学习笔记 星球日one - xml方式配置bean
ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...
随机推荐
- 再起航,我的学习笔记之JavaScript设计模式17(模板方法模式)
模板方法模式 由模板方法模式开始我们正式告别结构型设计模式,开始行为型设计模式的学习分享 行为型设计模式用于不同对象之间职责划分或算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模 ...
- Spring-MVC请求参数值和向页面传值
读取请求参数值 方式一:通过HttpServletRequest req做参数 DispatcherServlet在调用处理的方法之前,利用Java反射分析方法的结构,通过分析,将req对象传过来 方 ...
- JavaScript二维码生成——qrcode.js
在开发中,有时候,我们需要根据不同的内容来动态生成二维码,则可以使用qrcode.js这个小插件来实现. 1.qrcode.js文件内容: (1)未压缩(qrcode.js): /** * @file ...
- KVM网页管理工具WebVirtMgr部署
KVM-WebVirtMgr 0ther https://github.com/retspen/webvirtmgr/wiki System Optimization(Only CentOS6.X) ...
- 【Alpha】第四次Daily Scrum Meeting
GIT 一.今日站立式会议照片 二.会议内容 1.采取老师提出的建议,考虑对送礼对象进行一个分类,这个在服务功能模块中完善. 2.回顾之前几次会议的内容,做一个小的总结,各抒己见,对每个人哪方面做得比 ...
- 团队作业8----第二次项目冲刺(Beta阶段) 第五天
BETA阶段冲刺第五天 1.小会议ing 2.每个人的工作 (1)昨天已完成的工作 文件读取的方式采用按钮的: (2) 今天计划完成的工作 (3) 工作中遇到的困难: 林莹:源代码的部分我们已经初步完 ...
- 201521123001《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...
- 201521123100 《Java程序设计》第3周学习总结
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...
- 201521123017 《Java程序设计》第1周学习总结
1. 本章学习总结 (1)对JAVA的历史发展的了解 (2)JAVA运行环境的搭建和JVM,JDK,JRE的相关的JAVA开发工具的认识及其掌握 (3)写法的不同,开头public class 文件名 ...
- 201521123071 《JAVA程序设计》第九周学习总结
第九周-异常 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 此处仅部分知识点归纳 2. 书面作业 1. 常用异常,题目5-1 1.1 截图你的提交结果(出现学号) ...