当前位置: 首页 > news >正文

镇江方圆建设监理咨询有限公司网站网站建设及维护推广合同

镇江方圆建设监理咨询有限公司网站,网站建设及维护推广合同,网站制作属于什么品牌,广西红豆社区梧州论坛即使我们想使用现有的最佳和最新技术#xff0c;我们也必须处理遗留代码。 想象一下#xff0c;新代码是用Spring框架的最新技术编写的#xff0c;而旧代码根本不是用Spring编写的。 然后在非托管Spring对象中使用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并在该类中添加以下功能。 此类还应实现Spring的ApplicationContextAware接口。 我们需要从接口实现的方法中唯一的参数是参数ApplicationContext 。 我们将此参数保存在静态成员变量中。 创建一个静态方法以返回SpringContextBridgedServices然后让该方法返回由Spring管理的Bean。 使用applicationContext.getBean(SpringContextBridgedServices.class)返回它。 自动连接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 TaxServiceOverridepublic 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(ClassT serviceType); } 现在我们可以通过调用SpringContextBridge.services().getService(TaxService.class)获得服务。 在这种替代方案中我们无法控制可以桥接哪个Spring托管Bean。 翻译自: https://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html
http://www.pierceye.com/news/865209/

相关文章:

  • 关于网站开发人员的薪资易语言怎么做无限打开网站
  • 网站备案名称几个字企业网站定制案例
  • 新浪云服务器做网站重庆建设厅官方网站
  • 苏州市住房和城乡建设局官方网站郑州专业旅游网站建设
  • 网站免费正能量直接进入浏览器下载安装公开课网站建设
  • 个人做电影网站合法吗网页制作与网站建设完全学习手册下载
  • 椒江做网站wordpress的分类
  • 新手做网站应该注意什么重庆市建设工程造价信息网公众号
  • 网址输入奉化seo页面优化外包
  • 坪山商城网站建设哪家效益快教务管理系统是应用软件吗
  • 深圳网站搭建找谁怎么在手机上制作app
  • 做app和做网站的区别桂林市天气预报15天
  • 高端织梦html5网站模板 dedecms网络公司模板关键词排名优化方法
  • 上海网站建设咨找个网站2021能看到
  • 可以用服务器做网站查询公司信息
  • 个人可以备案企业网站吗旅行社网站 模板
  • 三丰云做网站步骤网站怎么上传ftp
  • 做二手车有哪些网站有哪些手续网站建设单位有哪些方面
  • 建设网站的和服务器常州免费网站制作
  • 电子外贸网站重庆有什么好玩的
  • 商务网站的建设阶段包括郑州的做网站公司哪家好
  • 深圳网站建设网络公司七星彩网投网站建设
  • 手机建网站公司wordpress增加关键字
  • 招聘网站有哪些平台电商网站建设方式
  • 网站的备案要求域名icp备案查询
  • 网站的投资和建设项目站群服务器
  • 建网站电脑版和手机版怎么做网页实训总结及心得体会
  • 网站建设咨询费用一流的网站建设流程图
  • 如何进行营销型企业网站的优化如何在百度上推广自己
  • 360怎么变成建设银行首选网站网站建设财务分析