服务企业网站建设的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