第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Guice 框架編寫基于注解的方法攔截器時(shí)無(wú)法注入 java 對(duì)象

使用 Guice 框架編寫基于注解的方法攔截器時(shí)無(wú)法注入 java 對(duì)象

泛舟湖上清波郎朗 2021-12-30 17:13:31
我的應(yīng)用程序結(jié)構(gòu)就像我創(chuàng)建了一個(gè)注釋如下:-@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface SampleAnnotation {}然后創(chuàng)建了一個(gè)示例攔截器:public class SampleInterceptor implements MethodInterceptor {    private static final Logger logger = LoggerFactory.getLogger(SampleInterceptor.class);    @Inject    SampleService sampleService; // this is not working    public Object invoke(MethodInvocation invocation) throws Throwable {        logger.info("SampleInterceptor : Interceptor Invoked");        Object result = invocation.proceed();        Observable<List<Sample>> observable = (Observable<List<Sample>>) result;        SampleSender sender = null;        List<Sample> sampleList = observable.toBlocking().first();        for(Sample sample : sampleList ) {            sender = new SampleSender();            sender.setBoolean(sample.isBoolean());            logger.info("Pushing Data into Sender");            sampleService.insert(String.join("_", "key", "value"), sender); // here getting NullPointerException because sampleService is null        }        return result;    }}然后我創(chuàng)建了一個(gè) GuiceModule 如下:-public class SampleModule extends AbstractModule {    @Override    protected void configure() {        bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), new SampleInterceptor());}}我在其中使用上述注釋的類是// This class also have so many method and this was already declared and using in another services, I created a sample class hereclass SampleClassForInterceptor {      // this sampleMethod() is not a new method, its already created,       // now I am adding annotation to it, because after finishing this functionality,       // I want something should be done, so created annotation and added here      }}我的攔截功能開(kāi)始執(zhí)行之前執(zhí)行sampleMethod()的SampleClassForInterceptor類,然后執(zhí)行后sampleMethod()再次回來(lái)攔截,現(xiàn)在我在這里有一段代碼,將插入的結(jié)果(這是我們從得到sampleMethod())。這是我得到的地方NullPointerException,我檢查了代碼,發(fā)現(xiàn)該SampleService對(duì)象沒(méi)有被注入,它的值是null注意:我正在使用具有 RESTFUL 服務(wù)概念的微服務(wù)
查看完整描述

1 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

當(dāng)我requestInjection在SampleModule,然后SampleService被注入攔截器即我修改SampleModule代碼如下


public class SampleModule extends AbstractModule {

     @Override

     protected void configure() {

         SampleInterceptor interceptor = new SampleInterceptor();

         requestInjection(interceptor);

         bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), interceptor);

     }

}


查看完整回答
反對(duì) 回復(fù) 2021-12-30
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)