学习Netflix管理员–第1部分
最近幾天,我一直在與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部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通货膨胀是什么意思?
- 下一篇: 美元降息意味着什么?