-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document ControllerAdviceBean as internal usage #32776
Comments
I agree with you, @sinanoezdemir , about your note on the Javadoc of ControllerAdviceBean (in the beginning of your comment). I, too, find the exact match between your statements and the Javadoc statements. That should be altered. However, pardon me for not diving deep into your usecase and context of micro services. |
Hello @sinanoezdemir , thanks for bringing this up. We have discussed this today as a team and I'll summarize here. We're not sure if you don't want your shared library to be scanned for components, or if it's scanned by micro-services but you would like to make this bit optional.
Are your applications using Spring Boot? If so, you could contribute your own auto-configurations and never rely on scanning for your library components. Your classes would still be annotated with Now depending on the outcome of this discussion, we might revisit the |
@bclozel thanks for your response. I just mentioned this issue because of what you sad in your last sentence
In our team we try to avoid relying on component scanning for libraries. Instead we want to manually configure the beans within the services and use the libraries like a blue print for these kind of things. In this case exception handling. I have a working workaround but I think it would be great if it would be possible to register ControllerAdvices by annotation and programmatically. Off course I understand that this is probably easier sad then done. |
Thanks for your response.
I don't think that pointing to a class would really qualify as programmatic registration. This class would still contain
The auto-configuration approach would be more aligned with your vision of programmatic registration in this case. Have you considered this? |
I have the same concerns。 1、common-spring.jar 2、app-1 GlobalExceptionHandler is useful 2、app-2 We may no need to package a new common-spring.jar just only modify the basePackages |
@qzmer1104 so you are looking for something like this, but for exception handling? Have you considered custom auto-configurations? |
Thank you, @bclozel, for your responses. I agree that @ExceptionHandler and @configuration would still be necessary, but that is acceptable. My main goal was to avoid relying on component scanning. With the configuration class, you have to register the bean manually, and the library containing the exception handlers does not create beans by itself. We try to avoid the creation of beans within libraries whenever possible. We also considered auto-configuration, and that approach would work. There are ways to implement a workaround, so that is not the issue. I just thought it would be beneficial if the framework supported this method of declaring controller advices. |
We have discussed this as a team and think that conditional configurations or Spring Boot auto-configurations are the preferred way of handling this use case. We'll repurpose this issue to update the |
Enhancement/ Misleading description in javadoc.
In the Javadoc of
ControllerAdviceBean
you can readControllerAdviceBean
Reading this I assume, that it should be possible to create ControllerAdviceBeans and register them programmatically (without using the
@ControllerAdvice
annotation).As far as I understood the ExceptionHandlerExceptionResolver is the class keeping the ControllerAdviceBeans to apply matching ExceptionHandlers when an exception occurs.
However, when initialising the exceptionHandlerAdviceCache it uses ControllerAdviceBean.findAnnotatedBeans which scans the application context for beans annotated with
@ControllerAdvice
.The exceptionHandlerAdviceCache is otherwise not modifiable. So there is actually no other way to add ControllerAdviseBeans besides using the annotation.
I think that's why the Javadoc is misleading.
However, it would be great to have the ability to define ControllerAdviseBeans without using the
@ControllerAdvice
annotation.I assumed something similar to this should work.
Please excuse me if I misunderstood a concept here. Just trying to suggest an improvement.
The text was updated successfully, but these errors were encountered: