我有一個Java注入問題,因爲我想注入一個名爲RemoteStatisticService的接口,但它在這種情況下一直返回null,因此錯誤爲NullPointerException。我試圖用init()方法和@PostConstruct來關注this,但仍然給我提供了同樣的錯誤。注入CDI的接口返回空指針異常
下面是MeasurementAspectService類的代碼:
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import *.dto.MeasureDownloadDto;
import *.dto.MeasureUploadDto;
import *.rs.RemoteStatisticService;
public class MeasurementAspectService {
private @Inject RemoteStatisticService remoteStatisticService;
public void storeUploadDto(MeasureUploadDto measureUploadDto) {
remoteStatisticService.postUploadStatistic(measureUploadDto);
}
public void storeDownloadDto(MeasureDownloadDto measureDownloadDto) {
remoteStatisticService.postDownloadStatistic(measureDownloadDto);
}
@PostConstruct
public void init() {
}
}
這是任何幫助表示讚賞接口類RemoteStatisticService
import static *.util.RemoteServiceUtil.PRIV;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import *.dto.MeasureDownloadDto;
import *.dto.MeasureUploadDto;
@Path(PRIV + "stats")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public interface RemoteStatisticService {
@POST
@Path("upload")
void postUploadStatistic(MeasureUploadDto mud);
@POST
@Path("download")
void postDownloadStatistic(MeasureDownloadDto mdd);
}
的代碼。謝謝
爲了有效注入,他們需要標記爲'@ Named' bean供CDI提供者處理,例如。 'Weld' – PDStat
在這種情況下,我應該在哪裏放置@named bean?我正在使用Maven,並且仍然與CDI一起使用 –
在課程級別,例如。 '@Named public class MeasurementAspectService' – PDStat