最近几天,我一直在与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 {}}

这是基本的依赖项注入,因此不需要指定这种依赖关系,Governator本身就是必需的,Guice就足够了,这就是使用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管理员–第1部分相关推荐

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

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

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

    netflix 最近几天,我一直在与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. PCB 3D模型与渲染
  2. JavaScript——基本的瀑布流布局及ajax动态新增数据
  3. Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Python
  4. [Tyvj 1729] 文艺平衡树
  5. C++检测步骤与示例
  6. Android下ListView的分页(9.6)
  7. ICCV 2019 Best Paper :SinGAN 解读,强烈推荐!
  8. ios中UIView和CALayer关系
  9. Qt视频直播软件--项目实战(Day3)
  10. linux环境下 Redis 配置文件
  11. PeakDo毫米波无线投屏器,你的无线图传神器
  12. 岩板铺地好吗_铺地的石板如何用处高逼格,三个大师案例来教你!
  13. 用c 语言乘法运算结果,c中的基本运算
  14. Git通过SSH拉取报错kex_exchange_identification
  15. angularjs 动态监控数据
  16. 输入若干个国家的英文名字,将它们按字母顺序从大到小排列,然后输出。
  17. C++核心准则C.150:unique_ptr管理的对象要用make_unique()​构建
  18. Android OAID 深度探究
  19. 如何破解周围WiFi,查看密码
  20. 鼠标双击事件 MINIUI

热门文章

  1. SpringSecurity认证
  2. 2020蓝桥杯省赛---java---B---8(数字三角形)
  3. linux下kill指令默认信号,Linux基础命令之kill命令
  4. python基础教程zip密码_python基础教程Python实现加密的RAR文件解压的方法(密码已知)...
  5. java作为kafka生产者实验及Expiring超时问题解决
  6. spring缓存_有关Spring缓存性能的更多信息
  7. heroku_将应用程序集成为Heroku附加组件
  8. java中无限大_Java 9中的无限集
  9. java方法示例注释 @_Java 8中的功能接口是什么? @功能注释和示例
  10. kafka streams_Kafka REST Proxy for MapR Streams入门