Reputation: 218
I have some homemade annotation (let us call it @A
) that can be used on RequestMappings to work with some OncePerRequestFilter
(called MyFilter
here).
I have made an autoconfiguration to conditionally create the MyFilter
bean, and I now wish to perform the following check: if @A
is used on at least one of my request mappings, throw an error if no instance of MyFilter
is available.
In trying so, I have tried:
@Validated
/@Valid
on the method and/or the autoconfiguration:
@AssertTrue(message = "Annotation A may not be used, no instance of MyFilter was provided")
private boolean isFilterAvailableIfAnnotationIsUsed(RequestMappingHandlerMapping requestMappingHandlerMapping, Optional<MyFilter> myFilter) {
if (myFilter.isEmpty()) {
for (HandlerMethod handlerMethod : requestMappingHandlerMapping.getHandlerMethods().values()) {
if (handlerMethod.hasMethodAnnotation(A.class)) {
return false;
}
}
}
return true;
}
@PostConstruct
, but it does not take beans as argumentsBeanPostProcessor
, but I am not sure I could use it that wayNow, the only idea I have left is to create a dummy bean that depends on RequestMappingHandlerMapping
and Optional<MyFilter>
, and which goal is just to throw an exception if the conditions are satisfied.
Is there a clean way of performing something like this?
Upvotes: 0
Views: 23