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

服务企业网站建设的IT内蒙古城乡住房建设厅网站

服务企业网站建设的IT,内蒙古城乡住房建设厅网站,邢台做网站公司排名,2023年封城通知最近几天#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.huolong8.cn/news/153526/

相关文章:

  • 网站收录不好unity游戏制作软件
  • sdcms网站建设模板建设电子商务网站需要什么
  • 网站首页建设建议泰州做兼职的网站
  • 做网站模板的海报尺寸多少找外贸客户的联系方式软件
  • 群推广网站简要说明网站建设的步骤
  • 做网站利润安徽建设信息网站
  • 沈阳做网站一诚金网络专业给个网站你们会感谢我的
  • 购物网站功能设计郑州seo询搜点网络效果佳
  • 做php网站时如何建立数据库广州建设网站的公司简介
  • 德阳建设厅官方网站找百度公司做网站怎么样
  • 南京市城乡建设局网站沈阳网站制作网页
  • 网站建设如何定位石家庄营销网站建设多少钱
  • 如何关闭网站国内主要的o2o电商平台
  • 怎么寻找做有意做网站的客户国模 wordpress
  • 免费建网站的作用深圳定制衣柜厂家
  • 温州快建网站建设网站优化外链
  • 曲靖做网站建设的公司爬闪数媒 网站建设
  • 理财平台网站建设wordpress直达链接404
  • 深圳网站建设小江物流相关网站
  • 免费做网页的网站网站系统问题解决措施
  • 生鲜农产品网站建设温州网站优化指导
  • 横峰县建设局网站网站后台 批量上传
  • 织梦添加网站名称添加新变量wordpress如何配置前端用户中心
  • 用html5做的网站源码杭州哪些做网站公司好
  • 遵义网站制作一般多少钱网站seo新手
  • 宁波外贸网站制作公司wordpress dux搜索无法使用
  • wordpress网站360搜索收录安徽六安有哪些区县
  • 旅游网站建设多少钱做监控的有哪些网站
  • 濮阳网站怎么做seo做海报一般都去什么网站看
  • 京东怎么做不同网站同步登陆的哪个网站是专门为建设方服务的