空指针异常-依赖注入-Google Guice

时间:2018-12-20 11:59:34

标签: java dependency-injection jax-ws guice

在将依赖项注入到我创建的拦截特定传入请求的Jersey读取器拦截器中时,我遇到NullPointerException。

这是我创建的ReaderInterceptor的代码,在这里我试图注入所需的依赖项。

public class RegisterServiceInterceptor implements ReaderInterceptor {

    @Inject
    private CustomerBackendManager customerBackendManager;

    public static final Logger LOGGER = LoggerFactory.getLogger(RegisterServiceInterceptor.class);

    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
        InputStream is = context.getInputStream();
        String body = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
        ObjectMapper mapper = new ObjectMapper();
        try {
            SampleObject sampleObject = mapper.readValue(body, SampleObject.class);
            customerBackendManager.createDatabase(sampleObject.getDatabaseName());
        } catch (JsonGenerationException | JsonMappingException e) {
            LOGGER.info(e.getMessage());
        }
        InputStream in = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
        context.setInputStream(in);
        return context.proceed();
    }

}

我在customerBackendManager.createDatabase(...);行得到NPE。没有将依赖项注入到此类中。

我已将此依赖项的配置定义为

public class ExampleAddonMicroserviceModule extends AbstractModule
        implements ConfigurationAwareModule<ExampleAddonMicroserviceConfiguration> {

    @Provides
    @Singleton
    public CustomerBackendManager customerBackendManager(DBI dbi, Injector injector) {
        CustomerBackendManager manager = dbi.onDemand(CustomerBackendManagerImpl.class);
        injector.injectMembers(manager);
        return manager;
    }

    @Provides
    @Singleton
    public RegisterServiceInterceptor registerServiceInterceptor(DBI dbi, Injector injector) {
        RegisterServiceInterceptor manager = dbi.onDemand(RegisterServiceInterceptor.class);
        injector.injectMembers(manager);
        return manager;
    }
}

我需要添加什么其他配置才能将此依赖项添加到此类?

1 个答案:

答案 0 :(得分:0)

我想出了解决这个问题的方法。

问题在于,Jersey期望配置中的Interceptor类,在满足拦截条件时需要调用它,这导致RegisterServiceInterceptor类被初始化两次。因此,RegisterServiceInterceptor类中的NPE。

我在另一个配置文件中将该配置定义为

@Provider
public class ResourceFilteringConfiguration implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        if (RegistrationService.class.equals(resourceInfo.getResourceClass())
                && resourceInfo.getResourceMethod().getName().equals("register")) {
            context.register(RegisterServiceInterceptor.class);
        }
    }

}

context.register(RegisterServiceInterceptor.class);行中,我传递了RegisterServiceInterceptor类本身,这导致它在达到依赖项配置(问题中提到的)之前进行了初始化。因此,此拦截器被初始化了两次,从而导致了此问题。我将配置文件代码更改为

@Provider
public class ResourceFilteringConfiguration implements DynamicFeature {

    @Inject
    private RegisterServiceInterceptor registerServiceInterceptor;

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        if (RegistrationService.class.equals(resourceInfo.getResourceClass())
                && resourceInfo.getResourceMethod().getName().equals("register")) {
            context.register(registerServiceInterceptor);
        }
    }

}

您一定已经注意到,我所做的更改是将RegisterServiceInterceptor的对象传递给配置,并且开始工作正常。