日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

netflix_学习Netflix管理员–第2部分

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 netflix_学习Netflix管理员–第2部分 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

netflix

為了繼續上一篇有關Netflix Governator的一些基礎知識的文章,在這里,我將介紹Netflix Governator帶給Google Guice的另一項增強功能–生命周期管理

生命周期管理本質上提供了進入對象所經歷的不同生命周期階段的鉤子,以引用有關Governor的Wiki文章 :

Allocation (via Guice)|v Pre Configuration|v Configuration|V Set Resources|V Post Construction|V Validation and Warm Up|V-- application runs until termination, then... -- |V Pre Destroy

為了說明這一點,請考慮以下代碼:

package sample.gov;import com.google.inject.Inject; import com.netflix.governator.annotations.AutoBindSingleton; import sample.dao.BlogDao; import sample.model.BlogEntry; import sample.service.BlogService;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;@AutoBindSingleton(baseClass = BlogService.class) public class DefaultBlogService implements BlogService {private final BlogDao blogDao;@Injectpublic DefaultBlogService(BlogDao blogDao) {this.blogDao = blogDao;}@Overridepublic BlogEntry get(long id) {return this.blogDao.findById(id);}@PostConstructpublic void postConstruct() {System.out.println("Post-construct called!!");}@PreDestroypublic void preDestroy() {System.out.println("Pre-destroy called!!");} }

在這里,已經使用@PostConstruct和@PreDestroy批注對兩種方法進行了注釋,以掛鉤到此對象的管理者生命周期的這些特定階段。 整潔的是,這些批注不是特定于Governor的,而是現在烘焙到JDK中的JSR-250批注。

對該類調用測試將適當地調用帶注釋的方法,這是一個示例測試:

mport 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.*; import static org.hamcrest.Matchers.*;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()));manager.close();}}

長期以來,Spring Framework一直支持類似的機制 -因此,基于完全相同的基于JSR-250的注釋也適用于Spring bean。

如果您有興趣進一步探索它, 這是我的GitHub項目,其中包含帶有生命周期管理的示例。

翻譯自: https://www.javacodegeeks.com/2015/01/learning-netflix-governator-part-2.html

netflix

總結

以上是生活随笔為你收集整理的netflix_学习Netflix管理员–第2部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。