即使我们想使用现有的最佳和最新技术,我们也必须处理遗留代码。 想象一下,新代码是用Spring框架的最新技术编写的,而旧代码根本不是用Spring编写的。 然后在非托管Spring对象中使用Spring托管Bean是我们必须处理的模式之一。 遗留代码具有非托管的Spring对象,而我们要引用的代码是Spring托管的Bean。 我们如何解决这个问题?

创建一个Spring Bean

假设我们有一个名为TaxService的托管Spring Bean和一个名为LegacyObject的对象。 LegacyObject是遗留代码,从中可以引用托管Spring Bean上的calculateTax方法。

税务服务

package com.jdriven;import org.springframework.stereotype.Service;@Service
public class TaxServiceImplimplements TaxService {@Overridepublic Double calculateTax(Double price) {return new Double(price * 0.21);}
}

与桥接服务方法的接口

我们定义一个包含方法列表的接口。 这些方法中的每一个都返回一个Spring托管Bean。 我们创建了一个名为getTaxService的方法来返回刚刚创建的TaxService Bean。

SpringContextBridgedServices

package com.jdriven;/*** This interface represents a list of Spring Beans (services) which need to be referenced from a non Spring class.*/
public interface SpringContextBridgedServices {TaxService getTaxService();
}

实施Spring Context Bridge

接下来,我们为SpringContextBridgedServices接口创建一个实现。 让我们将此类SpringContextBridge ,使其成为Spring Bean,并在该类中添加以下功能。

  1. 此类还应实现Spring的ApplicationContextAware接口。 我们需要从接口实现的方法中唯一的参数是参数ApplicationContext 。 我们将此参数保存在静态成员变量中。
  2. 创建一个静态方法以返回SpringContextBridgedServices然后让该方法返回由Spring管理的Bean。 使用applicationContext.getBean(SpringContextBridgedServices.class)返回它。
  3. 自动连接TaxService并将其返回到我们需要从SpringContextBridgedServices方法实现的方法中。

SpringContextBridge

package com.jdriven;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/**
* Register this SpringContextBridge as a Spring Component.
*/
@Component
public class SpringContextBridge implements SpringContextBridgedServices, ApplicationContextAware {private static ApplicationContext applicationContext;@Autowiredprivate TaxService taxService; //Autowire the TaxService@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}/*** A static method to lookup the SpringContextBridgedServices Bean in * the applicationContext. It is basically an instance of itself, which * was registered by the @Component annotation.** @return the SpringContextBridgedServices, which exposes all the * Spring services that are bridged from the Spring context.*/public static SpringContextBridgedServices services() {return applicationContext.getBean(SpringContextBridgedServices.class);}@Overridepublic TaxService getTaxService() {return taxService; //Return the Autowired taxService}
}
  • 注意1:有可能以静态方法本身返回Spring托管的bean。 我选择不这样做,因此我的静态方法较少,以后可以模拟一些参考服务。
  • 注2:最终,您希望将这两种功能分开。 一个持有ApplicationContext并返回SpringContextBridgedServices Bean。 另一个是SpringContextBridgedServices Bean本身。 在这个简短的演示中,我只是将它们放在同一个Bean中。

带我去桥

现在是时候打电话给这座桥了。 就像下面的代码所示的那样简单。

传统对象

package com.jdriven;public class LegacyObject {private Double price;public Double doTheCalculation() {//Get the Service from the BridgeTaxService taxService = SpringContextBridge.services().getTaxService();return taxService.calculateTax(this.price);}
}

灵活但不受限制的替代方案

这是限制桥接服务列表的一种方式。 仅SpringContextBridgedServices接口中提到的服务将被桥接。 如果您想要一种更灵活但受控制较少的方法,则可以重写SpringContextBridgedServices

SpringContextBridgedServicesAlternative

package com.jdriven;public interface SpringContextBridgedServicesAlternative {<T> T getService(Class<T> serviceType);
}

现在我们可以通过调用SpringContextBridge.services().getService(TaxService.class)获得服务。 在这种替代方案中,我们无法控制可以桥接哪个Spring托管Bean。

翻译自: https://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html

在非托管对象中使用Spring托管Bean相关推荐

  1. spring 托管bean_在非托管对象中使用Spring托管Bean

    spring 托管bean 即使我们想使用现有的最佳和最新技术,我们也必须处理遗留代码. 想象一下,新代码是用Spring框架的最新技术编写的,而旧代码根本不是用Spring编写的. 然后,在非托管S ...

  2. Java(多)线程中注入Spring的Bean

    问题说明 : 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被 ...

  3. 【spring】在servlet中注入spring的bean,servlet容器和spring容器

    一.Servlet容器 Servlet的整个生命周期好象都是由Servlet容器来处理的. 如果把它硬放到Spring容器中去创建,Servlet对象是可被Spring容器建出来,但Servlet容器 ...

  4. 在多线程中使用spring的bean

    由于spring在java开发中的广泛运用大大的方便了开发的同时,当运用一些技术比如多线程等 在由spring管理的配置文件中,可以通过封装spring提供工具,手动获得spring管理的bean,这 ...

  5. oracle 调用main方法,main方法中调用spring注入bean

    public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext conte ...

  6. 将Spring Bean注入非托管对象

    依赖注入带来的好处可能会上瘾. 使用注入配置应用程序结构比手动完成所有解析要容易得多. 当我们有一些在容器外部实例化的非托管类时,例如在Vaadin UI组件或JPA实体等其他框架中,它们是很难被退出 ...

  7. spring@Autowired的对象为null,非容器中的类如何调用容器中的类

    1.问题描述 我们平时使用@Autowired注入对象时,一般被注入的类都带有@Coponent.@Controller.@Service .@repository等注解才可以.注入类和被注入类都被s ...

  8. JPA EntityListeners中的Spring注入的Bean

    在使用JPA侦听器进行数据库加密中,我讨论了使用JPA EntityListener进行透明加密. 从某种意义上说,这种方法是透明的,因为JPA实体(几乎)完全不知道正在加密,而JPA EntityL ...

  9. 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...

最新文章

  1. 【爬坑】远程连接 MySQL 失败
  2. python request-python-request-各方法使用及格式
  3. 孔板流量计计算公式_带你全面了解各种流量计
  4. 汇编解析(1)-内存寻址之实模型平面模式(real mode flat mode)(1)
  5. Stock Arbitraging
  6. 使用 010 Editor 分析二进制文件格式
  7. java postdata_java发送post请求,使用multipart form-data的方式传递参数,可实现服务器间文件上传功能...
  8. 结果显示窗口如何缩小_【操作教程】零基础如何学习PS与Sai?
  9. 跨服务器 快速 导入数据表记录 Insert into SELECT
  10. Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析
  11. 蛋糕是叫胚子还是坯子_最好吃的蛋糕胚子——分蛋海绵蛋糕详解
  12. 网上下载的php源码怎么运行,在中国站长站下载的asp源码,如何在自己电脑上运行?...
  13. Qt 语言切换 QTranslator cmake qmake
  14. python数据科学导论_R与Python手牵手:数据科学导论系列(包的载入)
  15. 【安全脚本】 centos 下的病毒木马查杀脚本
  16. (三-一)IPC-邮箱通信
  17. 【存储过程造数mysql】
  18. C#合并单元格,AddMergedRegion
  19. ipad分屏功能怎么开启_英雄联盟手游设置怎么调最合适?英雄联盟手游设置方法与新手开启功能解析...
  20. 长沙距离中国的“凤凰城”还有多远?

热门文章

  1. python 高维数据_Python数据分析入门|利用NumPy高效处理高维数据
  2. eclipse maven 项目发布到tomcat 报错 Failed to scan JAR [file:/C:/xxxxx.jar] from WEB-INF/lib
  3. final 实例域+final类+final方法(阻止继承)
  4. java中的lombok_如何在Java中使用Lombok删除样板设置器吸气剂
  5. JMetro版本5.2已发布
  6. idea资源包下创建资源包_根据谁创建资源授权资源
  7. 本地缓存防止缓存击穿_防止缓存爆炸的快速提示
  8. java方法重载和重载方法_Java 8的方法参考进一步限制了重载
  9. git hok json_从战中反弹:将Git提交信息作为JSON返回
  10. 内核中断处理流程_处理中断