学习Netflix管理员–第1部分
最近幾天,我一直在與Netflix Governator合作,并嘗試使用Governator嘗試一個(gè)小樣本,以將其與Spring Framework的依賴項(xiàng)注入功能集進(jìn)行比較。 以下內(nèi)容并不全面,我將在下一系列文章中對(duì)此進(jìn)行擴(kuò)展。
因此,對(duì)于沒有經(jīng)驗(yàn)的人來說,Governorator是Google Guice的擴(kuò)展,通過一些類似于Spring的功能對(duì)其進(jìn)行了增強(qiáng),引用Governator網(wǎng)站:
類路徑掃描和自動(dòng)綁定,生命周期管理,配置到字段映射,字段驗(yàn)證和并行化的對(duì)象預(yù)熱。
在這里,我將演示兩個(gè)功能,類路徑掃描和自動(dòng)綁定。
基本依賴注入
考慮一個(gè)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定義這兩個(gè)組件之間的依賴關(guān)系,則將使用以下配置:
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中,依賴項(xiàng)配置是在帶有@Configuration注釋的類中指定的。 @Bean注釋的方法返回組件,請(qǐng)注意如何通過blogService方法中的構(gòu)造函數(shù)注入來注入blogDao。
此配置的單元測(cè)試如下:
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();}}請(qǐng)注意,Spring為單元測(cè)試提供了良好的支持,更好的測(cè)試如下:
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 {}}這是基本的依賴項(xiàng)注入,因此不需要指定這種依賴關(guān)系,Governator本身就是必需的,Guice就足夠了,這就是使用Guice Modules時(shí)配置的外觀:
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);} }此配置的單元測(cè)試如下:
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()));}}類路徑掃描和自動(dòng)綁定
類路徑掃描是一種通過在類路徑中查找標(biāo)記來檢測(cè)組件的方法。 使用Spring的樣本應(yīng)該澄清這一點(diǎn):
@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用作標(biāo)記,以指示它們是組件,并且依賴項(xiàng)由DefaultBlogService的構(gòu)造函數(shù)上的@Autowired注釋指定。
鑒于現(xiàn)在已經(jīng)簡化了配置,我們只需要提供應(yīng)該為此類帶注釋的組件進(jìn)行掃描的軟件包名稱,這就是完整測(cè)試的樣子:
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批注用作標(biāo)記批注來定義guice綁定,考慮到以下是對(duì)類路徑掃描的測(cè)試:
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組件指定要掃描的軟件包,這將自動(dòng)檢測(cè)這些組件并將它們連接在一起。
只是為了包裝類路徑掃描和自動(dòng)綁定功能,像Spring這樣的Governor提供了對(duì)junit測(cè)試的支持,更好的測(cè)試如下:
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()));}}結(jié)論
如果您有興趣進(jìn)一步探索這個(gè)問題,那么我在這個(gè)github項(xiàng)目中有一個(gè)示例,隨著我對(duì)Governator的更多了解,我將擴(kuò)展這個(gè)項(xiàng)目。
翻譯自: https://www.javacodegeeks.com/2015/01/learning-netflix-governator-part-1.html
總結(jié)
以上是生活随笔為你收集整理的学习Netflix管理员–第1部分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通货膨胀是什么意思?
- 下一篇: 非捕获Lambda的实例