Search code examples
javaspringannotationsspring-annotations

Annotating simple POJO as @Component in Spring for enhancing behavior


I should be able to enhanced some POJO returned by some Spring controllers. To do that and detect those that should be enhanced, I want the developper to be able to put a class annotation on the POJO. The enhancing is doing both by combining a @ControllerAdvice that intercept the call and the detection of this special annotation on the returned POJOs to enhanced them. Getting the list of POJO that are candidate to the decoration by detecting them at the launch of the application instead of doing it dynamically when analyzing the response of the controller in the @ControllerAdvice would be of great benefit in term of performance.

In Spring, is it acceptable to use a custom annotation inheriting @Component to flag some simple POJO classes not aiming at being instantiated and managed by Spring with the simple goal to be able to get the list of them through ClassPathScanningCandidateComponentProvider or should I preferably use some other third party library like Reflections to do the job?

My point is to not pervert the meaning of @Component just for the confort of the annotation scanning mechanism offered by Spring if @Component has no sense in this case.


Solution

  • We finally chose the following method: https://stackoverflow.com/a/1415338/256561 based on custom use of ClassPathScanningCandidateComponentProvider.