spring 托管bean

即使我们想使用现有的最佳和最新技术,我们也必须处理遗留代码。 想象一下,新代码是用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

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

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

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

  2. spring配置xml文件_XML配置文件中的Spring配置文件

    spring配置xml文件 我的上一个博客非常简单,因为它涵盖了我从Spring 3.0.x到Spring 3.1.x的轻松升级,最后我提到可以将Spring模式升级到3.1,以利用Spring的最新 ...

  3. spring初始化在ServletContextListener实现类中获取spring注入对象

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧! 由于项目须要,需在ServletContextListener监听接口实现类中调用sprin ...

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

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

  5. Spring Cloud Gateway 2.1.0 中文官网文档

    目录 1. How to Include Spring Cloud Gateway 2. Glossary 3. How It Works 4. Route Predicate Factories 5 ...

  6. Spring boot(6)---在Eclipse中搭建Spring boot 项目

    Spring boot入门:在Eclipse中搭建Spring boot 项目 Eclipse中的STS插件 打开Eclipse-Help-Eclipse Marketplace-popular 下载 ...

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

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

  8. .NET中的托管资源与非托管资源

    托管资源指的是.NET可以自动进行回收的资源,主要是指托管堆上分配的内存资源.托管资源的回收工作是不需要人工干预的,有.NET运行库在合适调用垃圾回收器进行回收. 非托管资源指的是.NET不知道如何回 ...

  9. 编写高质量代码改善C#程序的157个建议——建议50:在Dispose模式中应区别对待托管资源和非托管资源...

    建议50:在Dispose模式中应区别对待托管资源和非托管资源 真正资源释放代码的那个虚方法是带一个bool参数的,带这个参数,是因为我们在资源释放时要区别对待托管资源和非托管资源. 提供给调用者调用 ...

最新文章

  1. Java代码缺陷自动分析工具介绍
  2. 树节点的遍历,查找,删除(前序,中序,后序)
  3. struts2校验再提交多条提示信息
  4. .NET6之MiniAPI(七):中间件
  5. 问题 B: 十进制到二进制的转换
  6. 在python中、正确的函数定义格式为_Python函数的定义与实现
  7. 编辑文件 vi,vim的基本操作
  8. c#语言程序设计上机实验,《C#语言程序设计》实 验 报 告
  9. springboot图片上传和显示_Jeewx-Boot 1.1 版本发布,基于SpringBoot的开源微信管家系统...
  10. 必做作业3:原型化系统---乘车app
  11. echar3D地图+3D柱形图
  12. 三原色是红黄蓝对吗_为什么三原色是红黄蓝而?
  13. (最小割求最小割集)poweroj2883病毒侵染
  14. 微信小游戏上传设置成体验版或者提交审核
  15. 域名解析中TTL是什么意思
  16. Dynamics 365 SiteMap Designer
  17. Android中级控件介绍(五)
  18. 汇聚数据库创新力量,加速企业数字化转型
  19. 小米笔记本android,7代小米笔记本安装凤凰系统(Phoenix OS)显示命令行ANDROID字样
  20. Jumping Frog

热门文章

  1. Reids实战(7)数据类型五sorted sets
  2. VS2015配置QT5.X环境
  3. SharePoint数据表组件错误
  4. 自定义的GridView控件源代码
  5. 为什么ConcurrentHashMap是弱一致的
  6. linux shell curl 超时与重试
  7. java maven centos7 yum安装
  8. linux tcpdump monitor模式 抓不到包 解决办法
  9. linux 服务管理两种方式service和systemctl
  10. golang 结构体简介