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

网站建设架南京app制作开发公司

网站建设架,南京app制作开发公司,工程公司绩效考核,深圳营销型网站开发最近几天#xff0c;我一直在与Netflix Governator合作#xff0c;并尝试使用Governator尝试一个小样本#xff0c;以将其与Spring Framework的依赖项注入功能集进行比较。 以下内容并不全面#xff0c;我将在下一系列文章中对此进行扩展。 因此#xff0c;对于没有经验的… 最近几天我一直在与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()));}ConfigurationComponentScan(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()));}ConfigurationComponentScan(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
http://www.pierceye.com/news/696535/

相关文章:

  • 给人做传销网站开发平台软件要多少钱
  • 腾讯建设网站视频视频视频手机网站报价表
  • 门户网站建设要求南京seo公司教程
  • 网站建设制作要学什么软件福建省城乡建设厅网站
  • 网站遇到攻击时应该怎么做网络广告推广平台
  • 老牌网站建深圳市建设工程服务交易中心
  • 网页界面设计布局青岛百度关键词优化
  • 彩票网站APP建设seo为什么不景气了
  • ps做网站标签wordpress 角色和权限管理
  • 网站建设情况报告范文微网站可以做成域名访问
  • 增城网站公司电话西安高端网站建设首选
  • 信息流广告素材网站零基础学做网站
  • 北京服饰网站建设实训百度搜索引擎的总结
  • 营销型网站建设的一般过程包括哪些环节?体育视频网站建设
  • 门户网网站seo怎么做电子商务平台怎么注册
  • 凡科网站插件代码阿里云网站备案后
  • 网站用什么系统好用免费网站建设找哪家
  • 网站到期续费吗网站开发是培训
  • 别人帮做的网站怎么修改怎么做产品推广和宣传
  • 国内返利网站怎么做php建设网站工具
  • 网站设计教程文档创业商机网农村
  • 宁夏交通建设质监局官方网站免费注册二级域名的网站
  • 网站门户设计网站建设有没有做的必要
  • 建模师的就业前景整站优化工具
  • 微信公众号怎么做链接网站网站404 原因
  • 安卓手机做服务器网站网站设计时多页面切换时什么控件
  • 长沙正规网站建设价格网站推广怎么发外链
  • 专业版装修用什么网站做导航条深圳网站制作易捷网络
  • 哪个公司建设网站好手机网站维护费
  • 中山高端网站建设wordpress调用分类文章列表