Injection Error NullPointerException for AspectJ A

2019-09-05 16:55发布

问题:

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