Injection Error NullPointerException for AspectJ Annotation Class Java

旧巷老猫 提交于 2019-12-12 06:10:02

问题


I'm trying to inject a class called MeasurementService into an AspectJ Annotation class called MeasurementAspect but I received an error of NullPointerException and I checked that the injection of MeasurementService is null in this case. The injected class itself is then triggered in one of the advices.

The MeasurementAspect class itself doesn't have constructor as I think there is no need to have it and all advice will be injected to the appropriate methods and executed automatically.

Here is the excerpt of MeasurementAspect (as it is too long to copy all):

    @Aspect
    public class MeasurementAspect {
...   
    private @Inject MeasurementService measurementService;
    private final MeasureDownloadDto measureDownloadDto = new MeasureDownloadDto();
    private final MeasureUploadDto measureUploadDto = new MeasureUploadDto();

...

    @Around("execution(* *.storeFile(..))")
        public Object CalculateUploadMetadata(ProceedingJoinPoint joinPoint) throws Throwable {
            start = System.nanoTime();
            Object result = joinPoint.proceed();
            end = System.nanoTime();
            duration = end - start;
            setDurationUploadMetadata(duration);
            measureUploadDto.setDurationUploadMetadata(duration);
            System.err.println(
                    SDF.format(new Date()) + " Elapsed time for uploading meta data (ns) = " + getDurationUploadMetadata());
            System.err.println("measurementService" + measurementService);
            measurementService.storeUploadDto(measureUploadDto);
            durUploadEachChunk.clear();
            return result;
        }
...
}

Here is the MeasurementService class that will be injected:

public class MeasurementService {

    private final RemoteStatisticService remoteStatisticService;

    @Inject
    public MeasurementService(RemoteStatisticService remoteStatisticService) {
        this.remoteStatisticService = remoteStatisticService;
    }

    public void storeUploadDto(MeasureUploadDto measureUploadDto) {
        remoteStatisticService.postUploadStatistic(measureUploadDto);
    }

    public void storeDownloadDto(MeasureDownloadDto measureDownloadDto) {
        remoteStatisticService.postDownloadStatistic(measureDownloadDto);
    }

}

Any help is appreciated. Thanks

ps: It is still related to my another question NoAspectBoundException error in class constructor of AspectJ class with Dependency Injection , I hope it can give whole point of view of the problem

来源:https://stackoverflow.com/questions/37319528/injection-error-nullpointerexception-for-aspectj-annotation-class-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!