netflix_学习Netflix管理员–第1部分

netflix

最近几天,我一直在与Netflix Governator合作,并尝试使用Governator尝试一个小样本,以将其与Spring Framework的依赖项注入功能集进行比较。 以下内容并不全面,我将在下一系列文章中对此进行扩展。

因此,对于没有经验的人来说,Governorator是对Google Guice的扩展,通过一些类似于Spring的功能对其进行了增强,引用Governator网站:

类路径扫描和自动绑定,生命周期管理,配置到字段映射,字段验证和并行化的对象预热。

在这里,我将演示两个功能,类路径扫描和自动绑定。

基本依赖注入

考虑一个BlogService,具体取决于BlogDao:

public class DefaultBlogService implements BlogService {private final BlogDao blogDao;public DefaultBlogService(BlogDao blogDao) {this.blogDao = blogDao;}@Overridepublic BlogEntry get(long id) {return this.blogDao.findById(id);}
}

如果我使用Spring定义这两个组件之间的依赖关系,则将使用以下配置:

package sample.spring;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sample.dao.BlogDao;
import sample.service.BlogService;@Configuration
public class SampleConfig {@Beanpublic BlogDao blogDao() {return new DefaultBlogDao();}@Beanpublic BlogService blogService() {return new DefaultBlogService(blogDao());}
}

在Spring中,依赖项配置在带有@Configuration注释的类中指定。 用@Bean注释的方法返回组件,请注意如何通过blogService方法中的构造函数注入来注入blogDao。

此配置的单元测试如下:

package sample.spring;import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import sample.service.BlogService;import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;public class SampleSpringExplicitTest {@Testpublic void testSpringInjection() {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(SampleConfig.class);context.refresh();BlogService blogService = context.getBean(BlogService.class);assertThat(blogService.get(1l), is(notNullValue()));context.close();}}

请注意,Spring为单元测试提供了良好的支持,更好的测试如下:

package sample.spring;package sample.spring;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sample.service.BlogService;import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SampleSpringAutowiredTest {@Autowiredprivate BlogService blogService;@Testpublic void testSpringInjection() {assertThat(blogService.get(1l), is(notNullValue()));}@Configuration@ComponentScan("sample.spring")public static class SpringConig {}}

这是基本的依赖项注入,因此不需要指定这种依赖关系,Governice本身就不需要管治者,这就是使用Guice Modules时配置的外观:

package sample.guice;import com.google.inject.AbstractModule;
import sample.dao.BlogDao;
import sample.service.BlogService;public class SampleModule extends AbstractModule{@Overrideprotected void configure() {bind(BlogDao.class).to(DefaultBlogDao.class);bind(BlogService.class).to(DefaultBlogService.class);}
}

此配置的单元测试如下:

package sample.guice;import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Test;
import sample.service.BlogService;import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;public class SampleModuleTest {@Testpublic void testExampleBeanInjection() {Injector injector = Guice.createInjector(new SampleModule());BlogService blogService = injector.getInstance(BlogService.class);assertThat(blogService.get(1l), is(notNullValue()));}}

类路径扫描和自动绑定

类路径扫描是一种通过在类路径中查找标记来检测组件的方法。 使用Spring的样本应阐明这一点:

@Repository
public class DefaultBlogDao implements BlogDao {....
}@Service
public class DefaultBlogService implements BlogService {private final BlogDao blogDao;@Autowiredpublic DefaultBlogService(BlogDao blogDao) {this.blogDao = blogDao;}...
}

在这里,注释@ Service,@ Repository用作标记,以指示它们是组件,并且依赖项由DefaultBlogService的构造函数上的@Autowired注释指定。

鉴于现在已经简化了配置,我们只需要提供应该为此类带注释的组件进行扫描的软件包名称,这就是完整测试的样子:

package sample.spring;
...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SampleSpringAutowiredTest {@Autowiredprivate BlogService blogService;@Testpublic void testSpringInjection() {assertThat(blogService.get(1l), is(notNullValue()));}@Configuration@ComponentScan("sample.spring")public static class SpringConig {}
}

总督提供了类似的支持:

@AutoBindSingleton(baseClass = BlogDao.class)
public class DefaultBlogDao implements BlogDao {....
}@AutoBindSingleton(baseClass = BlogService.class)
public class DefaultBlogService implements BlogService {private final BlogDao blogDao;@Injectpublic DefaultBlogService(BlogDao blogDao) {this.blogDao = blogDao;}....
}

在这里,@ AutoBindSingleton注释被用作标记注释,以定义guice绑定,考虑到以下是对类路径扫描的测试:

package sample.gov;import com.google.inject.Injector;
import com.netflix.governator.guice.LifecycleInjector;
import com.netflix.governator.lifecycle.LifecycleManager;
import org.junit.Test;
import sample.service.BlogService;import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;public class SampleWithGovernatorTest {@Testpublic void testExampleBeanInjection() throws Exception {Injector injector  = LifecycleInjector.builder().withModuleClass(SampleModule.class).usingBasePackages("sample.gov").build().createInjector();LifecycleManager manager = injector.getInstance(LifecycleManager.class);manager.start();BlogService blogService = injector.getInstance(BlogService.class);assertThat(blogService.get(1l), is(notNullValue()));}}

查看如何使用Governator的LifecycleInjector组件指定要扫描的程序包,这将自动检测这些组件并将它们连接在一起。

只是为了包装类路径扫描和自动绑定功能,像Spring这样的Governor提供了对junit测试的支持,更好的测试如下:

package sample.gov;import com.google.inject.Injector;
import com.netflix.governator.guice.LifecycleTester;
import org.junit.Rule;
import org.junit.Test;
import sample.service.BlogService;import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;public class SampleWithGovernatorJunitSupportTest {@Rulepublic LifecycleTester tester = new LifecycleTester();@Testpublic void testExampleBeanInjection() throws Exception {tester.start();Injector injector = tester.builder().usingBasePackages("sample.gov").build().createInjector();BlogService blogService = injector.getInstance(BlogService.class);assertThat(blogService.get(1l), is(notNullValue()));}}

结论

如果您有兴趣进一步探索这个问题,那么我在这个github项目中有一个示例,随着我对Governator的更多了解,我将扩展这个项目。

翻译自: https://www.javacodegeeks.com/2015/01/learning-netflix-governator-part-1.html

netflix

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/338457.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

laravel ajax ip,怎么在Laravel中利用AJAX动态刷新部分页面

怎么在Laravel中利用AJAX动态刷新部分页面发布时间:2021-02-17 13:12:43来源:亿速云阅读:119作者:Leah怎么在Laravel中利用AJAX动态刷新部分页面?很多新手对此不是很清楚,为了帮助大家解决这个难题&#xf…

vue2 怎么用vite_Vue2和Vue3开发组件有什么区别

我们一直都有关注和阅读很多关于Vue3的新特性和功能即将到来。但是我们没有一个具体的概念在开发中会有如何的改变和不一样的体验。还有一些童鞋已经开始又慌又抓狂了 -- “又要开始学新的写法了 (ノToT )ノ ~┻┻”。所以这里我使用Vue2和Vue3开发一个简单的表格组件来展示一下…

C语言中实现边沿函数算法及应用,这是抛弃PLC留下的痛!

很多从事PLC编程的朋友都知道,不管是什么品牌的PLC,都有上升沿和下降沿指令。❤那么什么情况下我们才会使用或必须使用边沿信号呢?边沿信号我们又如何获取呢?如图1,任何一个开关信号(或数字信号&#xff09…

jboss将war放在那?_将策略插入JBoss Apiman

jboss将war放在那?JBoss apiman项目 本周刚刚发布了1.0.3.Final 。 它主要是一个错误修复版本,仅进行了一些相对较小的改进。 自从我上次写博客以来,其中的一项特殊功能就是对插件的支持。 这些插件可以轻松添加到系统中,以提供其他功能。 …

服务器系统装驱动精灵,云服务器安装驱动精灵

云服务器安装驱动精灵 内容精选换一换您可以选择在云服务器上安装一个或多个应用。如需在云服务器上安装其他应用,请参考如下操作进行添加。暂时仅允许支持VR应用的云服务器安装VR应用。暂时仅允许支持3D应用的云服务器安装3D应用。暂时仅允许支持VR应用的云服务器有…

注入器 过检测_连云港管道检测服务

连云港管道检测服务 管道稀释淤泥施工时应采用专业高压水车将两个检查井注入室内灌水,并使用挖泥机将检查井中的污泥与排污管混合,以稀释污泥为目的. 如果是手工作业应与机械作业配合以不断搅拌污泥,直到将其稀释到水中为止.管道吸污。 公司备…

lock.lock_HibernateCascadeType.LOCK陷阱

lock.lock介绍 引入了Hibernate 显式锁定支持以及Cascade Types之后 ,就该分析CascadeType.LOCK行为了。 Hibernate锁定请求触发内部LockEvent 。 关联的DefaultLockEventListener可以将锁定请求级联到锁定实体子级。 由于CascadeType.ALL也包括CascadeType.LOCK …

浅谈面向对象思想下的 C 语言

如何使用OO思维方式面向对象(object Oriented,简称:OO)在于用“找对象”的方式去规划和描述问题。一、怎样“找对象” (思维过程)“对象”是具有共性的一个群体。以 L298N 控制马达的官方推荐方法为例,控制的共性在于&…

我的世界服务器里怎么无限随机传送,我的世界随机传送插件使用教程 权限指令分享...

导读:在我的世界中玩家可以利用随机传送插件来进行传送人物质与设定点,那么随意传送插件该如何使用呢、下面小编我就来教教各位,我的世界随意传送插件使用教程。什么是RandomLocationRandomLocation让你传送到预设区域的随机位置。可以通过命…

热敏电阻温度特性曲线_NTC热敏电阻如何选型

什么是NTCNTC 热敏电阻是负温度系数的电阻,其特性是电阻值随着温度的升高而呈下降趋势。这个与PTC或者PT100等正温度系数的热敏电阻相反。NTC 热敏电阻NTC的阻值-温度对应曲线如下图所示(100K为例,B值3950)。NTC 热敏电阻R-T曲线下面介绍选型原则。2.根据…

如何使用C语言的面向对象?

我们都知道,C 才是面向对象的语言,但是C语言是否能使用面向对象的功能?(1)继承性typedef struct _parent{int data_parent;}Parent;typedef struct _Child{struct _parent parent;int data_child;}Child;在设计C语言继承性的时候,我们需要做…

netflix_学习Netflix管理员–第2部分

netflix为了继续上一篇有关Netflix Governator的一些基础知识的文章,在这里,我将介绍Netflix Governator带给Google Guice的另一项增强功能–生命周期管理 生命周期管理本质上提供了进入对象所经历的不同生命周期阶段的钩子,以引用有关Gover…

t3软件怎么生成报表_临沂用友畅捷通T3财务通软件财税一体化

用友T3财税通针对财税一体化的发展趋势,在用友通上海财税专版的基础上,经过完善和提高。同时加入了税务核算功能、所得税汇算功能。 财税通财务软件的财税同步处理,可将事后税务处理的汇总涉税数据工作化整为零,分解到日常凭证填制…

每日干货丨C语言知识总结----循环结构

介绍循环结构可以看成是一个条件判断语句和一个向回 转向语句 的组合。另外,循环结构的三个要素:循环变量、 循环体 和循环终止条件. ,循环结构在 程序框图 中是利用判断框来表示,判断框内写上条件,两个出口分别对应着…

apache hadoop_使用Apache Hadoop计算PageRanks

apache hadoop目前,我正在接受Coursera的培训“ 挖掘海量数据集 ”。 我对MapReduce和Apache Hadoop感兴趣已有一段时间了,通过本课程,我希望对何时以及如何MapReduce可以帮助解决一些现实世界中的业务问题有更多的了解(我在这里介…

730阵列卡支持多大硬盘_凯捷月销破2万,配6座头等舱空间,到底有多舒服?试驾了才知道...

能够在还未上市的前一个月,就以预售的方式卖出超过2万台,上汽通用五菱在乘用车市场之中的号召力可见一斑。过去我们都将五菱视为商务领域的铭牌,包括宏光、荣光、之光等等家族,都在各自细分市场占据着最顶端位置。如今&#xff0c…

C语言循环嵌套

在C语言中,if-else、while、do-while、for 都可以相互嵌套。所谓嵌套(Nest),就是一条语句里面还有另一条语句,例如 for 里面还有 for,while 里面还有 while,或者 for 里面有 while,w…

rem 前端字体_web前端入门到实战:一次搞懂CSS字体单位:px、em、rem和%

对于绘图和印刷而言,“单位”相当重要,然而在网页排版里,单位也是同样具有重要性,在CSS3普及以来,更支持了一些方便好用的单位(px、em、rem…等),这篇文章将整理这些常用的CSS单位&a…

jvmti_JVMTI标记如何影响GC暂停

jvmti这篇文章分析了为什么Plumbr Agents在某些情况下以及如何延长GC暂停的时间。 对基本问题进行故障诊断揭示了有关在GC暂停期间如何处理JVMTI标记的有趣见解。 发现问题 我们的一位客户抱怨说,附加了Plumbr代理后,应用程序的响应速度明显降低。 通过…

C语言-使用goto语句从循环中跳出

实例代码// //实现功能:使用goto语句从循环中跳出 //#include "stdio.h"#define EXIT 0void show_Menu(){printf("菜单选项:\t");printf("1:显示\t");printf("2:添加\t");printf("3&#xff1a…