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

netflix_学习Netflix管理员–第1部分相关推荐

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

    netflix 为了继续上一篇有关Netflix Governator的一些基础知识的文章,在这里,我将介绍Netflix Governator带给Google Guice的另一项增强功能–生命周期管 ...

  2. 学习Netflix管理员–第1部分

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

  3. 学习Netflix管理员–第2部分

    为了继续上一篇有关Netflix Governator的一些基础知识的文章,在这里,我将介绍Netflix Governator带给Google Guice的另一项增强功能–生命周期管理 生命周期管理 ...

  4. hadoop学习-Netflix电影推荐系统

    1.推荐系统概述 电子商务网站是推荐系统应用的重要领域之一,当当网的图书推荐,大众点评的美食推荐,QQ好友推荐等等,推荐无处不在. 从企业角度,推荐系统的应用可以增加销售额等等,对于用户而言,系统仿佛 ...

  5. 我学习网络管理员的第一步---各个时期的基本要求

    一.在宿舍中的演练---5台左右计算机的网络环境 ●网络的基础知识:包括对网络认识.分类及拓扑结构的理解: ●网络的搭建:网线制作及网络设备的连接,如何制作网线,布线的注意事项及网络中设备的互联: ● ...

  6. 【计算机毕业设计】061微信互助学习平台

    一.系统截图(需要演示视频可以私聊) 摘要 随着信息技术在管理上越来越深入而广泛的应用,管理信息系统的实施在技术上已逐步成熟.本文介绍了微信互助学习平台的开发全过程.通过分析微信互助学习平台管理的不足 ...

  7. springclound学习

    一.springclound学习 Netflix公司的 解决的问题:服务比较多时,我们可以在注册中心通过服务名调用

  8. 学习小组实现过程、学习心得等

    作业文档 关于JAVA Stack的实现 https://gitee.com/huuujojo/homework 先是对stack的了解,明白栈是个什么东西 然后是查阅其他的集合类在jdk中是怎么实现 ...

  9. sql server死锁_如何解决SQL Server中的死锁

    sql server死锁 In this article, we will talk about the deadlocks in SQL Server, and then we will analy ...

最新文章

  1. 2021年金三银四春招实习回顾
  2. C语言怎么输出百分号%
  3. leetcode 909. 蛇梯棋
  4. 一篇文章学习Python中的多线程
  5. sklearn 3. 实例:随机森林在乳腺癌数据上的调参
  6. 李开复:一切靠命运或靠自己都是不合适的
  7. 更新Sogou代理服务器程序,支持HTTPS
  8. 微控制器STM32L412RBT6,STM32L412CBU6(128KB)MCU+FPU,规格
  9. 关于SQLServer关键词“union all”与“order by”的矛盾
  10. C++编写中的一些特殊符号
  11. 电话卡插到终端服务器通话时长,如何降低呼叫中心通话时长而不影响服务质量...
  12. 关于风向的u、v分量,及根据uv计算风向公式
  13. 自学方法测试【进行中】
  14. [论文笔记]Age of Information Aware Radio Resource Management in Veh Net: A Proactive DRL Perspective
  15. P1048 [NOIP2005 普及组] 采药
  16. 分布式架构之网络通信
  17. MDN的HTML入门,关于MDN,HTML入门来自MDN文档
  18. 2022年最新的Android面试大厂必考174题(附带详细答案)
  19. Mysql来帮忙:多行合并成一列
  20. IAP15F2K61S2芯片引脚图

热门文章

  1. P2597-[ZJOI2012]灾难【DAG支配树】
  2. nssl1157-简单数学题【约数,换元法】
  3. 纪中A组模拟赛总结(2021.7.22)
  4. Nacos(二)之概念
  5. Java虚拟机必学之四大知识要点,附学习资料
  6. 一个正则表达式酿成的惨案
  7. 你必须了解Spring的生态
  8. C++描述杭电OJ 2008.数值统计 ||
  9. 《此生未完成》痛句摘录(2)
  10. 检测性异常VS非检测性异常