前言

我们先探一探官方文档关于Method Injection的章节是怎么说的:

In most application scenarios, most beans in the container are singletons. When a singleton bean needs to collaborate with another singleton bean or a non-singleton bean needs to collaborate with another non-singleton bean, you typically handle the dependency by defining one bean as a property of the other. A problem arises when the bean lifecycles are different. Suppose singleton bean A needs to use non-singleton (prototype) bean B, perhaps on each method invocation on A. The container creates the singleton bean A only once, and thus only gets one opportunity to set the properties. The container cannot provide bean A with a new instance of bean B every time one is needed.

用一句话概括就是 一个单例Bean A每次获取另外一个Bean B的时候怎么保证这个Bean B是一个新的实例?

正文

ApplicationContextAware接口

官方文档首先也提到了一个解决方案就是把A弄成容器的Aware( make bean A aware of the container),也就是实现ApplicationContextAware接口。

A solution is to forego some inversion of control. You can make bean A aware of the container by implementing the ApplicationContextAware interface, and by making a getBean("B") call to the container ask for (a typically new) bean B instance every time bean A needs it. The following example shows this approach

文档里随后就提供了一个示例来说明这个解决方案如何做。

// a class that uses a stateful Command-style class to perform some processing

package fiona.apple;

// Spring-API imports

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

public class CommandManager implements ApplicationContextAware {

private ApplicationContext applicationContext;

public Object process(Map commandState) {

// grab a new instance of the appropriate Command

Command command = createCommand();

// set the state on the (hopefully brand new) Command instance

command.setState(commandState);

return command.execute();

}

protected Command createCommand() {

// notice the Spring API dependency!

return this.applicationContext.getBean("command", Command.class);

}

public void setApplicationContext(

ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

}

虽然解决了一开始提出的问题,但是Spring随后就说到:

The preceding is not desirable, because the business code is aware of and coupled to the Spring Framework. Method Injection, a somewhat advanced feature of the Spring IoC container, lets you handle this use case cleanly.

也就是说 前面的方法是不可取的,因为业务代码知道并耦合到Spring框架。那怎么降低这个耦合度呢?后半句就给出了答案:方法注入是Spring IOC容器的一个稍微高级的特性,它允许您干净地处理这个用例。

Lookup Method方法注入

首先再次引入官方文档中的阐述:

Lookup method injection is the ability of the container to override methods on container-managed beans and return the lookup result for another named bean in the container. The lookup typically involves a prototype bean, as in the scenario described in the preceding section. The Spring Framework implements this method injection by using bytecode generation from the CGLIB library to dynamically generate a subclass that overrides the method.

简要概括下这段话有三个意思:

Lookup Method注入可以让容器重写容器中bean上的方法并返回容器中另一个bean的查找结果。

Lookup通常会是一个原型bean,如文章开头所说的。

Spring框架通过使用CGLIB库中的字节码生成来动态生成覆盖该方法的子类,从而实现这种方法注入。

使用Lookup方式需要注意以下几点:

For this dynamic subclassing to work, the class that the Spring bean container subclasses cannot be final, and the method to be overridden cannot be final, either.

Unit-testing a class that has an abstract method requires you to subclass the class yourself and to supply a stub implementation of the abstract method.

Concrete methods are also necessary for component scanning, which requires concrete classes to pick up.

A further key limitation is that lookup methods do not work with factory methods and in particular not with @Bean methods in configuration classes, since, in that case, the container is not in charge of creating the instance and therefore cannot create a runtime-generated subclass on the fly.

这段话也可以概括为以下几点:

使这个动态子类可以用,这个类不能是final,要重写的方法也不能是final。

单元测试具有抽象方法的类需要您自己对该类进行子类化,并提供抽象方法的存根实现。

具体的方法对于组件扫描也是必要的,这需要具体的类来获取。

一个关键限制是Lookup方法不适用于工厂方法,尤其是配置类中的@Bean方法,因为在这种情况下,容器不负责创建实例,因此不能动态创建运行时生成的子类。

接下来Spring就拿上面本来基于ApplicationContextAware的方法来说,就可以用Lookup来替换了。对于前面代码段中的CommandManager类,Spring容器动态重写createCommand()方法的实现。CommandManager类没有任何Spring依赖项,如修改后的示例所示

In the case of the CommandManager class in the previous code snippet, the Spring container dynamically overrides the implementation of the createCommand() method. The CommandManager class does not have any Spring dependencies, as the reworked example shows.

Xml配置lookup-method

public abstract class CommandManager {

public Object process(Object commandState) {

// grab a new instance of the appropriate Command interface

Command command = createCommand();

// set the state on the (hopefully brand new) Command instance

command.setState(commandState);

return command.execute();

}

// okay... but where is the implementation of this method?

protected abstract Command createCommand();

}

在包含要注入的方法的CommandManager类中,要注入的方法需要以下形式的签名:

[abstract] theMethodName(no-arguments);

如果方法是抽象的,则动态生成的子类将实现该方法。否则,动态生成的子类将重写在原始类中定义的具体方法。

我们来看下Bean的配置:

当需要myCommand这个Bean的新实例时,被标识为commandManager的bean就会调用自己的createCommand()方法。如果需要的话,必须小心地将myCommand 这个bean部署为原型。如果是单例,则每次都返回相同的myCommand bean实例.

@Lookup注解

在基于注解的组件模型中,可以通过@lookup注释声明查找方法,如下例所示

public abstract class CommandManager {

public Object process(Object commandState) {

Command command = createCommand();

command.setState(commandState);

return command.execute();

}

@Lookup("myCommand")

protected abstract Command createCommand();

}

或者,你也可以依靠目标bean根据lookup方法的声明返回类型进行解析

public abstract class CommandManager {

public Object process(Object commandState) {

MyCommand command = createCommand();

command.setState(commandState);

return command.execute();

}

@Lookup

protected abstract MyCommand createCommand();

}

需要注意的是你通常应该用一个具体的存根实现声明这种带注解的查找方法,以便它们与Spring的组件扫描规则兼容,默认情况下抽象类会被忽略。此限制不适用于显式注册或显式导入的bean类。也就是说如果用组件扫描Bean的话因为抽象类默认是被忽略的,但是你加上这个Lookup注解后就不会呗忽略。

Spring在最后也提供了其他两种解决思路:

Another way of accessing differently scoped target beans is an ObjectFactory/ Provider injection point. See Scoped Beans as Dependencies。You may also find the ServiceLocatorFactoryBean (in the org.springframework.beans.factory.config package) to be useful.

通过ObjectFactory或者ObjectProvider.

通过ServiceLocatorFactoryBean.

这两个方案我们之后会单独写文章来探讨,下篇文章我打算来具体的使用下这个Lookup 方法注入并且从源码角度来看下Spring如何巧妙地实现它的。

总结

到此这篇关于Spring中为何要引入Lookup注解的文章就介绍到这了,更多相关Spring为何引入Lookup内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

java为什么引入注解_说说Spring中为何要引入Lookup注解相关推荐

  1. @autowired注解_品Spring:对@Autowired和@Value注解的处理方法(文末附spring系列资源合集)...

    作者:编程新说李新杰 出自:微信公众号"编程新说" 原文:品Spring:对@Autowired和@Value注解的处理方法 在Spring中能够完成依赖注入的注解有JavaSE提 ...

  2. java redis 批量删除_在Spring中使用Redis Lua脚本批量删除缓存

    背景 之前分享了一篇利用lua脚本批量删除redis的key的文章.现在项目中我打算使用spring的缓存,而Spring缓存以前我是用ehcache来做实现的.没发现什么问题..这次我换成redis ...

  3. Spring中解决事务以及异步注解失效

    Spring中解决事务以及异步注解失效 一.重现@Transaction失效的场景 有如下业务场景,新增订单后,自动发送短信,下面的代码在同一个类中: @Transaction public void ...

  4. java.lang.IllegalArgumentException: 字符[_]在域名中永远无效。 at

    [http-nio-8080-exec-1] org.apache.coyote.AbstractProcessor.parseHost [xxx_tomcat] 是无效主机注意:更多的请求解析错误将 ...

  5. spring 加载java类_在Spring中基于Java类进行配置的完整步骤

    在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...

  6. java悲观者不加事务_在spring中,使用事务和不用事务的区别

    各位高手大家好,我最近在学习Spring,自己也作了几个例子.但我在开发的过程中发现我使用事务和不使用事务效果都一样,我想问一下两者的区别,谢谢大家:第一种,不使用事务,Spring... 各位高手大 ...

  7. Spring 中 @Component、@Service 等注解如何被解析?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | http://8rr.co/EjqL 前言 @ ...

  8. @PostConstruct、@PreDestroy注解介绍及Spring中@PostConstruct、constructor、@Autowired的顺序

    @PostConstruct和@PreDestroy @PostConstruct和@PreDestroy注解是Common Annotations中的注解,Common Annotations原本是 ...

  9. java的abstract的意思_java – spring中的abstract =“true”是什么意思?

    Spring中的抽象bean有点不同于抽象类.事实上,Spring中的抽象bean甚至不需要映射到任何类.以此为例: 和类: public class FooDao { private DataSou ...

最新文章

  1. C++添加程序到windows的启动项的代码
  2. Tomcat 处理 HTTP 请求源码分析(下)【转】
  3. 洛谷P1155 双栈排序
  4. 数据库的四个范式之间的区别
  5. Caffe 学习系列
  6. 郁闷的开始--8月21日
  7. 2021-09-02spark streaming
  8. java爬虫技术之Selenium爬虫
  9. 排序(二分插入排序)
  10. 目标检测综述学习笔记
  11. linux同花顺乱码,打开同花顺软件全是问号
  12. 5、优化方法:随机梯度下降法
  13. html改变按钮样式
  14. 基于控制的角度无人机集群——避障(有源代码)
  15. 【超详细】初探FME--获取等时圈
  16. 卢周来:穿越政治经济学年代
  17. GEE学习笔记-基本数据类型
  18. 2017物联网安全事件盘点
  19. 1983年美国制定的c语言标准,C语言的发展历史
  20. [Android 硬件] Eclipse错误:Conversion to Dalvik format failed with error 1

热门文章

  1. 小程序生成长图(预览,不限高度)+小程序码+小程序码中间logo换成自定义logo
  2. 腾讯地图+element-ui 实现地址搜索标记功能
  3. QT自定义控件--键盘输入框
  4. 用了10年开源工具,换了Smartbi后,3分钟搞定一份报表
  5. html中js隐藏div的高度,js如何获取div(ul li)元素的宽度和高度(包括width/height和CSS的宽度和高度)...
  6. 将一句话的单词进行倒置,标点符号不倒置
  7. python2安装pyinstaller失败
  8. 转行历程|用一年时间写一份简历,成功跳槽
  9. python代码调试中间变量_Python代码调试的那些“最少且必要”技巧
  10. vs编辑器文件另存为后无法调试